0
votes

I want to cvt bgr mat to lab mat. Then change some pixels in mat. After that, I restore image.

int main() {
    Mat originalMat = imread("E:\\cworkspace\\Opencv\\Opencv\\test3.jpg");
    if (originalMat.empty()) {
        cout << "load failed" << endl;
        return -1;
    }
    else {
        cout << "load success" << endl << endl;
    }
    Rect  rect = Rect(50, 50, 300, 300);
    Mat roi_img = originalMat(rect);
    cvtColor(originalMat, originalMat, COLOR_BGR2Lab);

    for (int i = 1; i < roi_img.rows; i++) {
        for (int j = 1; j < roi_img.cols; j++) {
            roi_img.at<Vec3b>(i, j)[0] = 0;
            roi_img.at<Vec3b>(i, j)[1] = 0;
            roi_img.at<Vec3b>(i, j)[2] = 0;
        }
    }
    cvtColor(originalMat, originalMat, COLOR_Lab2BGR);
    imshow("originalMat", originalMat);
    waitKey();
    return 0;
}

This is my simplified code.Eventually the ROI was changed to blue.From my understanding.It should be black.

enter image description here

1
(0,0,0) isn't black in Lab when 8bit unsigned values are used. See the docs -- a and b are offset by 128.Dan Mašek

1 Answers

0
votes

I think you should replace <Vec3b> with <Vec3f> because after BGR2Lab conversion, the type of the output image would be float and so with <Vec3b>, it writes nonsense values. I mean do this:

roi_img.at<Vec3f>(i, j)[0] = 0;
roi_img.at<Vec3f>(i, j)[1] = 0;
roi_img.at<Vec3f>(i, j)[2] = 0;