Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of all colors in an image using opencv and python

Python beginner here. I'm using python 3.6 and opencv and I'm trying to create a list of rgb values of all colors present in an image. I can read the rgb values of one pixel using cv2.imread followed by image[px,px]. The result is a numpy array. What I want is list of rgb tuples of unique colors present in an image and am not sure how to do that. Any help is appreciated. TIA

like image 417
Kalyan M Avatar asked Aug 06 '18 10:08

Kalyan M


People also ask

How many color possibilities are there for each pixel in an RGB image in OpenCV?

RGB Image : Pixel intensities in this color space are represented by values ranging from 0 to 255 for single channel. Thus, number of possibilities for one color represented by a pixel is 16 million approximately [255 x 255 x 255 ].

What color code does OpenCV use?

The reason the early developers at OpenCV chose BGR color format is that back then BGR color format was popular among camera manufacturers and software providers.

How to detect color in Python using OpenCV?

Detect color in Python using OpenCV 1) Detection of colors in saved images: Import the OpenCV and NumPy libraries so that you can use their parameters... 2. Read the image by providing a proper path else save the image in the working directory and just give the name of an... 3. Use cv2.cvtColor () ...

Is OpenCV supported by any system?

Not only supported by any system, such as Windows, Linux, Mac, etc. but also it can be run in any programming language like Python, C++, Java, etc. OpenCV also allows you to identify color in images. Don’t you know how to find these colors in images?

What is RGB RGB value in OpenCV?

RGB values range from 0 to 255 for each channel. The red colour is (255, 0, 0) -- in other words, full red value, no green, and no blue. Rebecca Stone thanks for reply. How can i get the rgb color values from inside of a contour in opencv?

How many flags do I have available in OpenCV?

The list and number of flags may vary slightly depending on your version of OpenCV, but regardless, there will be a lot! See how many flags you have available: The first characters after COLOR_ indicate the origin color space, and the characters after the 2 are the target color space.


1 Answers

Check out numpy's numpy.unique() function:

import numpy as np

test = np.array([[255,233,200], [23,66,122], [0,0,123], [233,200,255], [23,66,122]])
print(np.unique(test, axis=0, return_counts = True))

>>> (array([[  0,   0, 123],
       [ 23,  66, 122],
       [233, 200, 255],
       [255, 233, 200]]), array([1, 2, 1, 1]))

You can collect the RGB numpy arrays in a 2D numpy array and then use the numpy.unique() funtion with the axis=0 parameter to traverse the arrays within. Optional parameter return_counts=True will also give you the number of occurences.

like image 77
iR0Nic Avatar answered Oct 19 '22 07:10

iR0Nic