Local normalization in OpenCV

后端 未结 3 1856
迷失自我
迷失自我 2021-01-12 16:39

I\'m trying to implement in OpenCV a local normalization algorithm to reduce the difference of illumination in an image. I have found a MATLAB function, and I have implement

3条回答
  •  天命终不由人
    2021-01-12 17:06

    Here is my implementation (I am using 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;
    }
    

    The result as expected:

    normalized_image

    Note that you can specify the kernel size as Size(0,0) and it will be computed from the sigma values.

提交回复
热议问题