Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV: convert Scalar to different color space

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.)

like image 215
fuenfundachtzig Avatar asked Apr 04 '17 21:04

fuenfundachtzig


People also ask

How do I change color space in OpenCV?

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.

How do you convert BGR to HSV?

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.

What is cvtColor in OpenCV?

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]])


1 Answers

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]);
}
like image 191
Morris Franken Avatar answered Sep 22 '22 08:09

Morris Franken