Search code examples
pythonopencvstatusbar

Python OpenCV - Add status bar for show mouse position and color RGB


I would like to show the coordinates of the mouse position and the RGB color on the status bar, when loading images or videos with the "imread" command. I'm working on Windows 10 Python 3.7.4 Opencv 4.2.0.34

Example:

import cv2

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    if not ret:
        exit()
    cv2.imshow('example', frame)    
    k = cv2.waitKey(1)
    if k == ord('q'): 
        exit()

cap.release()
cv2.destroyAllWindows()

Solution

  • To have a status-bar to show the values on, you have to build your OpenCV with Qt as mentioned here, now for demonstrating the R, G, B values on top of the image you can do:

    import cv2
    
    """
    Reference:
    https://www.geeksforgeeks.org/displaying-the-coordinates-of-the-points-clicked-on-the-image-using-python-opencv/
    """
    
    # function to display the coordinates of
    # of the points clicked on the image 
    def move_event(event, x, y, flags, params):
        imgk = img.copy()
        # checking for right mouse clicks     
        if event==cv2.EVENT_MOUSEMOVE:
      
            # displaying the coordinates
            # on the Shell
            # print(x, ' ', y)
      
            # displaying the coordinates
            # on the image window
            font = cv2.FONT_HERSHEY_SIMPLEX
            org = (x, y)
            B = imgk[y, x, 0]
            G = imgk[y, x, 1]
            R = imgk[y, x, 2]
    
        cv2.putText(imgk, '(x, y)=({}, {})'.format(x, y), org, font, 1, (255, 255, 255), 1, cv2.LINE_8)
        cv2.putText(imgk, '                  ,R={}'.format(R), org, font, 1, (0, 0, 255), 1, cv2.LINE_8)
        cv2.putText(imgk, '                         ,G={}'.format(G), org, font, 1, (0, 255, 0), 1, cv2.LINE_8)
        cv2.putText(imgk, '                                 ,B={}'.format(B), org, font, 1, (255, 0, 0), 1, cv2.LINE_8)
        cv2.imshow('image', imgk)
    
    # reading the image
    img = cv2.imread('image.jpg')
    
    # displaying the image
    cv2.namedWindow("image", cv2.WINDOW_NORMAL)
    cv2.imshow('image', img)
    
    # setting mouse hadler for the image
    # and calling the click_event() function
    cv2.setMouseCallback('image', move_event)
    
    # wait for a key to be pressed to exit
    cv2.waitKey(0)
    
    # close the window
    cv2.destroyAllWindows()