Comparing 2 images by subtracting pixels and what happen if rgb values goes negative?

Hi,
I am wondering how do you go about comparing the pixels of each images with subtraction and return the image with higher pixel difference. If subtraction works but rgb values goes negative, will it just display a black screen?

2 Comments

I wrote a code for the same, but still with a problem.
i have 10x10 matrix image.
for x=1:9
for y=1:9
r (x,y) = (aa(x,y)-(aa(x,y+1)));
x=x+1;
end
end
Gul, matrices and images are indexed (y, x), not (x,y). And you should not assign x to something when x is being assigned by the for statement. Start a new question if you still need help.

Sign in to comment.

 Accepted Answer

dhrye:
OK, look at your code very carefully:
image2 = 'image2.jpg';
% Display it.
imread(image2);
Now, does that look like how I told you to fix it? (Which is below)
image2 = imread('image2.jpg');
imshow(image2);
No, it does not. You're not displaying it because imread reads it in from a disk file, it does not display it in an axes like imshow does. And you're still having image2 be a character string despite me telling you to set it equal to the output of imread. Correct it and try again.

3 Comments

Thanks image analyst, I got over zealous about making the code work. The image is out!
??? Array dimensions must match for binary array op. Did you mean this error for the character string?
Come on, you know better that that by now. You must copy and paste the actual red text error message. What you said is paraphrased. All I can say is that you're doing something unknown operation with some unknown images where the sizes of the images aren't the same.
alright. matlab's new to me and i really suck at programming. Thanks for the advice.

Sign in to comment.

More Answers (1)

If you subtract one uint8() value from another uint8() value, and the result would abstractly be 0, then the uint8() data type will "saturate" the negative values to 0 (e.g., black on a grayscale image.)
If you convert the pixels to double precision before you do the subtraction, then the double precision result for any one pixel can be negative.
I do not think I quite understand what you mean about returning the image with the higher pixel difference?
Consider two cases. In the first case, the two images are exactly the same except that one pixel is 255 different than the other (e.g., a spot of white compared to a spot of black.) The total pixel difference would be 255 since the difference for all the other pixels is 0 (under the assumption that they are exactly the same.) Now in the second case, the two images are exactly the same except that 256 pixels differ by exactly 1 -- pretty much invisible if those pixels are scattered around. The total pixel difference would be 256 * 1 = 256 (under the assumption that they are exactly the same.) Now, which case has the higher pixel difference? The first case with its big 255 difference in one place, or the second because its total 256 invisible differences add up to more?

14 Comments

