Ask Your Question

read image as HSV vs convert red RBG to HSV?

asked 2017-12-29 01:19:42 -0600

Santhosh1 gravatar image


Why is the final result of both different?

Reading the image as HSV OR converting a color image into HSV


Extracting the pixels using HSV values should give SAME RESULT

Here's the code for reading image as HSV then selecting HSV value

cim = cv2.imread('OI5.jpg',cv2.COLOR_RGB2HSV)
huemask1 = ((cim >np.array([100,0,0])).astype(np.float32)+(cim>np.array([100,0,0])).astype(np.float32)*(-0.5)+0.5)
mask1_intrd = (huemask1.astype(np.uint8))*255

Here's the OUTPUT

image description

Here's the code for reading image as color, convert to HSV the selecting by HSV Value

cim = cv2.imread('OI5.jpg')

hsvim = cv2.cvtColor(cim,cv2.COLOR_RGB2HSV)

huemask1 = ((hsvim >np.array([100,0,0])).astype(np.float32)+(hsvim>np.array([100,0,0])).astype(np.float32)*(-0.5)+0.5)

mask1_intrd = (huemask1.astype(np.uint8))*255

Here's the OUTPUT

image description

edit retag flag offensive close merge delete


Shouldn't the 2nd parameter of cv2.imread() be cv2.BGR2HSV?...

moHe gravatar imagemoHe ( 2017-12-29 01:46:22 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-12-29 02:20:23 -0600

LBerger gravatar image

Please read imread doc and imread modes There is no flag cv2.COLOR_RGB2HSV for imread mode but it could be a value with a binary mask

edit flag offensive delete link more


Even if there is no such flag, we are still getting some value? This type of reading a color image directly as HSV would be much useful for certain conditions. Will bring in more more robustness into OpenCV, I feel. Could these be implemented in the next release?

Santhosh1 gravatar imageSanthosh1 ( 2017-12-29 02:49:23 -0600 )edit

To answer to your question IMHO I think the answer is no. But feel free to write an issue may be opencv people will say yes.

I will try to debug code to understand why you get a result (and there is no exeption)

LBerger gravatar imageLBerger ( 2017-12-29 03:32:04 -0600 )edit

Hi Santhosh, you can still get some value, but the program will view you BGR channels as RGB. As a result, it mix the B and R, and then get a mistaken result.

moHe gravatar imagemoHe ( 2017-12-29 04:33:24 -0600 )edit

@moHe If you look into imread posted by @LBerger you can see the color image is always red as BGR

Santhosh1 gravatar imageSanthosh1 ( 2017-12-29 05:35:28 -0600 )edit

@santhosh when you use COLOR_RGB2HSV as imread flags it means IMREAD_REDUCED_COLOR_4+IMREAD_LOAD_GDAL. If you haven't got GDAL it does not matter no exception is thrown but image size is reduced (scale factor is 4). May be an exception should be thrown when gdal lib is not installed

LBerger gravatar imageLBerger ( 2017-12-29 09:11:40 -0600 )edit

Question Tools

1 follower


Asked: 2017-12-29 01:19:42 -0600

Seen: 5,547 times

Last updated: Dec 29 '17