作者的原始实现(C++)可以找到here:见GenerateIntensityNormalizedDatabase()。
这已被另一位 Python 学生重新实现。 python的实现是:
import cv2
import numpy as np
def StdDev(img, meanPoint, point, kSize):
kSizeX, kSizeY = kSize / 2, kSize / 2
ystart = point[1] - kSizeY if 0 < point[1] - kSizeY < img.shape[0] else 0
yend = point[1] + kSizeY + 1 if 0 < point[1] + kSizeY + 1 < img.shape[0] else img.shape[0] - 1
xstart = point[0] - kSizeX if 0 < point[0] - kSizeX < img.shape[1] else 0
xend = point[0] + kSizeX + 1 if 0 < point[0] + kSizeX + 1 < img.shape[1] else img.shape[1] - 1
patch = (img[ystart:yend, xstart:xend] - meanPoint) ** 2
total = np.sum(patch)
n = patch.size
return 1 if total == 0 or n == 0 else np.sqrt(total / float(n))
def IntensityNormalization(img, kSize):
blur = cv2.GaussianBlur(img, (kSize, kSize), 0, 0).astype(np.float64)
newImg = np.ones(img.shape, dtype=np.float64) * 127
for x in range(img.shape[1]):
for y in range(img.shape[0]):
original = img[y, x]
gauss = blur[y, x]
desvio = StdDev(img, gauss, [x, y], kSize)
novoPixel = 127
if desvio > 0:
novoPixel = (original - gauss) / float(desvio)
newVal = np.clip((novoPixel * 127 / float(2.0)) + 127, 0, 255)
newImg[y, x] = newVal
return newImg
要使用强度归一化,您可以这样做:
kSize = 7
img = cv2.imread('{IMG_FILENAME}', cv2.IMREAD_GRAYSCALE).astype(np.float64)
out = IntensityNormalization(img, kSize)
要可视化生成的图像,请不要忘记将 out 转换回 np.uint8 (why?)。如果你想重现他的结果,我建议你使用 C++ 中的原始实现。
免责声明:我来自此paper 的作者的同一lab。