Israel Zinc
Israel Zinc

Reputation: 2769

Cannot get correct pixel color image python

I am using OpenCV 3 on MacOS and all I am trying to get the RGB values of pixels in an image.

For example, I am using the following image:

enter image description here

I've made a program that detects the click of the user in the image and outputs the RGB color using the x,y coordinates of the click, but apparently when I was clicking in some regions of the circles, I was getting an incorrect value. For example: When I click inside the blue circle, let's say on the point (177,340) of the image, it outputs the value (255,255,255), which is definitely incorrect.

But when I click a bit to the right or left, it outputs the correct value: (17, 51, 225). This happens for the whole image,

I was suspecting it was something related to the scale, and then I just opened the image on python and got the value of the pixel using the following code:

import cv2
import numpy as np

img = cv2.imread('circles.jpg', 1)
print(img[177,340])

But still, what I get is this:

array([255, 255, 255], dtype=uint8)

I suspect it's something related to the coordinates system that I am not aware of. Can someone give me a hand about it?

Thanks in advance.

Upvotes: 0

Views: 964

Answers (1)

thewaywewere
thewaywewere

Reputation: 8626

I drawed the point [177,340] in magenta (255,0,255) to your image as the code below.

image[175:179,338:342] = (255,0,255) # enlarge a bit for easy viewing

It's clearly showed in the image that the point is located in the white background.

enter image description here

Likely, you coded the (row,col) incorrectly as (col,row) so that you get color [17,51,225] which is the "orange and biggest circle" in your image instead of [244,70,18] which is the "blue circle".

Upvotes: 2

Related Questions