actually I would want to get an image that is different from the original in the sense of a tracking system.
For example, the first image shows the corridor and the second image shows a person at the corridor. I want to display the second image as there's a change.
I am using this for a start but it doesn't seem to work.
% Calculate difference:
differenceImage = grayImage - image2;
% Display it.
subplot(2, 2, 3);
imshow(differenceImage, []);
title('Difference Image', 'FontSize', fontSize);
% Threshold the difference.
thresholdedImage = differenceImage > 4;
% Sum up the image.
sumOfAllPixels = sum(sum(thresholdedImage));
minAllowableDifference = 1000; % Choose something that works for you.
if sumOfAllPixels > minAllowableDifference
uiwait(msgbox('The images are different.'));
else
uiwait(msgbox('The images are the same.'));
end
Your images are probably uint8() datatype. You should probably use
differenceImage = double(grayImage) - double(image2);
Also, in your line
thresholdedImage = differenceImage > 4;
consider using
thresholdedImage = abs(differenceImage) > 4;
Remember, you might happen to start with something bright in view that then moves away; you don't want to be ignoring everything in that bright area afterwards just because all the subtractions after that in the area go negative.
By the way, if you skim through the User Manual for the following camera and read the section on Motion Detection, you might find the technique interesting:
http://www.vivotek.com/products/model.php?network_camera=ip7161
fullFileName = 'image1.jpg';
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
screen.
image2 = 'image2.jpg';
% Display it.
subplot(2, 2, 2);
imshow(image2, []);
title('Second Image', 'FontSize', fontSize);
% Calculate difference:
differenceImage = double(grayImage) - double(image2);
% Display it.
subplot(2, 2, 3);
imshow(differenceImage, []);
title('Difference Image', 'FontSize', fontSize);
% Threshold the difference.
thresholdedImage = abs(differenceImage > 4);
% Sum up the image.
sumOfAllPixels = sum(sum(thresholdedImage));
minAllowableDifference = 1000; % Choose something that works for you.
if sumOfAllPixels > minAllowableDifference
uiwait(msgbox('The images are different.'));
else
uiwait(msgbox('The images are the same.'));
end
okay thanks. There's something wrong with my code. Is it okay if you help me see where I went wrong. Been on this for days. And I just started using MatLab
What problem are you observing? There is nothing immediately obvious, other than the fact that you have not specifically set a colormap() for your imshow() purposes.
Do take note that if there _are_ negative values, then because you have requested automatic value mapping in imshow(), it is the most negative value that would have the lowest color index (e.g. black), and that the color corresponding to a value of 0 could end up a fair ways through the color index (e.g. lighter gray). In some cases the color corresponding to difference 0 could end up coming out white.
this was the error that I got.
??? screen.
|
Error: Expression or statement is incomplete or incorrect.
Also to note. If there are no difference, I would want to return the original image.
Take a closer look at
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
screen.
The word "screen" should be part of the previous line, not on a line by itself.
hmm, fml but i still get the same error. and it doesnt really compare the image. the 1st image pop up with the text original grayscale image. that's all. It's not that easy to compare images huh? esp for a beginner?
It is still complaining about "screen." ? And you saved the file after you edited it? Then just delete the comment
% Enlarge figure to full screen.
and save.
The error with "screen." would have prevented the program from getting any further than displaying the title on the original image.
??? Error using ==> imageDisplayParsePVPairs at 72
Invalid input arguments.
Error in ==> imageDisplayParseInputs at 70
[common_args,specific_args] = imageDisplayParsePVPairs(varargin{:});
Error in ==> imshow at 199
[common_args,specific_args] = ...
then it showed this. how long have u been doing matlab?
Ha! I think Walter has been doing MATLAB more than a day or two. Look at your code (which is actually an adaption of my code I must have posted somewhere):
image2 = 'image2.jpg';
% Display it.
subplot(2, 2, 2);
imshow(image2, []);
You're trying to show a character string 'image2.jpg' while it expects a 2D or 3D numerical array. Try this:
image2 = imread('image2.jpg');
so that image2 will be the actual image (numerical array) rather than a character string.
yeah, image analyst. Am new to this coding and decided I could get some code and start to edit to my preference. I have this error now after editing the errors you mentioned. Cheers!
Do I have to use imsubtract?
??? Error using ==> minus
Matrix dimensions must agree.
Here's my code:
>> fullFileName = 'image1.jpg';
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', 20);
set(gcf, 'Position', get(0,'screensize'));
% Make a second image with a black block on it.
image2 = 'image2.jpg';
% Display it.
imread(image2);
% Calculate difference:
differenceImage = double(grayImage) - double(image2);
% Display it.
subplot(2, 2, 3);
imshow(differenceImage, []);
title('Difference Image', 'FontSize', 20);
% Threshold the difference.
thresholdedImage = abs(differenceImage > 4);
% Sum up the image.
sumOfAllPixels = sum(sum(thresholdedImage));
minAllowableDifference = 100; % Choose something that works for you.
if sumOfAllPixels > minAllowableDifference
uiwait(msgbox('The images are different.'));
imshow(image2);
else
uiwait(msgbox('The images are the same.'));
imshow(fullFileName);
end
dhrye, i am trying your code...got errors as below..not really understnad with this error, Pls help...tq~
Error using image Error using image Numeric or logical matrix required for image CData
What point is it happening at for you? The array that it is complaining about, what does class() of it show ?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!