Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get RGB value opencv python

I am loading an image into python e.g.

image = cv2.imread("new_image.jpg")

How can i acccess the RGB values of image?

like image 756
Rory Lester Avatar asked Aug 29 '12 22:08

Rory Lester


People also ask

How do I get the RGB value of a pixel in Python OpenCV?

I think the most easiest way to get RGB of an image is use cv2. imshow("windowName",image) . The image would display with window, and the little information bar also display coordinate (x,y) and RGB below image.

How do I extract the RGB values from an image?

Click on the color selector icon (the eyedropper), and then click on the color of in- terest to select it, then click on 'edit color'. 3. The RGB values for that color will appear in a dialogue box.

How do I find the average image RGB value in Python?

Use the average() Function of NumPy to Find the Average Color of Images in Python. In mathematics, we can find the average of a vector by dividing the sum of all the elements in the vector by the total number of elements.


2 Answers

You can do

image[y, x, c]

or equivalently image[y][x][c].

and it will return the value of the pixel in the x,y,c coordinates. Notice that indexing begins at 0. So, if you want to access the third BGR (note: not RGB) component, you must do image[y, x, 2] where y and x are the line and column desired.

Also, you can get the methods available in Python for a given object by typing dir(<variable>). For example, after loading image, run dir(image) and you will get some usefull commands:

'cumprod', 'cumsum', 'data', 'diagonal', 'dot', 'dtype', 'dump', 'dumps', 'fill',
'flags', 'flat', 'flatten', 'getfield', 'imag', 'item', 'itemset', 'itemsize', 
'max', 'mean', 'min', ...

Usage: image.mean()

like image 104
Yamaneko Avatar answered Sep 21 '22 12:09

Yamaneko


Get B G R color value of pixel in Python using opencv

import cv2
image = cv2.imread("sample.jpg")
color = int(image[300, 300])
# if image type is b g r, then b g r value will be displayed.
# if image is gray then color intensity will be displayed.
print color

output: [ 73 89 102]

like image 28
Rohit Salunke Avatar answered Sep 19 '22 12:09

Rohit Salunke