I am using a Scalar
to define the color of a rectangle I am drawing with OpenCV
:
rectangle(imgOriginal, Point(0, 0), Point(25, 50), Scalar(H, S, V), CV_FILLED);
However, the color is defined in HSV color space rather than RGB (imgOriginal
is RGB).
How do I convert Scalar
(or its input, the integer variables H
, S
, and V
) to RGB?
(So far I only found answers telling me how to convert a whole image with cvtColor
which is not what I want.)
Changing Color-space For color conversion, we use the function cv. cvtColor(input_image, flag) where flag determines the type of conversion. For HSV, hue range is [0,179], saturation range is [0,255], and value range is [0,255]. Different software use different scales.
In OpenCV, to convert an RGB image to HSV image, we use the cv2. cvtColor() function. This function is used to convert an image from one color space to another.
cvtColor() method is used to convert an image from one color space to another. There are more than 150 color-space conversion methods available in OpenCV. We will use some of color space conversion codes below. Syntax: cv2.cvtColor(src, code[, dst[, dstCn]])
Although not optimal, You can use the following:
Scalar ScalarHSV2BGR(uchar H, uchar S, uchar V) {
Mat rgb;
Mat hsv(1,1, CV_8UC3, Scalar(H,S,V));
cvtColor(hsv, rgb, CV_HSV2BGR);
return Scalar(rgb.data[0], rgb.data[1], rgb.data[2]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With