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
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 ].
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.
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 () ...
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?
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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With