Python OpenCV select 播放时 video/stream 的投资回报率
Python OpenCV select ROI on video/stream while its playing
感谢是否有人可以在播放视频流时帮助 select 获得 ROI(不希望它暂停或捕获第一帧)
我是不是遗漏了什么,我试过将框架设置为同名
cv2.selectROI('Frame', frame, False)
cv2.imshow('Frame',frame)
谢谢
在那种情况下你不能使用 cv2.selectROI()
因为函数是阻塞的,即它会停止你的程序的执行,直到你选择了你感兴趣的区域(或取消它)。
要实现您想要的效果,您需要自己处理 ROI 的选择。这是一个简短的示例,说明如何执行此操作,使用两次左键单击来定义 ROI,然后单击右键将其擦除。
import cv2, sys
cap = cv2.VideoCapture(sys.argv[1])
cv2.namedWindow('Frame', cv2.WINDOW_NORMAL)
# Our ROI, defined by two points
p1, p2 = None, None
state = 0
# Called every time a mouse event happen
def on_mouse(event, x, y, flags, userdata):
global state, p1, p2
# Left click
if event == cv2.EVENT_LBUTTONUP:
# Select first point
if state == 0:
p1 = (x,y)
state += 1
# Select second point
elif state == 1:
p2 = (x,y)
state += 1
# Right click (erase current ROI)
if event == cv2.EVENT_RBUTTONUP:
p1, p2 = None, None
state = 0
# Register the mouse callback
cv2.setMouseCallback('Frame', on_mouse)
while cap.isOpened():
val, frame = cap.read()
# If a ROI is selected, draw it
if state > 1:
cv2.rectangle(frame, p1, p2, (255, 0, 0), 10)
# Show image
cv2.imshow('Frame', frame)
# Let OpenCV manage window events
key = cv2.waitKey(50)
# If ESCAPE key pressed, stop
if key == 27:
cap.release()
感谢是否有人可以在播放视频流时帮助 select 获得 ROI(不希望它暂停或捕获第一帧) 我是不是遗漏了什么,我试过将框架设置为同名
cv2.selectROI('Frame', frame, False)
cv2.imshow('Frame',frame)
谢谢
在那种情况下你不能使用 cv2.selectROI()
因为函数是阻塞的,即它会停止你的程序的执行,直到你选择了你感兴趣的区域(或取消它)。
要实现您想要的效果,您需要自己处理 ROI 的选择。这是一个简短的示例,说明如何执行此操作,使用两次左键单击来定义 ROI,然后单击右键将其擦除。
import cv2, sys
cap = cv2.VideoCapture(sys.argv[1])
cv2.namedWindow('Frame', cv2.WINDOW_NORMAL)
# Our ROI, defined by two points
p1, p2 = None, None
state = 0
# Called every time a mouse event happen
def on_mouse(event, x, y, flags, userdata):
global state, p1, p2
# Left click
if event == cv2.EVENT_LBUTTONUP:
# Select first point
if state == 0:
p1 = (x,y)
state += 1
# Select second point
elif state == 1:
p2 = (x,y)
state += 1
# Right click (erase current ROI)
if event == cv2.EVENT_RBUTTONUP:
p1, p2 = None, None
state = 0
# Register the mouse callback
cv2.setMouseCallback('Frame', on_mouse)
while cap.isOpened():
val, frame = cap.read()
# If a ROI is selected, draw it
if state > 1:
cv2.rectangle(frame, p1, p2, (255, 0, 0), 10)
# Show image
cv2.imshow('Frame', frame)
# Let OpenCV manage window events
key = cv2.waitKey(50)
# If ESCAPE key pressed, stop
if key == 27:
cap.release()