2013-04-24 239 views
3

我正在画一条线,我正在逐帧地逐帧浏览,因此我可以计算线的角度。我做了一个非常简单的脚本,通过视频几步之遥,尝试收集到阵列中的每个图像中点击了点,但是连这似乎工作...下面的代码:Python&CV2:如何用鼠标在图像上绘制线条然后返回线条坐标?

import cv2, cv 

cap = cv2.VideoCapture('video.avi') 

box = [] 
def on_mouse(event, x, y, flags): 
    if event == cv.CV_EVENT_LBUTTONDOWN: 
     print 'Mouse Position: '+x+', '+y 
     box.append(x, y) 

#cv2.rectangle(img, pt1, pt2, color) 
#cv2.line(img, pt1, pt2, color) 
drawing_box = False 

cv.SetMouseCallback('real image', on_mouse, 0) 
count = 0 
while(1): 
    _,img = cap.read() 
    img = cv2.blur(img, (3,3)) 

    cv2.namedWindow('real image') 
    cv2.imshow('real image', img) 

    if cv2.waitKey(0) == 27: 
     cv2.destroyAllWindows() 
     break 
print box 

任何帮助感谢!

非常感谢

约翰

+1

有[类似](http://stackoverflow.com/a/10883266/176769)示例](http://stackoverflow.com/a/8196180/176769)铺设[around](http://stackoverflow.com/a/5493633/176769)此论坛。 – karlphillip 2013-04-24 17:04:57

回答

6

这里是我发现的修补程序:

def on_mouse(event, x, y, flags, params): 
    if event == cv.CV_EVENT_LBUTTONDOWN: 
     print 'Start Mouse Position: '+str(x)+', '+str(y) 
     sbox = [x, y] 
     boxes.append(sbox) 
    elif event == cv.CV_EVENT_LBUTTONUP: 
     print 'End Mouse Position: '+str(x)+', '+str(y) 
     ebox = [x, y] 
     boxes.append(ebox) 

count = 0 
while(1): 
    count += 1 
    _,img = cap.read() 
    img = cv2.blur(img, (3,3)) 

    cv2.namedWindow('real image') 
    cv.SetMouseCallback('real image', on_mouse, 0) 
    cv2.imshow('real image', img) 

    if count < 50: 
     if cv2.waitKey(33) == 27: 
      cv2.destroyAllWindows() 
      break 
    elif count >= 50: 
     if cv2.waitKey(0) == 27: 
      cv2.destroyAllWindows() 
      break 
     count = 0 
+0

只是好奇你为什么使用cv.SetMouseCallback而不是cv2?找到以下参考,cv2应该工作正常。 http://docs.opencv.org/trunk/db/d5b/tutorial_py_mouse_handling.html – user391339 2015-05-27 18:05:07