【问题标题】:Color correction in Opencv with color correction matrix使用颜色校正矩阵在 Opencv 中进行颜色校正
【发布时间】:2018-06-04 23:42:46
【问题描述】:

我有 4 台 raspberry pi 相机,它们从不同位置拍摄同一场景的照片。我想让它们在颜色方面尽可能相似。我尝试了一些直方图均衡,但没有取得多大成功。在互联网上搜索,颜色校正矩阵(CCM)出现了很多,所以我想在 Opencv,C++ 中尝试一下。我知道有关于色彩校正和色彩校准的完整理论,但是我只想尝试 CCM 并在 Opencv 中查看其结果,而不仅仅是 Matlab 或其他软件。

我拍了两张 MacBeth 颜色图表的照片并手动获取样本颜色。 (我知道我可以自动执行此操作,但在执行此操作之前,我想确保值得付出努力)。

这是我的代码:

//input images
CameraInfo c1, c2;
cv::Mat inputImgCam1 = cv::imread("color_c1.jpeg");
cv::Mat inputImgCam2 = cv::imread("color_c3.jpeg");

//convert them to float for multiplication
cv::Mat3f cam1F, cam2F;
inputImgCam1.convertTo(cam1F, CV_32FC3, 1 / 255.0);
inputImgCam2.convertTo(cam2F, CV_32FC3, 1 / 255.0);

//take manually the source and target colours
c1.Initialize(inputImgCam1, cv::Size(14, 8), false);
c2.Initialize(inputImgCam2, cv::Size(14, 8), false);

std::vector<cv::Vec3b> colors1 = c1.GetColors();
std::vector<cv::Vec3b> colors2 = c2.GetColors();

//convert them to Mat - 3 ch, 1 col, 4 rows
cv::Mat source(colors1);// = Convert(colors1);
cv::Mat target(colors2);// = Convert(colors2);

//reshape them - 1 ch, 3 cols, 4 rows
cv::Mat src = source.reshape(1, source.size().height);
cv::Mat trg = target.reshape(1, target.size().height);

//convert them to float
cv::Mat srcFloat, trgFloat;
src.convertTo(srcFloat, CV_32FC1, 1 / 255.0);
trg.convertTo(trgFloat, CV_32FC1, 1 / 255.0);

std::cout << srcFloat.size() << " " << srcFloat.t().size() << " " << trgFloat.size() << " " << trgFloat.t().size();

//compute the colour correction matrix: A*M = B => M = (A' * A).inv() * A' * B
cv::Mat ccm = (trgFloat.t() * trgFloat).inv() * trgFloat.t() * srcFloat;

//reshape the source image to 1 ch, width * height cols, 3 rows
cv::Mat cam1Reshaped = cam1F.reshape(1, 3);

//perform correction
cv::Mat result = cam1Reshaped.t() * ccm;

//reshape back, 3 ch, width cols, height rows
cv::Mat resultR = result.reshape(3, inputImgCam1.size().height);

//convert to uchar for saving
cv::Mat out;
resultR.convertTo(out, CV_8UC3, 255);

cv::imwrite("ccm_c1.jpg", out);

这里是 MacBeth 颜色图表的图像:source imagetarget image ; result

我的 CCM 看起来像这样:ccm values

显然有问题!结果图像看起来不像输入图像。我从here 获得灵感,但是当涉及到矩阵乘法时,opencv 有点让事情变得更加困难(图像必须是浮动的,顺序是列 x 行,而不是行 x 列..)。我希望得到至少与上面链接中的结果相同的结果。至少输入和输出显示相同的东西。

我希望你能帮助我。使用opencv我没有找到太多支持。 谢谢!

【问题讨论】:

  • "图像必须是浮点数,顺序是列 x 行,而不是行 x 列" 是的图像必须是浮点数,但矩阵顺序是 rows x cols,与您执行的矩阵乘法运算相同在纸上。
  • 你是对的!这就是问题所在。我错误地重塑了输入图像。我将发布正确的代码和结果图像。非常感谢!

标签: c++ opencv image-processing colors


【解决方案1】:

我复制了上面的结果(粉红色背景),发现了一行错误并在这里报告(我不允许评论)以便其他人可以节省一些时间。

改变后

cv::Mat ccm = trgFloat.t() * srcFloat * (trgFloat.t() * trgFloat).inv();

cv::Mat ccm = (srcFloat.t() * srcFloat).inv()* srcFloat.t() * trgFloat;

我得到了完美的结果图像。

【讨论】:

    【解决方案2】:

    感谢@Catree,我设法获得了与其他answer 相似的结果。

    这是更正后的代码:

    //input images
    CameraInfo c1, c2;
    cv::Mat inputImgCam1 = cv::imread("color_c1.jpeg");
    cv::Mat inputImgCam2 = cv::imread("color_c3.jpeg");
    
    //convert them to float for multiplication
    cv::Mat3f cam1F, cam2F;
    inputImgCam1.convertTo(cam1F, CV_32FC3, 1 / 255.0);
    inputImgCam2.convertTo(cam2F, CV_32FC3, 1 / 255.0);
    
    //take manually the source and target colours
    c1.Initialize(inputImgCam1, cv::Size(14, 8), false);
    c2.Initialize(inputImgCam2, cv::Size(14, 8), false);
    
    std::vector<cv::Vec3b> colors1 = c1.GetColors();
    std::vector<cv::Vec3b> colors2 = c2.GetColors();
    
    //convert them to Mat - 3 ch, 4 rows, 1 col
    cv::Mat source(colors1);// = Convert(colors1);
    cv::Mat target(colors2);// = Convert(colors2);
    
    //reshape them - 1 ch, 4 rows, 3 cols
    cv::Mat src = source.reshape(1, source.size().height);
    cv::Mat trg = target.reshape(1, target.size().height);
    
    //convert them to float
    cv::Mat srcFloat, trgFloat;
    src.convertTo(srcFloat, CV_32FC1, 1 / 255.0);
    trg.convertTo(trgFloat, CV_32FC1, 1 / 255.0);
    
    std::cout << srcFloat.size() << " " << srcFloat.t().size() << " " << trgFloat.size() << " " << trgFloat.t().size();
    
    //compute the colour correction matrix: A*M = B => M = (A' * A).inv() * A' * B
    cv::Mat ccm = trgFloat.t() * srcFloat * (trgFloat.t() * trgFloat).inv();
    
    //reshape the source image to 1 ch, width * height rows, 3 cols
    cv::Mat cam1Reshaped = cam1F.reshape(1, cam1F.size().height * cam1F.size().width);
    
    //perform correction
    cv::Mat result = cam1Reshaped * ccm;
    
    //reshape back, 3 ch, height rows, width cols
    cv::Mat resultR = result.reshape(3, inputImgCam1.size().height);
    
    //convert to uchar for saving
    cv::Mat out;
    resultR.convertTo(out, CV_8UC3, 255);
    
    cv::imwrite("ccm_c1.jpg", out);
    

    这是乘法后的CCMresult。 我认为更好的颜色采样将改善输出。

    【讨论】:

    • 什么是“CameraInfo”?它看起来像 Android 上的东西,但我没有看到它的“初始化”成员。
    • 这是一个自定义类。与安卓无关。
    猜你喜欢
    • 2015-08-13
    • 2012-09-02
    • 2016-12-12
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 2018-03-14
    相关资源
    最近更新 更多