1

How can I convert just one pixel from from one color space to another? I figured out that cv::cvtColor does not accept cv::Scalar neither cv::Vec3f. Or in other words, it accepts them but raise an error since they are going to be processed as 1-Channel cv::Mat at the end. Is there a better (cleaner like directly from cv::Scalar or cv::VecYX) way than constructing a one element cv::Mat like this?

cv::Vec3b covert_1_pixel_to_lab(cv::Vec3b const& src){
    cv::Vec3b src(b,g,r);
    cv::Mat src_mat(1, 1, CV_8UC3);
    src_mat.ptr<uchar>(0)[0] = src(0);
    src_mat.ptr<uchar>(0)[1] = src(1);
    src_mat.ptr<uchar>(0)[2] = src(2);
    cv::Mat dst_mat;
    cv::cvtColor(src_mat, dst_mat, cv::COLOR_BGR2Lab);
    return cv::Vec3b(dst_mat.ptr<uchar>(0)[0],
        dst_mat.ptr<uchar>(0)[1],
        dst_mat.ptr<uchar>(0)[2]);
}
Andre Holzner
  • 18,333
  • 6
  • 54
  • 63
Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160

1 Answers1

1

You can use ROI via Rects:

Rect r=Rect(x,y,1,1);
Mat img2=img(r);
cvtColor(img2,img2,CV_BGR2Luv);//or any other color space

This may make the change on img as well

Saransh Kejriwal
  • 2,467
  • 5
  • 15
  • 39