OpenCV中的本地规范化

stf*_*ani 6 c++ matlab opencv image-processing

我试图在OpenCV中实现局部归一化算法,以减少图像中的照明差异.我找到了一个MATLAB函数,我在OpenCV中实现了它.但是,我得到的结果与MATLAB函数给出的结果不同.

这是我的代码:

Mat localNorm(Mat image, float sigma1, float sigma2)
{
    Mat floatGray, blurred1, blurred2, temp1, temp2, res;

    image.convertTo(floatGray, CV_32FC1);
    floatGray = floatGray/255.0;

    int blur1 = 2*ceil(-NormInv(0.05, 0, sigma1))+1;
    cv::GaussianBlur(floatGray, blurred1, cv::Size(blur1,blur1), sigma1);
    temp1 = floatGray-blurred1;

    cv::pow(temp1, 2.0, temp2);
    int blur2 = 2*ceil(-NormInv(0.05, 0, sigma2))+1;
    cv::GaussianBlur(temp2, blurred2, cv::Size(blur2,blur2), sigma2);
    cv::pow(blurred2, 0.5, temp2);

    floatGray = temp1/temp2;
    floatGray = 255.0*floatGray;
    floatGray.convertTo(res, CV_8UC1);

    return res;
}
Run Code Online (Sandbox Code Playgroud)

该函数NormInv是Euan Dean在这篇文章中给出的C++实现.

以下显示了我得到的结果和理论结果,对于相同的值sigma1sigma2(分别为2.0和20.0)

http://s9.postimage.org/3xfdf8f8f/Results.jpg

我一直在使用不同的值试过sigma1sigma2,但他们都不工作.我也尝试过在高斯函数中做blur1=0,blur2=0但它也不起作用.

任何帮助,将不胜感激.提前致谢.

Abh*_*kur 9

在将图像转换为CV_8UC1之前,需要将图像标准化为0到255之间


Amr*_*mro 7

下面是我的实现(我用的sigma1=2,sigma2=20):

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;

int main(int argc, char** argv)
{
    Mat img, gray, float_gray, blur, num, den;

    // Load color image
    img = cv::imread("lena.png", 1);
    if( !img.data ) {
        return -1;
    }

    // convert to grayscale
    cv::cvtColor(img, gray, CV_BGR2GRAY);

    // convert to floating-point image
    gray.convertTo(float_gray, CV_32F, 1.0/255.0);

    // numerator = img - gauss_blur(img)
    cv::GaussianBlur(float_gray, blur, Size(0,0), 2, 2);
    num = float_gray - blur;

    // denominator = sqrt(gauss_blur(img^2))
    cv::GaussianBlur(num.mul(num), blur, Size(0,0), 20, 20);
    cv::pow(blur, 0.5, den);

    // output = numerator / denominator
    gray = num / den;

    // normalize output into [0,1]
    cv::normalize(gray, gray, 0.0, 1.0, NORM_MINMAX, -1);

    // Display
    namedWindow("demo", CV_WINDOW_AUTOSIZE );
    imshow("demo", gray);

    waitKey(0);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

结果如预期:

normalized_image

请注意,您可以将内核大小指定为Size(0,0),并将根据sigma值进行计算.