OpenCV compare two images and get different pixels
Posted
by
Richard Knop
on Stack Overflow
See other posts from Stack Overflow
or by Richard Knop
Published on 2010-12-28T23:53:43Z
Indexed on
2010/12/29
0:54 UTC
Read the original article
Hit count: 181
For some reason the code bellow is not working. I have two 640*480 images which are very similar but not the same (at least few hundred/thousand pixels should be different).
This is how I am comparing them and counting different pixels:
unsigned char* row;
unsigned char* row2;
int count = 0;
// this happens in a loop
// fIplImageHeader is current image
// lastFIplImageHeader is image from previous iteration
if ( NULL != lastFIplImageHeader->imageData ) {
for( int y = 0; y < fIplImageHeader->height; y++ )
{
row = &CV_IMAGE_ELEM( fIplImageHeader, unsigned char, y, 0 );
row2 = &CV_IMAGE_ELEM( lastFIplImageHeader, unsigned char, y, 0 );
for( int x = 0; x < fIplImageHeader->width*fIplImageHeader->nChannels; x += fIplImageHeader->nChannels )
{
if (row[x] == row2[x]) // the pixel in the first channel (usually G)
{
count++;
}
if (row[x+1] == row2[x+1]) // ... second channel (usually B)
{
count++;
}
if (row[x+2] == row2[x+2]) // ... third channel (usually R)
{
count++;
}
}
}
}
Now at the end I get number 3626 which would seem alright.
But, I tried opening one of the images in MS Paint and drawing thick red lines all over it which should increase the number of different pixels substantially. I got the same number again: 3626.
Obviously I am doing something wrong here.
I am comparing these images in a loop.
This line is before the loop:
IplImage* lastFIplImageHeader = cvCreateImageHeader(cvSize(640, 480), 8, 3);
Then inside the loop I load images like this:
IplImage* fIplImageHeader = cvLoadImage( filePath.c_str() );
// here I compare the pixels (the first code snippet)
lastFIplImageHeader->imageData = fIplImageHeader->imageData;
So lastFIplImageHeader is storing the image from the previous iteration and fIplImageHeader is storing the current image.
© Stack Overflow or respective owner