9

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.

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 converting from BGR to HSV, Hue = Blue, Saturation = Green, Value = Red and I can use the operator BGR2GRAY for convert from HSV to grayscale.

The 2 output images are different. Can I convert HSV to grayscale like that?

//Is it similaire with color space LAB?

hlzl
  • 479
  • 2
  • 5
  • 17
Amateur
  • 151
  • 1
  • 2
  • 12
  • 1
    How about CV_HSV2GRAY? Nope there is no such thing. You can't convert HSV to gray directly. – guneykayim May 22 '14 at 15:59
  • No option CV_HSV2GRAY, thanks guneykayim – Amateur May 22 '14 at 16:36
  • 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.. – Øystein W. May 22 '14 at 16:54
  • 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? – Amateur May 22 '14 at 16:59

4 Answers4

26

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();
hlzl
  • 479
  • 2
  • 5
  • 17
RevJohn
  • 1,054
  • 9
  • 15
  • Thanks a lot, with Lab space, it's the same. I just select L channel (channel[0]) for grayscale. – Amateur May 23 '14 at 00:47
3
hsv1 = cv2.cvtColor(frame1, cv2.COLOR_BGR2HSV)

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

cv2.imshow("gray-image",v1)
Book Of Zeus
  • 49,509
  • 18
  • 174
  • 171
  • Welcome to stackoverflow. In addition to the answer you've provided, consider giving a summary as to why this solves the issue. – jtate Mar 14 '19 at 02:25
1

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.

mqod
  • 93
  • 6
0

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.

Graham Monkman
  • 404
  • 6
  • 9
  • Does the formula for bgr then grayscale not yield the V channel mathematically? There are by definition lots of things besides saturated pixels you won't be able to resolve when you compress 24 bits into 8. – Mad Physicist May 09 '19 at 05:46