0

I want to make decision that whether two images match or not on the basis of pixel value.Here is a portion of my code

Vec3b intensity = image.at<Vec3b>(j, i);
uchar blue = intensity.val[0];
uchar green = intensity.val[1];
uchar red = intensity.val[2];

uchar col=blue+green+red;

similarly intensity1,blue1,....for other image and compare 'col 'of both images. But it is not giving correct output (unable to match two pixels in terms of their pixel values) ,Does this code is calculating intensities correctly??

skm
  • 5,015
  • 8
  • 43
  • 104
  • How does it `not giving correct output`? – herohuyongtao Feb 21 '14 at 19:41
  • My code reads two images Mat image=imread("b.bmp"); Mat templat=imread("a.bmp"); output is same in both conditions that is (i). when I load same image in "image" and "template" and (ii) when I load different image in "image" and "template". – user3278011 Feb 22 '14 at 03:54

2 Answers2

0

because your pixel values are in unsigned char form and you know that char cannot be added...so type cast the pixel values in int and then add.

An example: Have you ever seen something like this sum = 'a' + 'b' + 'c'; Does it make any sense? And you are doing the same by adding the three uchar values.

So, try to do the following:

int col = (int)red + (int)green + (int)blue;

do the same for the pixel which is to be matched.

But i would suggest you to compare the pixel by comparing their hue value which is more easy to understand for you as well.

step-1: convert your image to HSV cvtColor(src , dst, CV_RGB2HSV);

step-2: Then calculate the vale of hue (don't forget to do typecast as mentioned above) for the given pixel

step-3: Do the same thing for another pixel which you would like to compare.

skm
  • 5,015
  • 8
  • 43
  • 104
0

It's better to use luma (which is the weighted sum of gamma-compressed R'G'B' components of a color video) as the intensity for better matching:

intensity = 0.2126 R + 0.7152 G + 0.0722 B

You may also interested in Converting RGB to grayscale/intensity.

Community
  • 1
  • 1
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174