3

I'm looking to draw a line on a video which I'm stepping through frame by frame so I can calculate the angle of the line. I've made a very simple script which steps through the video and tries to collect the points clicked in each image in an array, however not even this seems to work... Here's the code:

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

Any help is appreciated!

Many Thanks

John

John Gannaway
  • 125
  • 1
  • 9
  • 1
    There are [similar](http://stackoverflow.com/a/10883266/176769) [examples](http://stackoverflow.com/a/8196180/176769) laying [around](http://stackoverflow.com/a/5493633/176769) this forum. – karlphillip Apr 24 '13 at 17:04

1 Answers1

6

Here is the fix that I found:

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
John Gannaway
  • 125
  • 1
  • 9
  • Just curious why are you using cv.SetMouseCallback and not cv2? Found following reference, cv2 should work fine. http://docs.opencv.org/trunk/db/d5b/tutorial_py_mouse_handling.html – user391339 May 27 '15 at 18:05