如果有人可以帮助我在视频流播放时选择 ROI(我不希望它暂停或捕获第一帧),我将不胜感激。我错过了什么吗?我尝试将框架设置为相同的名称。
cv2.selectROI('Frame', frame, False)
cv2.imshow('Frame',frame)
Run Code Online (Sandbox Code Playgroud)
小智 7
在这种情况下您不能使用cv2.selectROI(),因为该函数是阻塞的,即它会停止程序的执行,直到您选择了感兴趣的区域(或取消它)。
为了实现您想要的目标,您需要自己处理投资回报率的选择。下面是一个简短的示例,说明如何执行此操作,使用两次左键单击来定义 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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3994 次 |
| 最近记录: |