gpt4 book ai didi

python & CV2 : How do i draw a line on an image with mouse then return line coordinates?

转载 作者:太空宇宙 更新时间:2023-11-03 13:47:40 34 4
gpt4 key购买 nike

我想在逐帧浏览的视频上画一条线,以便计算线的角度。我已经制作了一个非常简单的脚本,它逐步浏览视频并尝试收集数组中每个图像中单击的点,但是即使这样似乎也行不通......这是代码:

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

感谢任何帮助!

非常感谢

约翰

最佳答案

这是我找到的修复方法:

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

关于 python & CV2 : How do i draw a line on an image with mouse then return line coordinates?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16195190/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com