In OpenCV, can you display the x, y and rgb values?

Question:

While following OpenCV tutorials, I noticed others have a display showing x and y coordinates like the screenshot below. Is this toolbar a feature of OpenCV? Is there a good way to implement a similar tooling that shows the current x, y and RGB of what has the mouse over?

OpenCV toolbar

Below is my current code snippet

import time

import cv2

def rescale_frame(frame, percent=75):
    width = int(frame.shape[2] * percent/ 100)
    height = int(frame.shape[0] * percent/ 100)
    dim = (width, height)
    return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)

vid = cv2.VideoCapture(0)
vid.set(cv2.CAP_PROP_FRAME_WIDTH, 160)
vid.set(cv2.CAP_PROP_FRAME_HEIGHT, 120)

vid.read()
time.sleep(1)

while True:
    # capture the video frame by frame
    ret, frame = vid.read()

    # display the resulting frame
    frame75 = rescale_frame(frame, percent=75)
    cv2.imshow('frame75', frame75)
    # cv2.imshow('frame', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

vid.release()
Asked By: Ben

||

Answers:

Yes, you can use OpenCV’s mouse callback.

Here I’ve added the callback to print the BGR value of the pixel clicked on along with its coordinates into the named window frame75

The callback is initallized with,

cv2.namedWindow('frame75')
cv2.setMouseCallback('frame75',mouse_pos_BGR)

And the callback function is,

def mouse_pos_BGR(event, x, y, flags, param):
    
    if event == cv2.EVENT_LBUTTONDOWN: # Left mouse click
        
        B = frame[y,x,0]
        G = frame[y,x,1]
        R = frame[y,x,2]
       
        print("Blue: ", B)
        print("Green: ", G)
        print("Red: ", R)        

        print("Coords x: ", x," y: ", y)

Full code listing,

import cv2
import time

def rescale_frame(frame, percent=75):
    
    width  = int(frame.shape[2] * percent/ 100)
    height = int(frame.shape[0] * percent/ 100)
    dim    = (width, height)
    
    return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)


def mouse_pos_BGR(event, x, y, flags, param):
    
    if event == cv2.EVENT_LBUTTONDOWN: # Left mouse click
        
        B = frame[y,x,0]
        G = frame[y,x,1]
        R = frame[y,x,2]
       
        print("Blue: ", B)
        print("Green: ", G)
        print("Red: ", R)        

        print("Coords x: ", x," y: ", y)


cv2.namedWindow('frame75')
cv2.setMouseCallback('frame75',mouse_pos_BGR)


vid = cv2.VideoCapture(0)
vid.set(cv2.CAP_PROP_FRAME_WIDTH, 160)
vid.set(cv2.CAP_PROP_FRAME_HEIGHT, 120)

vid.read()
time.sleep(1)

while True:
    # capture the video frame by frame
    ret, frame = vid.read()

    # display the resulting frame
    frame75 = rescale_frame(frame, percent=75)
    cv2.imshow('frame75', frame75)


    if cv2.waitKey(1) == 27:
        break

cv2.destroyAllWindows()
Answered By: DrBwts
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.