0

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.

3
  • 1
    Note that X starts from left and Y from top on OpenCV. Commented May 29, 2017 at 3:35
  • 3
    You are accessing RGB values as img[row, col], row goes top to bottom, col goes left to right. However, when you register a mouse click on the image, it's an (x, y) point, x goes left to right, y goes top to bottom. Exchange either row and col or x and y, should fix things. Commented May 29, 2017 at 5:52
  • 3
    If you are actually using solid graphic shapes (as opposed to real life photographs) in your application, definitely consider using PNG or GIF format rather than JPEG which introduces all sorts of problems by being lossy. Commented May 29, 2017 at 9:06

1 Answer 1

2

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".

Sign up to request clarification or add additional context in comments.

1 Comment

That was it, I was changing the row by the columns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.