## Convert HSV to grayscale in OpenCV

hsv to grayscale formula
opencv grayscale
cv2 hsv to gray python
convert hsv to bgr opencv python
convert rgb to hsv opencv
opencv hsv range
hsv range for blue opencv python
hsv color

I'm newbie in OpenCV. I'm learning Segmentation by Watershed algorithm and i have a problem.

I have to convert image color to image grayscale for using Watershed. When I use color space BGR, no problem but with space HSV, i'm not sure that the code belows is correct.

Mat im = imread("./Image/118035.jpg", CV_LOAD_IMAGE_COLOR);

Mat imHSV;
cvtColor(im, imHSV, CV_BGR2HSV);
imshow("HSV", imHSV);

cvtColor(imHSV, imHSV, CV_BGR2GRAY);
imshow("HSV to gray", imHSV);

imshow("BGR", im);
cvtColor(im, im, CV_BGR2GRAY);
imshow("BGR to gray", im);


I think, after convert from BGR to HSV, Hue = Blue, Saturation = Green, Value = Red and I can use the operator BGR2GRAY for convert from HSV to grayscale.

2 images output are different, can I convert HSV to grayscale like that?

//Is it similaire with color space LAB?

The conversion from HSV to gray is not necessary: you already have it. You can just select the V channel as your grayscale image by splitting the HSV image in 3 and taking the 3rd channel:

Mat im = imread("C:/local/opencv248/sources/samples/c/lena.jpg", CV_LOAD_IMAGE_COLOR);

Mat imHSV;
cvtColor(im, imHSV, CV_BGR2HSV);
imshow("HSV", imHSV);

//cvtColor(imHSV, imHSV, CV_BGR2GRAY);
Mat hsv_channels[3];
cv::split( imHSV, hsv_channels );
imshow("HSV to gray", hsv_channels[2]);

imshow("BGR", im);
cvtColor(im, im, CV_BGR2GRAY);
imshow("BGR to gray", im);

waitKey();


OpenCV – Convert HSV image to grayscale with hue or saturation , A lot of interesting operations with OpenCV start by converting to grayscale. As an interesting experiment, you can convert to HSV first, and� OpenCV – Convert HSV image to grayscale with hue or saturation channel A lot of interesting operations with OpenCV start by converting to grayscale. As an interesting experiment, you can convert to HSV first, and display the “grayscale” of one of these channels.

hsv1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2HSV)

h, s, v1 = cv2.split(hsv1)

cv2.imshow("gray-image",v1)


Convert HSV to grayscale in OpenCV - opencv - html, I have to convert image color to image grayscale for using Watershed. When I use color space BGR, no problem but with space HSV, i'm not sure that the code � Extracting the Percentage of color (Red,blue,green,yellow,orange) in an image in Opencv? Correct HSV InRange Values for 'Red' Objects. how to convert cifar10.bin from rgb to HSV. balltracking with python 2.7 and opencv. Detecting objects in OpenCV. How to find the hsv range of green gloves in a particular image?

Unfortunately I am unable to comment because of insufficient earned reputation.

Taking the 3rd channel alone will may give grey values you do not expect, as increasingly fully saturated colors,as an extreme example RGB 0,0,255 will appear as pure white once converted to grey scale by taking the value hsv channel.

This could certainly affect watershed (dependent on image content) as saturated red, greens and blues would not be differentiated in the V channel.

The obvious, converting to BGR then Grayscale could be a better option.

Changing Colorspaces, For BGR \rightarrow Gray conversion, we use the flag cv. Now that we know how to convert a BGR image to HSV, we can use this to extract a colored object. Method 2: Using cvtColor() function cvtColor() function in OpenCV is very helpful in converting color channels from one to another such as BRG to HSV or BRG to RGB. The same method can be used to convert BRG to GRAY by using the cv2.cvtColor(img,cv2.BGR2GRAY)

in HSV color-space, V channel is defined as max(R, G, B) but in gray-scale, value is defined by mean(R, G, B). in RGB2HSV conversion, we use these formulas for S and V channel:

V = max(R, G, B)
S = (max(R, G, B) - min(R, G, B)) / max(R, G, B)


so if S is zero, max(R, G, B) equals to min(R, G, B) and they are equal to mean(R, G, B). so if this criteria holds, V channel is equal to gray-scale value. other wise, they are different.

one way is to convert image to RGB and then convert it to GRAY. but if you look for a more straight way, you can use picture below:

HSV2RGB converion

and hence gray value is mean(R, G, B) you can calculate it as:

gray = m + (c + x) / 3


where you can compute m,c and x from formula in image.

Converting a rgb image to hsv and to grayscale, If your original image is BGR and you want only gray-scale, skip the HSV stage and use CV_BGR2GRAY. Only if you are interested in saturation or hue, use CV_BGR2HSV. In the HSV color space, the V is the same as gray-scale. For HSV, hue range is [0,179], saturation range is [0,255], and value range is [0,255]. Different software use different scales. So if you are comparing OpenCV values with them, you need to normalize these ranges. Object Tracking . Now that we know how to convert a BGR image to HSV, we can use this to extract a colored object.

Color conversions, \text{Gray to RGB[A]:} \quad R \leftarrow Y, G \leftarrow Y, B \leftarrow Y, The conversion from a RGB image to gray is done with: RGB \leftrightarrow HSV. In this article, we are going to learn how to convert an RGB format image Into an HSV format image using OpenCV in Python? Submitted by Ankit Rai, on May 03, 2019 An HSV is another type of color space in which H stands for Hue, S stands for Saturation and V stands for Value. A Hue represents color. It is an angle from 0 degrees to 360 degrees.

Changing Colorspaces — OpenCV-Python Tutorials 1 documentation, In this tutorial, you will learn how to convert images from one color-space to another, like BGR \leftrightarrow Gray, BGR \leftrightarrow HSV etc. In addition to that� Next, we need to convert the image to gray scale. To do it, we need to call the cvtColor function, which allows to convert the image from a color space to another. As first input, this function receives the original image. As second input, it receives the color space conversion code.

How to convert a HSV image into gray image????, Use rgb2gray: hsvImage = rgb2hsv(rgbImage); % Now do some computations on the HSV image. % Now convert back to RGB. rgbImage2 = hsv2rgb(hsvImage); % Convert to gray scale. grayImage = rgb2gray(rgbImage2); I'm a newbie in OpenCV. I'm learning the Segmentation by Watershed algorithm and I have a problem. I have to convert the image color to grayscale for using Watershed. When I use the BGR color space, no problem but with HSV, I'm not sure that the code below is correct.

• How about CV_HSV2GRAY? Nope there is no such thing. You can't convert HSV to gray directly.
• No option CV_HSV2GRAY, thanks guneykayim
• Why would you want to convert this? HSV is a color representation and are used for analysis in image processing. Don't get me wrong, but I'm curious to know why you want this function..
• I'm doing image segmentation with Watershed algorithm and it have one etape: convert image color to grayscale. In BGR, no problem but in HSV space (maybe in LAB, too) I don't have any idea. I don't know that I do like the code above, correct or not?
• Thanks a lot, with Lab space, it's the same. I just select L channel (channel[0]) for grayscale.