-1

I would like to read rgb or hsv values of a some image. I looked it up how to read it and I found a answer.

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

link: Get RGB value opencv python

but that code gave me an error like this: TypeError: only size-1 arrays can be converted to Python scalars

how can I fix this error? thank you.

Jeongmin
  • 57
  • 2
  • 8
  • Possible duplicate of [How can I read the RGB value of a given pixel in Python?](https://stackoverflow.com/questions/138250/how-can-i-read-the-rgb-value-of-a-given-pixel-in-python) – Nouman Sep 16 '18 at 09:59
  • For RGB images... The error message explains itself. The `image[300, 300]` is a ``. Not an integer. You can try changing the `color = int(image[300, 300])` to `color = image[300, 300]` and then try printing the `color` directly or printing `type(color)`. Your code might work for grayscale imaged (not tested this myself). HTH – Krishna Sep 16 '18 at 10:11
  • Add `import numpy as np` at the top, then add `color = np.array(image)` at the bottom. – Mark Setchell Sep 16 '18 at 10:19

1 Answers1

0

The code you wrote is almost right except for the third line Fixing removing the int function will result in the result you are looking for.

Code example

import cv2
image = cv2.imread("sample.jpg")
color = image[300,300]
print(color)

Result:

[ 95  92 101]

Here i got 3 number each representing the RGB space.

yapws87
  • 1,809
  • 7
  • 16