How can I compare whole set of binary images

1 view (last 30 days)
Hey people,
I need a help, because I don't know how to extricate myself from a code. My code has to compare two binary images. Image 1 has just some pixels with value 0 and that is also case for Image 2. So, task of my code is to compare if those pixels are overlapped, and if so, overlapped pixels on Image 2 would get a value 1. It has do a recursion for a set of binary images.
I've already done something, but manually.
Example:
I give you my unfinished code, but there are some written comments what it needs to do at those lines.
myFolderInput = 'F:\input_with_flakes\';
myFolderOutput= 'F:\output_wihout_flakes\';
if ~isdir(myFolderInput)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolderInput);
uiwait(warndlg(errorMessage));
return;
end
if ~isdir(myFolderOutput)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolderOutput);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolderInput, '*.jpg');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(myFolderInput, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
[i j]=size(imageArray)
if k==1
fprintf('The first one is skipped\n')
else
for m=1:i
for n=1:j
if imageArray(m,n)==imageArray(m,n) % if Previous Image(m,n)==Current Image(m,n)
imageArray(m,n)=1; % Current Image(m,n)=1;
elseif imageArray(m,n)==0 % elseif Current Image(m,n)==0
imageArray(m,n)=0; % Current Image(m,n)=0;
end
end
end
end
filename = sprintf('Excluded_%s', baseFileName);
imwrite(imageArray,[myFolderOutput, filename],'jpeg');
end

Answers (1)

Christian Lenz
Christian Lenz on 25 Jul 2012
Would it be possible to use a simpler code?
imgOverlap_pos = img1 .* img2;
This is pixel-wise multiplication. It can be 0*0=0, 0*1=0, 1*0=0, 1*1=0. So your resulting image shows ones in the places where the two images overlap. Then do the same with the negative images.
imgOverlap_neg = imcomplement(img1) .* imcomplement(img2);
And your result would be:
imgOverlap = imgOverlap_pos + imgOverlap_neg;
Around that you'll then need one loop to go through all your images.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!