简体   繁体   English

使用 python opencv 进行照明标准化

[英]illumination normalization using python opencv

I am doing a project in image classification with deep learning, and want to implement the illumination normalization which provided in section 4.4 of paper https://arxiv.org/pdf/1907.09449.pdf , but I didn't get the expected results.我正在做一个带有深度学习的图像分类项目,想实现论文https://arxiv.org/pdf/1907.09449.pdf的第 4.4 节中提供的光照归一化,但我没有得到预期的结果。

In section 4.4 of paper, it using gaussian kerenl filter to get the background image, then subtract it from Y channel in YCrCb color space, I try to reproduce the result but is not the same as paper do in Figure 4在论文的 4.4 节中,它使用 gaussian kerenl filter 得到背景图像,然后从 YCrCb 颜色空间中的 Y 通道中减去它,我尝试重现结果但与图 4 中的论文不一样

below is my code下面是我的代码

image = cv2.imread(file)
cv2.imshow("origin", image)

# illumination normalize
ycrcb = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)
y, cr, cb = cv2.split(ycrcb)

# get background which paper says (gaussian method using standard deviation 5 pixel)
gaussian = cv2.GaussianBlur(y, (0, 0), 5, 5)

# subtract background from Y channel which paper says
y = y - gaussian
ycrcb = cv2.merge([y, cr, cb])

output = cv2.cvtColor(ycrcb, cv2.COLOR_YCrCb2BGR)
cv2.imshow("output", output)

but the output result image (see below) is not the same as paper says (Figure 4)但 output 结果图像(见下文)与纸上说的不一样(图 4)

original image -> illumination normalized image原始图像->照明归一化图像

does anyone knows how to do that?有谁知道该怎么做? thank you for your greate help:-)感谢您的大力帮助:-)

You have two issues.你有两个问题。 First, you need to adjust the sigma for the Gaussian blur in proportion to the image size vs the 299 pixel size used in the reference.首先,您需要根据图像大小与参考中使用的 299 像素大小成比例调整高斯模糊的 sigma。 Second you need to bias the difference near mid-gray.其次,您需要将差异偏向中灰色附近。 Adjust the bias as desired for brightness.根据需要调整亮度偏差。

So here is how I achieved it in Python/OpenCV.所以这就是我在 Python/OpenCV 中实现它的方法。

Input:输入:

在此处输入图像描述

import cv2
import numpy as np

# read input
image = cv2.imread('retina2.jpg')
hh, ww = image.shape[:2]
print(hh, ww)
max = max(hh, ww)

# illumination normalize
ycrcb = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)

# separate channels
y, cr, cb = cv2.split(ycrcb)

# get background which paper says (gaussian blur using standard deviation 5 pixel for 300x300 size image)
# account for size of input vs 300
sigma = int(5 * max / 300)
print('sigma: ',sigma)
gaussian = cv2.GaussianBlur(y, (0, 0), sigma, sigma)

# subtract background from Y channel
y = (y - gaussian + 100)

# merge channels back
ycrcb = cv2.merge([y, cr, cb])

#convert to BGR
output = cv2.cvtColor(ycrcb, cv2.COLOR_YCrCb2BGR)

# save results
cv2.imwrite('retina2_proc.jpg', output)

# show results
cv2.imshow("output", output)
cv2.waitKey(0)


Results:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM