Reputation: 71
I have 4 raspberry pi cameras which takes photos of the same scene, from different positions. I want to make them as similar as possible in terms of colors. I've tried some histogram equalization without much success. Searching on the internet, color correction matrix (CCM) came up a lot so I thought to give it a try in Opencv, C++. I know there is an entire theory about colour correction and colour calibration, however I want to try only CCM and see its results in Opencv too, not just Matlab or other software.
I took 2 photos of a MacBeth color chart and manually get the samples colors. (I know I can do this automatically, but before I do this I want to make sure is worth the effort).
Here is my code:
//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);
And here are the images of a MacBeth color chart: source image ; target image ; result
My CCM looks something like this: ccm values
Clearly something is wrong! The result image doesn't looks nothing like the input images. I took inspiration from here, but opencv kind of makes things more difficult when it comes to matrix multiplication (the images must be float, the order is cols x rows, not rows x cols..). I want to have a result at least as the one in the link above. At least the input and output display the same thing.
I hope you can help me. I haven't found much support with this using opencv.. Thanks!
Upvotes: 1
Views: 10267
Reputation: 21
I reproduced above result(pinkish background), found one line of bug and reported here (I was not allow to comment) so that other people can save some time.
After changing
cv::Mat ccm = trgFloat.t() * srcFloat * (trgFloat.t() * trgFloat).inv();
to
cv::Mat ccm = (srcFloat.t() * srcFloat).inv()* srcFloat.t() * trgFloat;
I got perfect result image.
Upvotes: 2
Reputation: 71
Thanks to @Catree I've managed to obtain a similar result to the other answer.
This is the corrected code:
//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);
And here are the CCM and the result after the multiplication. I think a better sampling of the colours will improve the output.
Upvotes: 1