I'm really stuck here.. average images
3 views (last 30 days)
Show older comments
Hi so i've been working on this code for awhile now.
the idea behind it is that i used a machine to capture some images, the images i have are an object, the background without the object. and an image when the machine is switched off. what im trying to do is subtract the object image from both backgrounds.
the idea behind the code is i select an image(or images) , then select the flat images (background without the object) and average them, then select the dark images(when the machine is switched off) average that. then do some subtracting and division
some pixels that are impaired, so I've gotten the average for the pixel before and after. the image im getting looks like a barcode with alot of white lines up and down the image, rather than the original image with the background with adjustments
the images i'm using can be found at the site: https://www.flickr.com/photos/153947713@N07/?
the code i'm using below
clear
clc
close all
%-------------------------------------------------------------------------%
[filelist,workdir] = uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...
'*.*','All Files'},'Select the image(s)','MultiSelect', 'on'); %lets you select an image
if(~iscell(filelist))
img0 = imread(strcat(workdir,filelist));
nimg=1;
else
img0 = imread(strcat(workdir,filelist{1}));
nimg = numel(filelist); %number of elements in the array *howmany images you are selecting*
end
siz = size(img0); %array size of the selected image
ysize=siz(1);%rows
xsize=siz(2);%cols
bad = [78,1849]; %bad section of the image
%-----------------------------------------------------------------------------------------------------------------------------------------------%
filesAndFolders = dir(workdir);
filesInDir = filesAndFolders(~([filesAndFolders.isdir])); % Returns only the files in the directory
[flatlist,~] = uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...
'*.*','All Files'},'Select the flat image(s)','MultiSelect', 'on');
%flatlist = fullfile( workdir,{1:5} );
nflat = numel(flatlist); %number of images selected
%-----------------------------------------------------------------------------------------------------------------------------------------------%
[darklist,~] = uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...
'*.*','All Files'},'Select the dark image(s)','MultiSelect', 'on');
%darklist = fullfile( workdir,{1:5} );
ndark = numel(darklist);
dark = zeros(ysize,xsize); %zero's is similiar to fltarr, creates an array of zero's
flat = zeros(ysize,xsize);
%----------------------------------------------------------------------------------------------------------------------------%
for k = 1:ndark
if(~iscell(darklist))
imtemp = imread(darklist);
else
imtemp = imread(darklist{k});
end
averagedark = dark+single(imtemp)./ndark; %single or double
% ;Average dark images
end
%1116 to 1117 and 728 to 729
averagedark(1117,:)=(averagedark(1116,:)+averagedark(1118,:))/2 ;%;bad sector found by observation and is corrected from neighboring pixels
averagedark(729,:)=(averagedark(728,:)+averagedark(730,:))/2 ;
%-----------------------------------------------------------------------------------------------------------------------------------------------%
for k = 1:nflat
if(~iscell(flatlist))
imtemp = imread(flatlist);
else
imtemp = imread(flatlist{k});
end
averageflat = flat+single(imtemp)./nflat;%single or double
%;Average flat image
end
%1116 to 1117 and 728 to 729
averageflat(1117,:)=mean(averageflat(1116,:)+averageflat(1118,:));
averageflat(729,:)=mean(averageflat(728,:)+averageflat(730,:));
%-----------------------------------------------------------------------------------------------------------------------------------------------%
flatsub = averageflat-averagedark;
flatsub = max(0, averageflat - averagedark);
%flatsub(find(flatsub <= 0)) = 1;
flatsub(:,bad)=(flatsub(:,bad-1)+flatsub(:,bad+1))/2;
averagedark(:,bad)=(averagedark(:,bad-1)+averagedark(:,bad+1))/2;
mflat = mean(flatsub);
if((~iscell(filelist)))
im0 = imread(filelist); %multiple selected images
else
im0 = imread(filelist{1}); %returns the first image of the file
end
%1116 to 1117 and 728 to 729
im0(1117,:)=(im0(1116,:)+im0(1118,:))/2;
im0(729,:)=(im0(728,:)+im0(730,:))/2;
image_corrected=(single(im0)-averagedark)/flatsub;
resizedimage = imresize(image_corrected,[ ysize/4 xsize/4] ); %resize the image by multiplying the y and x axis by 4
imshow(resizedimage)
any help would be apperciated
0 Comments
Answers (1)
Image Analyst
on 19 Aug 2017
Attached is my demo for averaging images. Not sure what else to offer.
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!