【问题标题】:How to make image comparison in openCV more coarse如何使openCV中的图像比较更粗略
【发布时间】:2017-05-14 22:55:26
【问题描述】:

我正在用 python 在树莓派上编写代码,以使用均方误差比较两个图像。该项目是个人家庭安全的事情。

我的主要目标是检测我从 pi 相机捕获的图像之间的变化(如果将某些内容添加到当前图像或从图像中删除某些内容),但现在我的代码太敏感了。它受背景照明变化的影响,这是我不想要的。

我面前有两个选择,要么删除我当前的逻辑并开始一个新的逻辑,要么改进我当前的逻辑以解决这些噪音(如果我可以这样称呼他们的话)。我正在寻找改进逻辑的方法,但我想要一些关于如何去做的指导。

我最大的恐惧是,我是在浪费时间踢死马,还是应该寻找其他算法来检测图像的变化,还是应该使用边缘检测

    import numpy as np
import cv2
import os
from threading import Thread
######Function Definition########################################
def mse(imageA, imageB):
    # the 'Mean Squared Error' between the two images is the
    # sum of the squared difference between the two images;
    # NOTE: the two images must have the same dimension
    err = np.sum((imageA.astype("int") - imageB.astype("int")) ** 2)
    err /= int(imageA.shape[0] * imageA.shape[1])

    # return the MSE, the lower the error, the more "similar"
    # the two images are
    return err

def compare_images(imageA, imageB):
    # compute the mean squared error
    m = mse(imageA, imageB)
    print(m)


def capture_image():
        ##shell command to click photos
        os.system(image_args)

##original image Path variable
original_image_path= "/home/pi/Downloads/python-compare-two-images/originalimage.png"

##original_image_args is a shell command to click photos 
original_image_args="raspistill -o "+original_image_path+" -w 320 -h 240 -q 50 -t 500"
os.system(original_image_args)

##read the greyscale of the image in to the variable original_image
original_image=cv2.imread(original_image_path, 0)

##Three images
image_args="raspistill -o /home/pi/Downloads/python-compare-two-images/Test_Images/image.png -w 320 -h 240 -q 50 --nopreview -t 10 --exposure sports"
image_path="/home/pi/Downloads/python-compare-two-images/Test_Images/"
image1_name="image.png"

#created a new thread to take pictures
My_Thread=Thread(target=capture_image)

#Thread started
My_Thread.start()

flag = 0
while(True):
        if(My_Thread.isAlive()==True):
                flag=0
        else:
                flag=1
        if(flag==1):
                flag=0
                image1 = cv2.imread((image_path+image1_name), 0)
                My_Thread=Thread(target=capture_image)
                My_Thread.start()
                compare_images(original_image, image1)

【问题讨论】:

    标签: python-2.7 opencv image-processing raspberry-pi


    【解决方案1】:

    第一个改进是调整增益以补偿光的全局变化。就像取两张图像的平均强度,然后用强度的比率校正一张。

    如果前景发生变化,这可能会失败,这将影响全局平均值。如果前景中的变化没有太大的区域,您可以通过线性模型y = a.x 的稳健拟合得到估计。

    一个更糟糕但不幸的是常见的情况是背景照明以不均匀的方式变化。部分解决方案是尝试拟合非均匀增益模型,例如通过在拐角处估计的增益之间的双线性插值获得的模型,或者对图像进行更精细的细分。

    【讨论】:

    • 所以在这种情况下的增益将是m函数compare_image()
    • @ShravanSingh:一点也不。将是均值的比率。
    【解决方案2】:

    变化检测的主题是一个非常研究的领域。基本选项之一是通过为每个像素采样大量图像并计算每个像素的均值和方差,将每个像素建模为高斯分布。

    对于在光照变化时趋于变化的像素,像素的方差将大于变化不大的像素。

    为了检测某个像素的移动,您只需选择您认为像素值发生异常变化的概率是多少,并使用您计算的高斯分布来找出被认为是异常的对应值。

    要使此解决方案对您的树莓派有效,您需要首先对每个像素的值进行“离线”计算,该值将作为阈值,像素值的变化被视为移动并将它们存储在一个文件,而不是在“在线”圣人中,您只需将每个像素与计算值进行比较。

    对于“离线”阶段,我建议使用全天记录的图像,以获得每个像素所需的所有变化。这个阶段的诅咒可以在你的电脑上完成,只有输出文件会上传到树莓派

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-29
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      • 2016-09-19
      • 2011-06-18
      • 1970-01-01
      相关资源
      最近更新 更多