虽然找到2张图片之间的差异,但OpenCV差异大于预期

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了虽然找到2张图片之间的差异,但OpenCV差异大于预期相关的知识,希望对你有一定的参考价值。

我正在使用OpenCV(在android NDK中),我遇到了问题。我想找到两张图片之间的差异,而不是削减差异。但是,以下输出使差异更大。我使用了这个问题的图像CV - Extract differences between two images。我试图解决它,但没有成功。这是输出

Mat& backgroundImage = *(Mat*) addrRgba;
Mat& currentImage = *(Mat*) addrRgba2;
Mat diffImage;
absdiff(backgroundImage, currentImage, diffImage);
Mat mask=currentImage.clone();

          float threshold = 30.0f;
            float dist;

          for(int j=0; j<diffImage.rows; ++j)
              for(int i=0; i<diffImage.cols; ++i)
              {
                  if(diffImage.at<cv::Vec3b>(j,i)==Vec3b(0,0,0)){
                    Point center( i , j);
                   circle (mask,center,1,Scalar( 255, 255, 255 ),-1,9,0);
                  }

              }

               currentImage=mask;

第一张图片enter image description here第二张图片enter image description here结果enter image description here

另一方面,这段代码给了我这样的输出

Mat& backgroundImage = *(Mat*) addrRgba;
Mat& currentImage = *(Mat*) addrRgba2;
Mat diffImage;

      absdiff(backgroundImage, currentImage, diffImage);
      Mat gray;

      cvtColor(diffImage,gray, COLOR_BGR2GRAY);

      equalizeHist( gray, gray );
      Mat mask=currentImage.clone();
      cvtColor(mask,mask, COLOR_BGR2GRAY);
      float threshold = 30.0f;
      float dist;

                for(int j=0; j<gray.rows; ++j)
                    for(int i=0; i<gray.cols; ++i)
                    {
                        cv::Vec3b pix = gray.at<cv::Vec3b>(j,i);

                        if(pix==Vec3b(0,0,0)){

                              Point center( i , j);
                              circle (mask,center,1,Scalar( 255, 255, 255 ),-1,9,0);
                        }
                    }
                    Mat maskedImage;
                diffImage.copyTo(maskedImage,mask);
               currentImage=mask;

enter image description here ps:对不起设计感到抱歉

答案

我在python中的结果:

enter image description here

# 2017.12.22 15:48:03 CST
# 2017.12.22 16:12:26 CST

import cv2
import numpy as np

img1 = cv2.imread("img1.png")
img2 = cv2.imread("img2.png")
diff = cv2.absdiff(img1, img2)
gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)

## find the nozero regions in the gray
imask =  gray>0

## create a Mat like img2
canvas = np.zeros_like(img2, np.uint8)

## set mask 
canvas[imask] = img2[imask]
cv2.imwrite("result.png", canvas)

用c ++更新

//! 2017.12.22 17:05:18 CST
//! 2017.12.22 17:22:32 CST

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main() {

    Mat img1 = imread("img1.png");
    Mat img2 = imread("img2.png");

    // calc the difference
    Mat diff;
    absdiff(img1, img2, diff);

    // Get the mask if difference greater than th
    int th = 10;  // 0
    Mat mask(img1.size(), CV_8UC1);
    for(int j=0; j<diff.rows; ++j) {
        for(int i=0; i<diff.cols; ++i){
            cv::Vec3b pix = diff.at<cv::Vec3b>(j,i);
            int val = (pix[0] + pix[1] + pix[2]);
            if(val>th){
                mask.at<unsigned char>(j,i) = 255;
            }
        }
    }

    // get the foreground
    Mat res;
    bitwise_and(img2, img2, res, mask);

    // display
    imshow("res", res);
    waitKey();
    return 0;
}

类似的答案:

  1. CV - Extract differences between two images
  2. While finding a difference between 2 pictures OpenCV difference is bigger than it is supposed to be
另一答案

从这里:CV - Extract differences between two images

cv::Mat diffImage;
cv::absdiff(backgroundImage, currentImage, diffImage);

cv::Mat foregroundMask = cv::Mat::zeros(diffImage.rows, diffImage.cols, CV_8UC1);

float threshold = 30.0f;
float dist;

for(int j=0; j<diffImage.rows; ++j)
    for(int i=0; i<diffImage.cols; ++i)
    {
        cv::Vec3b pix = diffImage.at<cv::Vec3b>(j,i);

        dist = (pix[0]*pix[0] + pix[1]*pix[1] + pix[2]*pix[2]);
        dist = sqrt(dist);

        if(dist>threshold)
        {
            foregroundMask.at<unsigned char>(j,i) = 255;
        }
    }

然后进行背景减法。

另一答案

我在@Silencer的帮助下这样做了,因此我得到了正确的结果。希望如果有人遇到类似问题,这将有所帮助

      Mat& backgroundImage = *(Mat*) addrRgba;
      Mat& currentImage = *(Mat*) addrRgba2;
      Mat HSV_currentImage;
      Mat HSVbackgroundImagebg;
      Mat diffImage;
      cvtColor(backgroundImage, HSVbackgroundImagebg, CV_BGR2HSV);
      cvtColor(currentImage, HSV_currentImage, CV_BGR2HSV);
      absdiff(HSVbackgroundImagebg, HSV_currentImage, diffImage);
      Mat mask(diffImage.size(), CV_8UC1);
                float threshold = 30.0f;
                float dist;

                for(int j=0; j<diffImage.rows; ++j)
                    for(int i=0; i<diffImage.cols; ++i)
                    {
                        Vec3b pix = diffImage.at<cv::Vec3b>(j,i);
                        dist = (pix[0]*pix[0] + pix[1]*pix[1] + pix[2]*pix[2]);
                                    dist = sqrt(dist);
                        if(dist>threshold){
                          mask.at<unsigned char>(j,i) = 255;
                        }

                    }

      Mat res;
      bitwise_and(currentImage, currentImage, res, mask);
      currentImage=res;

以上是关于虽然找到2张图片之间的差异,但OpenCV差异大于预期的主要内容,如果未能解决你的问题,请参考以下文章

Python 比较俩张图片差异

使用 OpenCV 的视角和光照条件略有不同的两张图像之间的差异

如何使用 Homography 在 OpenCV 中转换图片?

如何在 Matlab 中获得 2 个图像之间的差异?

在OpenCV Python中检测/提取图像之间的最大差异

iOS图像比较算法在两张非常相似但不同的照片中找到差异