我面临着一个挑战,我得到了一张照片,我需要找到这张照片中的一个物体和我面前的一个物体之间的区别,我将使用 python 通过 IP 摄像机看到。 这是最初递给我的照片
这是来自 IP 摄像机的提要的样子
现在我应该编写一个程序来检测两个对象之间的差异和每个不同部分的颜色代码。如果它只包含这 2 张图片,这一切都很容易,但后者应该是来自相机的实时馈送,因此我编写的程序会因相机位置不同或曝光不同而导致差异不堪重负。
#!/usr/bin/env python
import cv2
from skimage import measure
import imutils
cap = cv2.VideoCapture(0)
img_gray = cv2.imread("pic1.png", 0)
img = cv2.imread("pic1.png")
while True:
_, frame = cap.read()
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
(score, diff) = measure.compare_ssim(img_gray, frame_gray, full=True)#we won't use the score
diff = (diff * 255).astype("uint8") # converting the float score from a (-1,1) range to 8 bit 0-255 range
thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(contours)
for cnt in contours:
area = cv2.contourArea(cnt)
if 3150 < area:
(x,y,w,h) = cv2.boundingRect(cnt)
cv2.rectangle(frame, (x,y), (x+w, y+h), (0,255,0))
cv2.imshow("Current", frame)
cv2.imshow("Back then", img)
cv2.imshow("mask", thresh)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
现在彩色视频源上的输出应该是这样的
颜色编码不是问题
我尝试给绘制的轮廓一个设定的区域来绘制,显然它没有工作,ssim分数计算也没有。我应该如何解决这个问题,因为感觉就像我正在尝试重新发明已经构建的东西,但经过 2 周的尝试后我找不到。