opencv-python使用cvui编写界面

安装cvui

pip install cvui

说明文档中用到指针的地方,使用列表代替(如low_threshold = [5]),取值时为low_threshold[0]

import cv2
import numpy as np
import cvui

#initial cvui
WINDOW_NAME = 'CVUI Test'
cvui.init(WINDOW_NAME)

#capture one frame
low_threshold = [5]
method = cv2.THRESH_BINARY

cap = cv2.VideoCapture(0)
ret, frame = cap.read()

#image size
img_h, img_w = frame.shape[0:2]

#ui size
ui_h=80
ui_w=200

# total size
total_h = img_h+ui_h
total_w = max(ui_w, img_w)

#initial faram
total_frame = np.zeros((total_h,  total_w, 3), np.uint8)
total_frame[:]=(150,150,150)

while(1):
    #process image
    ret, frame = cap.read()
    imgGray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    th1, imgGray = cv2.threshold(imgGray, low_threshold[0], 255, method);
    frame = cv2.cvtColor(imgGray,cv2.COLOR_GRAY2BGR)
    total_frame[0:img_h, 0:img_w] = frame

    #draw ui
    cvui.update()
    cvui.text(total_frame, 10, img_h+15, 'Hello world!')     
    if cvui.button(total_frame, 100, img_h+15, "Inverse"):
        method = cv2.THRESH_BINARY if method == cv2.THRESH_BINARY_INV else cv2.THRESH_BINARY_INV

    cvui.trackbar(total_frame, 180, img_h+10, 220, low_threshold, 5, 150)

    #show
    cvui.imshow(WINDOW_NAME, total_frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):        
        break
cap.release()
cv2.destroyAllWindows()

猜你喜欢

转载自blog.csdn.net/zouxin_88/article/details/122586954