【问题标题】:how to change pixels in lab mat in opencv如何在opencv中更改实验室垫中的像素
【发布时间】:2018-10-08 09:07:12
【问题描述】:

我想将 bgr mat 转换为实验室垫。然后改变mat中的一些像素。之后,我恢复图像。

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;
}

这是我的简化代码。最终投资回报率变成了蓝色。根据我的理解。它应该是黑色的。

【问题讨论】:

  • 当使用 8 位无符号值时,(0,0,0) 在 Lab 中不是黑色的。参见the docs -- ab 偏移 128。

标签: c++ opencv


【解决方案1】:

我认为您应该将&lt;Vec3b&gt; 替换为&lt;Vec3f&gt;,因为在BGR2Lab 转换后,输出图像的类型将是float,因此&lt;Vec3b&gt; 会写入无意义的值。 我的意思是这样做:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 2015-09-29
    • 2017-08-08
    • 1970-01-01
    相关资源
    最近更新 更多