Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get pixel coordinates if I know color(RGB)?

I use Python, opencv and PIL.

image = cv2.imread('image.jpg')

color = (235, 187, 7)

How can I get pixel coordinates(x, y) if I know pixels color?

like image 760
dr.dia Avatar asked Jul 04 '18 01:07

dr.dia


People also ask

How do you find the pixel value of an image RGB?

Retrieving the pixel contents (ARGB values) of an image −Get the pixel value at every point using the getRGB() method. Instantiate the Color object by passing the pixel value as a parameter. Get the Red, Green, Blue values using the getRed(), getGreen() and getBlue() methods respectively.

How do you find the pixel coordinate?

In terms of coordinates, a pixel can be identified by a pair of integers giving the column number and the row number. For example, the pixel with coordinates (3,5) would lie in column number 3 and row number 5. Conventionally, columns are numbered from left to right, starting with zero.

How do I find the pixel value of a color?

Use the ColorSync Utility calculator to get the color values of a pixel on your screen. In the ColorSync Utility app on your Mac, click Calculator in the toolbar of the ColorSync Utility window. Click the magnifying glass , then move the pointer over an area on the screen that you want to examine.

How many pixels are in a RGB image?

For example, assume the value (intensity) of red, green, and blue can each take on 256 values (0 through 255) for a pixel. An RGB value of (255, 0, 0) would imply a red pixel, an RGB value of (0, 255, 0) would be green, and an RGB value of (0, 0, 255) would be blue.


2 Answers

Here is a numpythonic solution. Numpy library speeds up operations wherever possible.

  • Assuming the color to be: color = (235, 187, 7)

indices = np.where(img == color)

  • I used the numpy.where() method to retrieve a tuple indices of two arrays where the first array contains the x-coordinates of the pixels of color (235, 187, 7) and the second array contains the y-coordinates of those pixels.

Now indices returns something like the following:

(array([ 81,  81,  81, ..., 304, 304, 304], dtype=int64),
 array([317, 317, 317, ..., 520, 520, 520], dtype=int64),
 array([0, 1, 2, ..., 0, 1, 2], dtype=int64))
  • I then used the zip() method to get a list of tuples containing those points.

coordinates = zip(indices[0], indices[1])

  • But if you notice since this is a color image with three channels each coordinate will be repeated thrice. We have to keep only the unique coordinates. This can be accomplished using set() method.

unique_coordinates = list(set(list(coordinates)))

like image 145
Jeru Luke Avatar answered Nov 09 '22 03:11

Jeru Luke


Try something like:

color = (235, 187, 7)
im = Image.open('image.gif')
rgb_im = im.convert('RGB')
for x in range(rgb_im.size()[0]):
    for y in range(rgb_im.size()[1]):
        r, g, b = rgb_im.getpixel((x, y))
        if (r,g,b) == colour:
            print(f"Found {colour} at {x},{y}!")

But getpixel can be slow, so look at using pixel access objects.

Also note that the value returned can depend on the image type. For example, a single value is returned with pix[1, 1] because GIF pixels refer to one of the 256 values in the GIF color palette.

See also this SO post: Python and PIL pixel values different for GIF and JPEG and this PIL Reference page contains more information on the convert() function.

By the way, your code would work just fine for .jpg images.

like image 41
QA Collective Avatar answered Nov 09 '22 03:11

QA Collective