简体   繁体   English

在单通道图像上应用 openCV 查找表 (LUT)

[英]Apply an openCV Lookup Table (LUT) on a single channel image

My goal is to take an image, convert it to LAB color space, and then apply a custom lookup table on only the lightness channel and display that.我的目标是拍摄图像,将其转换为 LAB 颜色空间,然后仅在亮度通道上应用自定义查找表并显示它。 The LUT would apply a gradient from blue to red - so my output image would show the brightest parts of the image in red, and the darkest in blue. LUT 将应用从蓝色到红色的渐变 - 所以我的 output 图像将以红色显示图像最亮的部分,以蓝色显示最暗的部分。

void MainWindow::convertBGRMatToLAB(const cv::Mat inputMat)
{
    // Create lookup table (LUT)
    cv::Mat lookupTable(1, 256, CV_8UC(3));
    for (int i=0; i<256; i++)
    {
        lookupTable.at<cv::Vec3b>(i)[0]= 255-i; // first channel  (B)
        lookupTable.at<cv::Vec3b>(i)[1]= 0;     // second channel (G)
        lookupTable.at<cv::Vec3b>(i)[2]= 255+i; // ...            (R)
    }

    // Convert to LAB color space.
    cv::Mat convertedLAB;
    cv::cvtColor(inputMat, convertedLAB, CV_BGR2Lab);

    // Isolate the L, A, B channels.
    cv::Mat convertedLABSplit[3];
    cv::split(convertedLAB, convertedLABSplit);

    // Apply our custom lookup table to L channel.
    cv::Mat outputMat(inputMat.rows, inputMat.cols, CV_8UC3);
    cv::LUT(convertedLABSplit[0], lookupTable, outputMat); // Program crashes here.
    //cv::LUT(inputMat, lookupTable, outputMat); // This works (but not what I am looking to do).

    // Show the output image
    cv::imshow("Output Image", outputMat);
}

But when I run my (Qt) application I get a Microsoft Visual C++ Runtime Library Debug error:但是当我运行我的(Qt)应用程序时,我得到一个 Microsoft Visual C++ 运行时库调试错误: VC++ 运行时错误

and:和:

OpenCV Error: Assertion failed ((lutcn == cn || lutcn == 1) && _lut.total() == 256 && _lut.isContinuous() && (depth == 0 || depth == 1)) in cv::LUT, file C:\OpenCV\3.4.0\source\opencv-3.4.0\modules\core\src\convert.cpp, line 4552

I believe the issue is related to applying a LUT to a single channel image, as the above code runs if I apply the LUT to just the input image.我相信这个问题与将 LUT 应用到单通道图像有关,因为如果我将 LUT 应用到输入图像,上面的代码就会运行。 So instead of:所以而不是:

cv::LUT(convertedLABSplit[0], lookupTable, outputMat);

I change it to:我将其更改为:

cv::LUT(inputMat, lookupTable, outputMat);

But I would like to apply my LUT to only the L channel in the LAB color space and discard the A and B channels.但我想将我的 LUT 仅应用于 LAB 颜色空间中的 L 通道并丢弃 A 和 B 通道。

Is the problem related to how I am creating my LUT?问题与我创建 LUT 的方式有关吗? How should I create a LUT like this for a single channel image?我应该如何为单通道图像创建这样的 LUT?

Thanks to @Dan Mašek's comment above.感谢@Dan Mašek 上面的评论。 I'm adding it as answer here so the question can be marked answered.我在这里将其添加为答案,以便可以将问题标记为已回答。

For a 3 channel lookup table, the input also needs to be 3 channel.对于 3 通道查找表,输入也需要是 3 通道。 Just merge 3 copies of the L channel back into a 3 channel Mat, and apply the LUT to the result.只需将 L 通道的 3 个副本合并回一个 3 通道 Mat,然后将 LUT 应用于结果。

Updated code:更新代码:

void MainWindow::convertLightToDarkColorMap(const cv::Mat inputMat)
{
    // Create lookup table (LUT)

    cv::Mat lookupTable(1, 256, CV_8UC(3));
    for (int i=0; i<256; i++)
    {
        lookupTable.at<cv::Vec3b>(i)[0]= 255-i; // first channel  (B)
        lookupTable.at<cv::Vec3b>(i)[1]= 0;     // second channel (G)
        lookupTable.at<cv::Vec3b>(i)[2]= 255+i; // ...            (R)
    }

    // Convert to LAB color space.
    cv::Mat convertedLAB;
    cv::cvtColor(inputMat, convertedLAB, CV_BGR2Lab);

    // Isolate the L, A, B channels.
    cv::Mat convertedLABSplit[3];
    cv::split(convertedLAB, convertedLABSplit);

    // 3 channel LUT only works on a 3 channel input.  So take (3) copes of the L channel and merge them into one.
    cv::Mat trippleL;
    std::vector<cv::Mat> trippleLArr;
    trippleLArr.push_back(convertedLABSplit[0]);
    trippleLArr.push_back(convertedLABSplit[0]);
    trippleLArr.push_back(convertedLABSplit[0]);
    cv::merge( trippleLArr, trippleL);

    // Apply our custom lookup table to L channel.
    cv::Mat outputMat(inputMat.rows, inputMat.cols, CV_8UC3);
    cv::LUT(trippleL, lookupTable, outputMat);

    // Show the output image
    cv::imshow("Output", outputMat);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM