0

For example, I create

Mat mat1 = Mat::zeros(Size(100, 100), CV_8UC3);

and fill each pixel with (0, 255, 255), which is supposed to be red in hsv.

However, if I imshow this mat, this will be printed as a BGR image and is not red.

How do I make this mat hsv format and setting (0, 255, 255) result in red?

MMM
  • 79
  • 1
  • 7

1 Answers1

0

imshow assumes that the image you pass to it is in BGR color space. However, you can create a small function that does your imshow of HSV images.

void imshowHSV(std::string& name, cv::Mat& image)
{
  cv::Mat hsv;
  cv:cvtColor(image, hsv, CV_HSV2BGR);
  cv::imshow(name, hsv);
}

But beware! this will convert and create a copy of the image, if you over use it it may have quite some overhead :)

api55
  • 11,070
  • 4
  • 41
  • 57
  • Don't you have to convert it the other way around, e. g. `CV_HSV2BGR`? `Mat` itself doesn't care about the color space, it only holds numbers. We can put whatever we want there (say, `HSV`). If `imshow` treats those as `BGR` values, the result is going to look wrong, so we convert them to `BGR`, then `BGR`, treated as `BGR`, is going to look right. – Headcrab Jun 25 '18 at 05:27
  • @Headcrab yes it is the other way around XD I wrote it fast and made that mistake, thanks for the comment I will fix it right away – api55 Jun 25 '18 at 05:29