From 544455bea83a0f3b5cf6eecd34ebfc214572935e Mon Sep 17 00:00:00 2001 From: uvos Date: Fri, 18 Jun 2021 00:34:11 +0200 Subject: [PATCH] fix mathematical mistake in normalize --- src/uvosunwrap/normalize.h | 54 ++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/src/uvosunwrap/normalize.h b/src/uvosunwrap/normalize.h index 1526af6..2018e81 100644 --- a/src/uvosunwrap/normalize.h +++ b/src/uvosunwrap/normalize.h @@ -21,28 +21,44 @@ #include #include #include +#include inline bool normalize(cv::Mat& image, const cv::Mat& referance) { - cv::Mat labReferance; - cv::cvtColor(referance, labReferance, cv::COLOR_BGR2Lab); - std::vector labPlanesRef(3); - cv::split(labReferance, labPlanesRef); + assert(image.size() == referance.size()); + assert(image.type() == referance.type()); + assert(image.type() == CV_8UC3 || image.type() == CV_32FC1); + + if(image.type() == CV_8UC3) + { + cv::Mat labReferance; + cv::cvtColor(referance, labReferance, cv::COLOR_BGR2Lab); + std::vector labPlanesRef(3); + cv::split(labReferance, labPlanesRef); - cv::Scalar mean = cv::mean(labPlanesRef[0]); - labPlanesRef[0].convertTo(labPlanesRef[0], CV_16SC1); - - labPlanesRef[0] = labPlanesRef[0] - cv::Scalar(mean); - - - cv::Mat labImage; - cv::cvtColor(image, labImage, cv::COLOR_BGR2Lab); - - std::vector labPlanes(3); - cv::split(labImage, labPlanes); - - cv::subtract(labPlanes[0], labPlanesRef[0], labPlanes[0], cv::noArray(), CV_8UC1); - cv::merge(labPlanes, labImage); - cv::cvtColor(labImage, image, cv::COLOR_Lab2BGR); + cv::Scalar mean = cv::mean(labPlanesRef[0]); + labPlanesRef[0].convertTo(labPlanesRef[0], CV_16SC1); + + labPlanesRef[0] = mean/labPlanesRef[0]; + + + cv::Mat labImage; + cv::cvtColor(image, labImage, cv::COLOR_BGR2Lab); + + std::vector labPlanes(3); + cv::split(labImage, labPlanes); + + labPlanes[0] = labPlanes[0].mul(labPlanesRef[0]); + cv::merge(labPlanes, labImage); + 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; }