Alessandro Cerro
Alessandro Cerro

Reputation: 179

cvtColor from RGBA to HLS doesn't modify anything

Dears, I'm new to opencv, and I don't know why, when I try to convert from RGBA to HLS, I don't receive any change. Better explaining, I was previously working with a 3channel image, so I was reading and converting from RGB to HLS/whatever else... Now, for other purposes (I needed to access to the alpha channel), I'm reading in BGR - converting in RGBA, and then processing the RGB channel via cv::Vec4b. But when it comes to convert to HLS, I don't receive any change. This is my function, that takes 2 cv::Mat input, 'src' & 'dst', convert them in HLS, apply some values, and then reconvert them to RGBA, below code:

    cvtColor(src, src, CV_RGBA2RGB);
    cvtColor(src, src, CV_RGB2HLS);

    cvtColor(dst, dst, CV_RGBA2RGB);
    cvtColor(dst, dst, CV_RGB2HLS);


    for(int i = 0; i < src.rows; i++)
        for(int j = 0; j < src.cols; j++){
            dst.at<cv::Vec3b>(i,j)[0] = cv::saturate_cast<uchar>(src.at<cv::Vec3b>(i,j)[0] + hue);
            dst.at<cv::Vec3b>(i,j)[1] = cv::saturate_cast<uchar>(src.at<cv::Vec3b>(i,j)[1] + luminance);
            dst.at<cv::Vec3b>(i,j)[2] = cv::saturate_cast<uchar>(src.at<cv::Vec3b>(i,j)[2] + saturation);
        }



    cvtColor(dst, dst, CV_HLS2RGB);
    cvtColor(dst, dst, CV_RGB2RGBA);

    cvtColor(src, src, CV_HLS2RGB);
    cvtColor(src, src, CV_RGB2RGBA);

where Hue, Luminance, Saturation, are some integer values. As said, previously, working just with RGB, was totally fine. Now, by using RGBA, it doesn't change anything.

Upvotes: 0

Views: 270

Answers (1)

Alessandro Cerro
Alessandro Cerro

Reputation: 179

by creating a new Mat is now working in RGBA:

    cv::Mat hslIN = src.clone();
    cv::Mat hslOUT;
    cv::cvtColor(hslIN, hslIN, CV_RGBA2RGB);

    cv::cvtColor(hslIN, hslIN, CV_RGB2HLS);
    cv::cvtColor(hslIN, hslOUT, CV_RGB2HLS);

    for(int i = 0; i < src.rows; i++)
        for(int j = 0; j < src.cols; j++){
            hslOUT.at<cv::Vec3b>(i,j)[0] = cv::saturate_cast<uchar>(hslIN.at<cv::Vec3b>(i,j)[0] + hue);
            hslOUT.at<cv::Vec3b>(i,j)[1] = cv::saturate_cast<uchar>(hslIN.at<cv::Vec3b>(i,j)[1] + luminance);
            hslOUT.at<cv::Vec3b>(i,j)[2] = cv::saturate_cast<uchar>(hslIN.at<cv::Vec3b>(i,j)[2] + saturation);
        }

    cv::cvtColor(hslOUT, hslOUT, CV_HLS2RGB);
    cv::cvtColor(hslOUT, dst, CV_RGB2RGBA);

Upvotes: 0

Related Questions