Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge HSV channels under OpenCV 3 in Python

Tags:

python

opencv

I'm trying to split a HSV image into its channels, change them and them merge them back, however once I run the merge function the channels are interpreted as RBG channels, so for example for the following example I get a yellow image,

import cv2

image = cv2.imread('example.jpg')
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv_image)
s.fill(255)
v.fill(255)
hsv_image = cv2.merge([h, s, v])

cv2.imshow('example', hsv_image)
cv2.waitKey()

What is the proper way to do this in OpenCV 3 using Python?

like image 687
Gonçalo Cabrita Avatar asked Jan 10 '16 23:01

Gonçalo Cabrita


People also ask

What does OpenCV merge do?

cv2. merge() is used to merge several single-channel images into a colored/multi-channel image.

What is HSV in OpenCV?

HSV color space: It stores color information in a cylindrical representation of RGB color points. It attempts to depict the colors as perceived by the human eye. Hue value varies from 0-179, Saturation value varies from 0-255 and Value value varies from 0-255. It is mostly used for color segmentation purpose.

How do I separate RGB channels in OpenCV?

In OpenCV, BGR sequence is used instead of RGB. This means the first channel is blue, the second channel is green, and the third channel is red. To split an RGB image into different channels, we need to define a matrix of 3 channels. We use 'Mat different_Channels[3]' to define a three-channel matrix.


1 Answers

Images are always displayed as they are BGR. If you want to show your HSV image, you need to convert to BGR first:

import cv2

image = cv2.imread('example.jpg')
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv_image)
s.fill(255)
v.fill(255)
hsv_image = cv2.merge([h, s, v])

out = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR)

cv2.imshow('example', out)
cv2.waitKey()
like image 187
Miki Avatar answered Sep 23 '22 06:09

Miki