【发布时间】: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