fix mathematical mistake in normalize

This commit is contained in:
2021-06-18 00:34:11 +02:00
parent 58bf764af9
commit 544455bea8

View File

@ -21,28 +21,44 @@
#include <opencv2/core/ocl.hpp> #include <opencv2/core/ocl.hpp>
#include <opencv2/imgproc.hpp> #include <opencv2/imgproc.hpp>
#include <stdint.h> #include <stdint.h>
#include <assert.h>
inline bool normalize(cv::Mat& image, const cv::Mat& referance) inline bool normalize(cv::Mat& image, const cv::Mat& referance)
{ {
cv::Mat labReferance; assert(image.size() == referance.size());
cv::cvtColor(referance, labReferance, cv::COLOR_BGR2Lab); assert(image.type() == referance.type());
std::vector<cv::Mat> labPlanesRef(3); assert(image.type() == CV_8UC3 || image.type() == CV_32FC1);
cv::split(labReferance, labPlanesRef);
if(image.type() == CV_8UC3)
{
cv::Mat labReferance;
cv::cvtColor(referance, labReferance, cv::COLOR_BGR2Lab);
std::vector<cv::Mat> labPlanesRef(3);
cv::split(labReferance, labPlanesRef);
cv::Scalar mean = cv::mean(labPlanesRef[0]); cv::Scalar mean = cv::mean(labPlanesRef[0]);
labPlanesRef[0].convertTo(labPlanesRef[0], CV_16SC1); labPlanesRef[0].convertTo(labPlanesRef[0], CV_16SC1);
labPlanesRef[0] = labPlanesRef[0] - cv::Scalar(mean); labPlanesRef[0] = mean/labPlanesRef[0];
cv::Mat labImage; cv::Mat labImage;
cv::cvtColor(image, labImage, cv::COLOR_BGR2Lab); cv::cvtColor(image, labImage, cv::COLOR_BGR2Lab);
std::vector<cv::Mat> labPlanes(3); std::vector<cv::Mat> labPlanes(3);
cv::split(labImage, labPlanes); cv::split(labImage, labPlanes);
cv::subtract(labPlanes[0], labPlanesRef[0], labPlanes[0], cv::noArray(), CV_8UC1); labPlanes[0] = labPlanes[0].mul(labPlanesRef[0]);
cv::merge(labPlanes, labImage); cv::merge(labPlanes, labImage);
cv::cvtColor(labImage, image, cv::COLOR_Lab2BGR); cv::cvtColor(labImage, image, cv::COLOR_Lab2BGR);
}
else
{
cv::Scalar mean = cv::mean(referance);
cv::Mat referanceNorm;
referance.copyTo(referanceNorm);
referanceNorm = mean/referanceNorm;
image = image*referanceNorm;
}
return true; return true;
} }