I want to subtract gray values of two images and want to make those regions as black where gray values doesn't match.But I am having problem in coding in the if condition where I want to compare gray values of images.

10 views (last 30 days)
Please let me know how to get gary value of every pixel location and how to compare them.
The code what i have done is:
l= imread('B.jpg');
k= imread('BF.jpg');
L = rgb2gray(l);
K = rgb2gray(k);
for i=1:256
for j= 1:384
if L(i,j) == K(i,j)
K(i,j)=1;
else K(i,j)=0;
end
end
end
imshow(K);
but when i run this it shows a complete black image.

Accepted Answer

Walter Roberson
Walter Roberson on 12 Jul 2015
l= imread('B.jpg');
k= imread('BF.jpg');
L = rgb2gray(l);
K = rgb2gray(k);
K = (K==L);
imshow(K);
This could easily be all black as well. You assume that the gray levels of the two will sometimes be identical but that is not certain. Gray intensity is calculated as 0.2989 * R + 0.5870 * G + 0.1140 * B . If floating point calculations were carried out in decimal then as 2989, 5870, and 1140 have no common factors, the result would be unique for each possible combination of R, G, and B values -- that is, comparing gray values would be equivalent to testing if the R, G, and B were all identical. As it is, floating point values are not carried out in decimal, and only 4358677 of the 16777216 possible values occur in practice, but that is still more than 1 in 4 of the possibilities.
Now if you were to quantize the gray values to 256 different levels, that would be different:
l= imread('B.jpg');
k= imread('BF.jpg');
L = im2uint8(rgb2gray(l));
K = im2uint8(rgb2gray(k));
K = (K==L);
imshow(K);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!