Local normalization in OpenCV

后端 未结 3 1823
迷失自我
迷失自我 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.

    0 讨论(0)
  • 2021-01-12 17:20

    you need to normalize the image between 0 and 255 before converting it to CV_8UC1

    0 讨论(0)
  • 2021-01-12 17:25

    This is the Python implementation of the same algo above:

    import cv2
    import numpy as np
    
    img = cv2.imread('/home/anmol/Downloads/lena.png')
    
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    float_gray = gray.astype(np.float32) / 255.0
    
    blur = cv2.GaussianBlur(float_gray, (0, 0), sigmaX=2, sigmaY=2)
    num = float_gray - blur
    
    blur = cv2.GaussianBlur(num*num, (0, 0), sigmaX=20, sigmaY=20)
    den = cv2.pow(blur, 0.5)
    
    gray = num / den
    
    gray = cv2.normalize(gray, dst=gray, alpha=0.0, beta=1.0, norm_type=cv2.NORM_MINMAX)
    
    cv2.imwrite("./debug.png", gray * 255)
    

    Outout:

    0 讨论(0)
提交回复
热议问题