首页 > 解决方案 > 在单通道图像上应用 openCV 查找表 (LUT)

问题描述

我的目标是拍摄图像,将其转换为 LAB 颜色空间,然后仅在亮度通道上应用自定义查找表并显示它。LUT 将应用从蓝色到红色的渐变 - 因此我的输出图像将以红色显示图像最亮的部分,以蓝色显示最暗的部分。

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

但是当我运行我的 (Qt) 应用程序时,我得到一个 Microsoft Visual C++ 运行时库调试错误: VC++ 运行时错误

和:

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

我相信这个问题与将 LUT 应用到单通道图像有关,因为如果我将 LUT 应用到输入图像,上面的代码就会运行。所以而不是:

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

我将其更改为:

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

但我想将我的 LUT 仅应用于 LAB 颜色空间中的 L 通道并丢弃 A 和 B 通道。

问题与我创建 LUT 的方式有关吗?我应该如何为单通道图像创建这样的 LUT?

标签: c++opencv

解决方案


Thanks to @Dan Mašek's comment above. 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. Just merge 3 copies of the L channel back into a 3 channel Mat, and apply the LUT to the result.

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

推荐阅读