18

I am loading an image into python e.g.

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

How can i acccess the RGB values of image?

Rory Lester
  • 2,858
  • 11
  • 49
  • 66
  • it does, but if i want to access the image rgb values how could this be done? – Rory Lester Aug 29 '12 at 22:29
  • is it a range from 0x000000 to 0xFFFFFF? you will probably need to convert it to hex to have a recognizable color ... and then take each r,g,b value from that (it might be a range of 0.0-1.0 in which case multiply by 0xFFFFFF (the max RGB Hex) – Joran Beasley Aug 29 '12 at 22:51
  • OpenCV has excellent [tutorials](https://docs.opencv.org/master/d6/d00/tutorial_py_root.html). – bfris Sep 21 '19 at 02:50
  • @RoryLester you can use `cv2.cvtColor(image, cv2.COLOR_BGR2YCbCr)` to convert your image easily, and it's probably more efficient. – Harsh Feb 04 '21 at 09:25

6 Answers6

16

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()

Yamaneko
  • 3,433
  • 2
  • 38
  • 57
  • 2
    `image[x, y, z]` also works, since this is just a numpy array. – Danica Aug 30 '12 at 01:05
  • Thank you. I wanted to ask, i am converting an image from rgb to ycbcr and i am looping though each pixel and changing it accordingly. The process is quite intensive and takes a few seconds even on small 200x200 images. Is there another aproach to this to enhance performance? perhaps you know. – Rory Lester Aug 30 '12 at 12:59
  • I think NumPy can perform matrix multiplications through `dot(a,b)`. Instead of accessing each element, maybe you could use it. – Yamaneko Aug 31 '12 at 00:39
  • 1
    I was having a lot of trouble with this - it turned out the x and y values for my image were reversed. I ended up having to access the pixel values as `image[y][x]`. – Chris Sep 30 '18 at 12:57
  • 1
    @Chris, I've just changed it. Reversing the convention for `x` and `y` was very misleading... Thank you for pointing that out! – Yamaneko Sep 30 '18 at 13:37
9

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]
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Rohit Salunke
  • 1,073
  • 1
  • 10
  • 14
5

This code will print the red , green and blue value of pixel 300, 300:

img1 = cv2.imread('Image.png', cv2.IMREAD_UNCHANGED)
b,g,r = (img1[300, 300])
print (r)
print (g)
print (b)
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
1

It worked for me well :

import cv2 
import numpy as np  
  
cap = cv2.imread('/home/PATH/TO/IMAGE/IMG_0835.jpg')
#You're free to do a resize or not, just for the example
cap = cv2.resize(cap, (340,480))
for x in range (0,340,1):
    for y in range(0,480,1):
        color = cap[y,x]
        print color
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
oukt_oukt
  • 13
  • 7
1

Below works.

import cv2

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

color = image[y, x]

blue = int(color[0])
green = int(color[1])
red = int(color[2])
CHOI
  • 114
  • 1
  • 5
0

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. Like this picture. You are allowed to use mouse to see the RGB of any pixel you want.

Code example:

import cv2

image = cv2.imread("new_image.jpg")
try:
    cv2.imshow("windowName",image)
    cv2.waitKey(0)
except:
    print("No this image")
chilin
  • 420
  • 7
  • 9