-3

Please tell how to get rgb value of whole image. I have printed rgb value of each pixel

import cv2
# LOAD AN IMAGE USING 'IMREAD'
img = cv2.imread("tiger.jpg")
# DISPLAYg
cv2.imshow("Tiger", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
print(img.shape)
[x,y,z]=img.shape


count=0
for i in range(1,x-1):
    for j in range(1,y-1):
        print(img[i,j]) 
        count=count+1
        print(count)
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • 1
    Explain `"whole image RGB values". Are you trying to calculate the total Amount/Percentage of Red in the Image (RGB) ? – glory9211 Sep 03 '21 at 09:07
  • 1
    average? median? minimum? maximum? – Christoph Rackwitz Sep 03 '21 at 09:49
  • I am trying to take out a single rgb color for an image. In short I have to add rgb value of all pixels in the image, i have to blend all the colors. Please help. – Eissa Faheem Sep 04 '21 at 14:48
  • 1
    what you have done is obtainable elegantly [here](https://stackoverflow.com/a/25102495/10452700) but what you looking for is [here](https://stackoverflow.com/q/11064786/10452700) alternatively you can use [this](https://www.kite.com/python/answers/how-get-the-rgb-values-of-an-image-using-pil-in-python) – Mario Sep 07 '21 at 07:02
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 07 '21 at 08:15

1 Answers1

1

you already have pixels. when you read image using

img = cv2.imread('tiger.jpg')

img contains pixels of your image, you don't need to use loops to get them. you can check it by just printing it like.

print(img)

#it gives you something like

[[[182 194 166]
  [182 194 166]
  [182 194 166]
  ...
  [255 176 109]
  [255 176 109]
  [255 176 109]]

 [[182 194 166]
  [182 194 166]
  [182 194 166]
  ...
  [255 176 109]
  [255 176 109]
  [255 176 109]]

 [[182 194 166]
  [182 194 166]
  [182 194 166]
  ...
  [255 176 109]
  [255 176 109]
  [255 176 109]]
  ...
  [132 171 210]
  [135 174 213]
  [137 176 215]]]

img is type of numpy array (numpy.ndarray) you can check it by this:

print(type(img))
JunaidAfzal
  • 135
  • 9
  • For this program: import cv2 img = cv2.imread("house.jpg") print(img) print(type(img)) output is None Please clearify. – Eissa Faheem Sep 04 '21 at 14:44
  • If you getting "NoneType" it means image is not opened, maybe its not on that place or maybe name or extensions is incorrect. – JunaidAfzal Sep 05 '21 at 05:43