1

I am trying to implement in python the same function than on this post: link

However, I can't get a smooth result, whatever I try, even though I tried both implementation, the corrected one of the OP and the second answer provided.

I am probably missing something small but I can't figure out what.

def local_norm(img, sigma_1, sigma_2):

    float_gray = img * cv2.CV_32FC1 / 255.0
    blur_1 = int(2 * np.ceil(- norm.ppf(0.05, loc=0, scale=sigma_1)) + 1)
    blurred_1 = cv2.GaussianBlur(float_gray, (blur_1, blur_1), sigma_1, sigma_1)
    temp_1 = float_gray - blurred_1

    temp_2 = cv2.pow(temp_1, 2.0)
    blur_2 = int(2 * np.ceil(- norm.ppf(0.05, loc=0, scale=sigma_2)) + 1)
    blurred_2 = cv2.GaussianBlur(temp_2, (blur_2, blur_2), sigma_2, sigma_2)
    temp_2 = cv2.pow(blurred_2, 0.5)

    float_gray = temp_1 / temp_2
    res = cv2.normalize(float_gray, 0, 255, cv2.NORM_MINMAX)
    res = res * cv2.CV_32S
    return res

I must precise that at the end, I use cv2.CV_32S because with the OP's encoding I end up with a black image. For the rest, I use the same sigma, 2.0 and 20.0.

enter image description here

Community
  • 1
  • 1
Yohan Obadia
  • 2,552
  • 2
  • 24
  • 31

1 Answers1

1

There were some errors in your implementation, So here is the correctly translated code in python:

import cv2
import numpy as np

img = cv2.imread('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

cv2.normalize(gray, dst=gray, alpha=0.0, beta=1.0, norm_type=cv2.NORM_MINMAX)

cv2.imwrite("./debug.png", gray * 255)

Output:

enter image description here

ZdaR
  • 22,343
  • 7
  • 66
  • 87