简体   繁体   English

如何将两个图像与 c 进行比较

[英]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.我想实现一种能够使用 OpenCV 3.0.0 的 C api 比较两个图像的算法。 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.图像由网络摄像头捕获,因此它们具有相同的分辨率(不要担心很小)和相同的文件格式(jpeg)(但我们可以根据需要更改它)我的想法是获取两张图像,以黑白和用一个简单的 for 构造逐位比较。 In b&w we can consider the image as a full matrix with 0s and 1s.在 b&w 中,我们可以将图像视为具有 0 和 1 的完整矩阵。 Example:例子:

Image1 (black and white) Image1(黑白)

  • 0 0 0 1 0 0 0 1
  • 0 1 0 1 0 1 0 1
  • 0 0 0 0 0 0 0 0
  • 0 0 1 1 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.所以我知道用 opencv 有很多方法可以做到这一点,但我读了很多对我没用的例子。 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?将图像视为一个矩阵,我们可以用一个简单的双精度结构来逐个像素地比较 (pixel_image[r:rows][c:columns] == pixel_image2[r:rows][c:columns] 只有当“像素”是被认为是 integer。所以问题是如何从 IplImage 类型中访问像素值?
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) (请不要回答我:“使用 c++ - 使用 python - c 已弃用 ecc..”,我知道有很多简单的方法可以用高级别的方法做到这一点)

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)这样你就可以访问单个像素的值(如果图像加载为 RGB)(请记住 opencv 见 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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