Comparing 2 images by subtracting pixels and what happen if rgb values goes negative?
Show older comments
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
Gul Rukh Khan
on 8 Dec 2019
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
Image Analyst
on 8 Dec 2019
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.
Accepted Answer
More Answers (1)
Walter Roberson
on 4 Feb 2012
1 vote
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
dhrye
on 4 Feb 2012
Walter Roberson
on 4 Feb 2012
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.
Walter Roberson
on 4 Feb 2012
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
dhrye
on 4 Feb 2012
Edited: Walter Roberson
on 22 Jul 2019
Walter Roberson
on 4 Feb 2012
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.
dhrye
on 4 Feb 2012
Walter Roberson
on 4 Feb 2012
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.
dhrye
on 4 Feb 2012
Walter Roberson
on 4 Feb 2012
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.
dhrye
on 4 Feb 2012
Image Analyst
on 4 Feb 2012
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.
dhrye
on 5 Feb 2012
Edited: Walter Roberson
on 22 Jul 2019
YK Toh
on 4 Jan 2013
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
Walter Roberson
on 4 Jan 2013
What point is it happening at for you? The array that it is complaining about, what does class() of it show ?
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!