Counting overlapping pixels between two images

I'm looking to find the number of overlapping shaded pixels between two images. The two images are attached. How could I count the overlapping shaded red pixels between the two images? Any assistance would be much appreciated. If possible an easily repeatable process, as I have to run it with 64 images.

 Accepted Answer

Call imregister to align them. Then use min() to take the min values of the two images.
By the way, I'll be soon leaving for canoe camping over the weekend so good luck. I'll be back Sunday afternoon.

11 Comments

Thanks for all your help, have a great trip!
If you're still around could you quickly help me with what A would be in my case (using min(A,[],dim), right?)? I'm confused by the min help site.
A would be the two images stacked:
A = cat(3, image1, image2);
See if that works. If not, do it on each color channel one at a time.
So here's my code so far. Am I on the right track? I'm not getting any error messages but I'm also not getting a min value when I run the script.
Not really. This will take a while to fix...
This is about all I had time for:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 13;
filename = {'CMHJ_A1FC';'CMHJ_A2FC';'CMHJ_A3FC';'CMHJ_A4FC';'CMHJ_A5FC';'CMHJ_A6FC';...
'CMHJ_A7FC';'CMHJ_A8FC';'CMHJ_B1FC';'CMHJ_B2FC';'CMHJ_B3FC';...
'CMHJ_B4FC';'CMHJ_B5FC';'CMHJ_B6FC';'CMHJ_B7FC';'CMHJ_B8FC'};
folder = 'C:\Users\Matt\Documents\Temporary';
fixedFileName = fullfile(folder, 'CMHJ_P1FC.jpg');
if ~exist(fixedFileName, 'file')
message = sprintf('Moving file name #%d does not exist:\n%s\nSkipping it.', k, fixedFileName);
uiwait(v(message));
return;
end
fixedReference = imread (fixedFileName);
subplot(1, 3, 1);
imshow(fixedReference);
title('Fixed Reference Image', 'fontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
drawnow;
for k = 1 : length(filename)
movingFileName = fullfile(folder, sprintf('%s.jpg', filename{k}));
if ~exist(movingFileName, 'file')
promptMessage = sprintf('Moving file name #%d does not exist:\n%s\nSkipping it.', k, movingFileName);
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
break;
end
continue;
end
movedImage = imread (movingFileName);
subplot(1, 3, 2);
imshow(movedImage);
caption = sprintf('Moved image: %s', filename{k});
title(caption, 'fontSize', fontSize, 'Interpreter', 'none');
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
drawnow;
% Register the moved image to the fixed, reference image.
% imshowpair (fixedReference, movedImage);
%
% fixedReference = rgb2gray (fixedReference);
% movedImage = rgb2gray (movedImage);
% [optimizer, metric] = imregconfig ('multimodal');
% movingRegisteredDefault = imregister (fixedReference, movedImage, 'affine', optimizer, metric);
% subplot(1, 3, 2);
% imshowpair (movingRegisteredDefault, movedImage)
% title('reg', 'fontSize', fontSize);
stackedImage = cat(3, movedImage, fixedReference);
minImage = min(stackedImage, [], 3);
subplot(1, 3, 3);
imshow(minImage);
title('Combined Image', 'fontSize', fontSize);
promptMessage = sprintf('Do you want to Continue processing,\nor Cancel to quit processing?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
break;
end
end
Okay thanks for your help, hope you had a good canoe trip
How to calculate overlap measure between two x-ray images , and the picture is attached to the message. Please answer a question. Thank you
mohamed, start a new question and attach your two original images, and the code that found the red and green coordinates. And clearly specify what is the overlap area you want. Is it the area that is common to both the green and red outlines regions
I want the code that defines the green points on the mask, and calculates the overlap between the area that is common to both the green and red outlines regions, the code that I have specifies the red line only on the image
Well just do something similar to get the green line. I'm not sure what is different - maybe a different parameter at some point in your algorithm. Then you can get the mask for each and AND them:
[rows, columns, numberOfColorChannels] = size(yourImage);
redMask = poly2mask(xRed, yRed, rows, columns];
greenMask = poly2mask(xGreen, yGreen, rows, columns];
overlap = redMask & greenMask;
overlapArea = nnz(overlap);
Again, let's take this to a different thread, so we don't keep sending Matthew emails about new activity in his 6 year old question. You can reply here ONCE again, ONLY to put the link to your new question.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!