I was trying to read red pixel values of an part of the image using opencv

screen = np.array(ImageGrab.grab(bbox=(0,0,1920,1080)))

I was capturing my screen and

health_bar_bgr = screen[970:990, 800:900] 
health_bar = cv.cvtColor(health_bar_bgr, cv.COLOR_BGR2RGB)

and cropped the part I wanted and changed the bgr to rgb value. now I want to find the value of r (in rgb) of the pixels on the cropped area.

b,g,r = (health_bar)
    print (r)

when I tried this I got a error saying

Traceback (most recent call last):
  File "d:/Open_cv/.vscode/speec_recog/import cv2 as cv.py", line 37, in <module>
    b,g,r = (health_bar)
ValueError: too many values to unpack (expected 3)

sorry if this a easy question I am new to opencv

# if it is BGR
b = health_bar[:, :, 0]
g = health_bar[:, :, 1]
r = health_bar[:, :, 2]

ImageGrab (PIL/pillow) returns RGB order data, that you’re (needlessly) converting to BGR for OpenCV’s consumption.

spare the cvtColor, directly get the red channel:

screen_rgb = np.array(ImageGrab.grab(bbox=(0,0,1920,1080))) # RGB order
health_bar_rgb = screen_rgb[970:990, 800:900]
r = health_bar_rgb[:, :, 0]

if you need to show anything using cv.imshow, then yes, cvtColor with cv.COLOR_RGB2BGR makes sense to convert the data you received from ImageGrab.

1 Like

thanks it working fine