我有打开相机显示视频的这段代码。
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
如您在上面的代码中所见,我使用cv2.imshow函数显示视频。
我需要创建一个按钮来捕获视频。
我的问题是可以在cv2.imshow函数创建的窗口内创建按钮吗?
答案 0 :(得分:0)
注释提供了不错的选择,键绑定和/或tkinter。如果您确实想要视觉界面,但不想使用tkinter,则可以使用以下“技巧”之一:
OpenCV确实支持滑块,因此您可以使用范围为1的滑块来构建开/关界面。
在单独的窗口中显示按钮的图像。添加鼠标回调,并检查鼠标单击是否在“按钮”的尺寸之内。
代码:
import cv2
import numpy as np
# button dimensions (y1,y2,x1,x2)
button = [20,60,50,250]
# function that handles the mousclicks
def process_click(event, x, y,flags, params):
# check if the click is within the dimensions of the button
if event == cv2.EVENT_LBUTTONDOWN:
if y > button[0] and y < button[1] and x > button[2] and x < button[3]:
print('Clicked on Button!')
# function that handles the trackbar
def startCapture(val):
# check if the value of the slider
if val == 1:
print('Capture started!')
else:
print('Capture stopped!')
# create a window and attach a mousecallback and a trackbar
cv2.namedWindow('Control')
cv2.setMouseCallback('Control',process_click)
cv2.createTrackbar("Capture", 'Control', 0,1, startCapture)
# create button image
control_image = np.zeros((80,300), np.uint8)
control_image[button[0]:button[1],button[2]:button[3]] = 180
cv2.putText(control_image, 'Button',(100,50),cv2.FONT_HERSHEY_PLAIN, 2,(0),3)
#show 'control panel'
cv2.imshow('Control', control_image)
cv2.waitKey(0)
cv2.destroyAllWindows()