0

I have this code that open camera show video.

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()

as you can see in code above I use cv2.imshow function to show video.

I need to create button to capture video.

My question is it possible to create button inside window that created by cv2.imshow function?

Michael
  • 13,950
  • 57
  • 145
  • 288
  • check this https://stackoverflow.com/questions/21523398/how-to-give-start-stop-capture-close-buttons-in-opencv-cam-window-in-python – Karthick Aravindan Apr 30 '19 at 10:51
  • Possible duplicate of [How to give Start,stop,capture,Close buttons in Opencv Cam window in python](https://stackoverflow.com/questions/21523398/how-to-give-start-stop-capture-close-buttons-in-opencv-cam-window-in-python) – Karthick Aravindan Apr 30 '19 at 10:51
  • you cannot add gui feature to cv2, so either bind your keys to do stuff as suggested by @KarthickAravindan , or use some sort of gui engine, such as tkinter,[here](https://stackoverflow.com/questions/32342935/using-opencv-with-tkinter) is exactly how to do cv2 with tkinter – Nullman Apr 30 '19 at 11:00
  • *"possible to create button inside window"*: Yes using `tkinter window` with `.wm_attributes("-topmost", True)` – stovfl Apr 30 '19 at 14:01

1 Answers1

4

The comments provide good options, keybindings and/or tkinter. If you do want a visual interface, but do not want to use tkinter, you can use one of these 'tricks':

  • OpenCV does support sliders, so you can use those with a range of 1 to build an on/off interface.

  • Display an image of a button in a separate window. Add a mousecallback, and check if mouse clicks are within the dimensions of the 'button'.

You can even combine the two in a single control panel:
enter image description here

Code:

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()
J.D.
  • 4,511
  • 2
  • 7
  • 20
  • It's a commonly used workaround, because openCV lacks GUI features such as buttons. By using a trackbar with only values 0 and 1, you have create a sort of switch. It's not optimal, but as I said, it's a workaround. – J.D. Apr 30 '19 at 21:26
  • 1
    You can use either the trackbar or the button, I included both to show the code for both options – J.D. May 01 '19 at 11:01
  • Both, `Button` and `Trackbar`, are independent , thanks for clarification. – stovfl May 01 '19 at 11:17