-1

I'd like to implement an algorithm capable of comparing two images using the C api of OpenCV 3.0.0 . The image are captured by webcam so they have the same resolution (don't worry is small) and the same file format (jpeg) (but we can change it if we want) My idea is to get two image, transform in b&w and with a simple for construct compare bit by bit. In b&w we can consider the image as a full matrix with 0s and 1s. Example :

Image1 (black and white)

  • 0 0 0 1
  • 0 1 0 1
  • 0 0 0 0
  • 0 0 1 1

So i know there's a lot of way to do it with opencv but i read a lot of example that are useless for me. So my idea is to manually operate with every pixel. Considering the image as a matrix we can compare pixel by pixel with a simple double construct for (pixel_image[r:rows][c:columns] == pixel_image2[r:rows][c:columns] only if the "pixel" is considered as integer. So the problem is how can i access to the value of pixel from IplImage type?
Certainly in this way i think i have a bad optimization of code, or maybe i can't adapt it to a large scale of image data.

(Please don't answer me with: "use c++ - use python - c is deprecated ecc..", i know there're so many way to do it in easy way with an high level code)

Thanks to all.

Andrea
  • 1
  • 1
  • 3
    Pixel-wise comparison won't give you any satisfactory results. Think of an image consisting of vertical black and white stripes of one pixel width. Now think that on one of the images it is shifted one pixel left. Not a big deal, right? But you algorithm will consider these two images maximally different. – Eugene Sh. Mar 03 '21 at 18:24
  • Yes i know it, indeed i will insert a sort of treshold error in percentage(only if i use my algorithm). So the problem is the same, how i can access to the pixel value from IplImage type? @EugeneSh. – Andrea Mar 03 '21 at 18:31

1 Answers1

0

Ok i solved. This way u can access the value of single pixel (if the image is loaded as RGB)( rememeber that opencv see BGR)

IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
double tmpb,tmpg,bmpr;
for(int i=0;i<img->height;i++){
    for(int j=0;j<img->width;j++){
        tmpb=cvGet2D(img,i,j).val[0];
        tmpg=cvGet2D(img,i,j).val[1];
        tmpr=cvGet2D(img,i,j).val[2];
    }
}

If the image is a single channel (for example if we convert it to black and white) is

IplImage* img=cvCreateImage(cvSize(640,480),IPL_DEPTH_32F,3);
int tmp 
for(int i=0;i<img->height;i++){
    for(int j=0;j<img->width;j++){
        tmp=cvGet2D(img,i,j).val[0];
        printf("Value: %d",tmp);
    }
}
Andrea
  • 1
  • 1