【发布时间】:2014-03-04 18:48:46
【问题描述】:
我已经将 opencv 与 python 一起使用了两个星期。我想找到一个移动物体的中心。
我首先发现两帧和按位的区别。如下图所示,按位后,它就像移动物体(四转子)周围的一组白点。
所以我的问题是:在这种情况下,我应该使用什么方法来找到移动物体(四转子)的中心?
我想也许只是找到所有这些点的平均中心,但我不知道该怎么做。
这是示例图片
这是我的代码
import cv2
def diffImg(t0, t1, t2):
d1 = cv2.absdiff(t2, t1)
d2 = cv2.absdiff(t1, t0)
return cv2.bitwise_and(d1, d2)
cam = cv2.VideoCapture('track_outdoor.avi')
winName = "Movement Indicator"
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)
# Read three images first:
t_minus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
while True:
diff_frame= diffImg(t_minus, t, t_plus)
cv2.imshow( 'Original', t_plus )
cv2.imshow( winName, diff_frame )
# Read next image
t_minus = t
t = t_plus
t_plus = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
key = cv2.waitKey(10)
if key == 27:
cv2.destroyWindow(winName)
break
更新 #1
首先感谢苦行僧的回答
我只是尝试应用方法找到质心 它可以工作,但它并不像我需要的那样准确。
所以我想出了另一个主意,如果我改变 diff_frame 的亮度是吗 使用轮廓等方法或其他方法更容易、更准确?
【问题讨论】:
-
你有没有尝试在diff图像中找到这些点的不规则外轮廓?
标签: python opencv image-processing