OpenCV: convert Scalar to different color space

前端 未结 4 1569
刺人心
刺人心 2021-01-17 21:28

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


        
相关标签:
4条回答
  • 2021-01-17 21:55

    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]);
    }
    
    0 讨论(0)
  • 2021-01-17 21:57

    This worked for me,

    Mat rgb;
    Mat hsv(1, 1, CV_8UC3, Scalar(224, 224, 160));
    
    cvtColor(hsv, rgb, CV_HSV2BGR);
    Scalar rgb = Scalar((int)rgb.at<cv::Vec3b>(0, 0)[0],(int)rgb.at<cv::Vec3b>(0, 0)[0],(int)rgb.at<cv::Vec3b>(0, 0)[0])
    
    0 讨论(0)
  • 2021-01-17 22:06

    OpenCV 3.2.0. Note: h is in range [0,360] and l and s is in [0,1]

            Mat hls(1, 1, CV_32FC3, Scalar(h, l, s));
            Mat rgb;
            cvtColor(hls, rgb, COLOR_HLS2RGB);
            Scalar c = Scalar(255*rgb.at<float>(0,0), 255*rgb.at<float>(0,1), 255*rgb.at<float>(0,2));
    
    0 讨论(0)
  • 2021-01-17 22:15

    Use this to convert a single value:

    cv::Vec3f rgb;
    cv::Vec3f hsv;
    hsv[0] = H;
    hsv[1] = S;
    hsv[2] = V;
    cvtColor(hsv, rgb, CV_HSV2BGR);
    

    Then you can use it:

    rectangle(imgOriginal, Point(0, 0), Point(25, 50), 
        Scalar(rgb[0], rgb[1], rgb[2]), CV_FILLED);
    
    0 讨论(0)
提交回复
热议问题