简体   繁体   中英

How to compare two image with c

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.

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM