公差变量中两张照片之间的差异

Posted

技术标签:

【中文标题】公差变量中两张照片之间的差异【英文标题】:Difference between two photos in tollerance variable 【发布时间】:2016-12-13 13:47:04 【问题描述】:

我有两张照片: 和

我发现这些照片之间存在差异。但是这些差异包括光线的变化,相机的抖动等。我只想看到差异照片中的那个人。我写了一个阈值,我成功了。但是这个阈值并不能纠正其他照片。由于我在 *** 中的声誉,我无法展示错误的示例。你可以在其他照片上运行我的代码,你可以看到这些障碍。我的代码如下。我还能怎么做这个阈值?

 #include <Windows.h>
 #include <opencv\highgui.h>
 #include <iostream>
 #include <opencv2\opencv.hpp>
 using namespace cv;
using namespace std;
int main() 

Mat siyah;
Mat resim = imread("C:/Users/toshiba/Desktop/z.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Mat resim2 = imread("C:/Users/toshiba/Desktop/t.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if (resim.empty() || resim2.empty())

    cout << "Dosya Açılamadı " << "\n";
    return 0;

for (int i = 0; i < resim.rows; i++)

    for (int j = 0; j <resim.cols; j++)
    
        if (resim.data[resim.channels()*(resim.cols*(i)+
            (j))] - resim2.data[resim2.channels()*(resim2.cols*(i)+
                (j))]>30) 
            resim.data[resim.channels()*(resim.cols*(i)+
                (j))] = 255;

        
        else
            resim.data[resim.channels()*(resim.cols*(i)+
                (j))] = 0;


        //inRange(resim, 150, 255, siyah);
    

//inRange(resim, 150, 255, siyah);
namedWindow("Resim", CV_WINDOW_NORMAL);
imshow("Resim", resim);
waitKey();

system("PAUSE");
waitKey();
return 0;

【问题讨论】:

您的问题没有简单的答案。图像处理中几乎没有任务可以通过对所有图像应用相同的全局阈值来解决。在不知道图像范围和必须处理的噪音的情况下,不可能给您太多建议。作为初学者,如果您不处理压缩图像,它将有很大帮助。那么你有更多的信息可以使用。 【参考方案1】:

如果您的背景始终相同,并且包含对象的图片很少出现,那么您可以经常更新参考图像,以便从参考图像到要分析的图像的照明变化总是很小。然后,您可以测量/计算一个阈值,该阈值在计算图像差异时大部分时间都会起作用。我不确定你的相机为什么会移动 - 它不是固定的吗?

【讨论】:

【参考方案2】:

我使用Otsu thresholding 和GrabCut algorithm 编写了以下代码。它不使用任何预设的阈值,但我仍然不确定它对其他图像的性能如何(也许如果您提供更多图片进行测试)。代码在 Python 中,但它主要包括调用 OpenCV 函数和填充矩阵,因此应该很容易转换为 C++ 或其他。您的图像的结果:

单独使用 Otsu 得到以下掩码: 腿还好,其他都乱了。但似乎没有误报,所以我将蒙版作为确定的前景,将其他所有内容作为可能的背景并将其提供给 GrabCut。

import cv2
import numpy as np

#read the empty background image and the image with the guy in it,
#convert them into float32, so we don't get integers overflow
img_empty = cv2.imread("000_empty.png", 0).astype(np.float32)
img_guy = cv2.imread("001_guy.jpg", 0).astype(np.float32)

#absolute difference -> back to uint8 for thresholding etc.
diff = np.abs(img_empty - img_guy).astype(np.uint8)

#otsu thresholding
ret2, th = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

#read our image again for GrabCut
img = cv2.imread("001_guy.jpg")

#fill GrabCut mask
mask = np.zeros(th.shape, np.uint8)
mask[th == 255] = cv2.GC_FGD #the value is GC_FGD (foreground) when our thresholded value is 255
mask[th == 0] = cv2.GC_PR_BGD #GC_PR_BGD (probable background) otherwise

#some internal stuff for GrabCut...
bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)

#run GrabCut
cv2.grabCut(img, mask, (0, 0, 1365, 767), bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_MASK)

#convert the `mask` we got from GrabCut into a binary mask,
#then apply it to the original image
mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
img = img*mask2[:,:,np.newaxis]

#save the results
cv2.imwrite("003_grabcut.jpg", img)

【讨论】:

以上是关于公差变量中两张照片之间的差异的主要内容,如果未能解决你的问题,请参考以下文章

数据库中两张表之间的数据同步实现思路(增加删除更新)Mysqlsqlserver

计算Postgresql中两列之间的运行差异

如何获取同一列中两行之间的差异

如何计算Python Pandas中两列之间的日期差异[重复]

MySQL Workbench中两行之间的差异,但未授权LAG

使用python返回excel中两个不同文件中两列之间的差异