gpt4 book ai didi

python - 删除彩色图像的晕影滤镜

转载 作者:行者123 更新时间:2023-12-02 16:08:00 25 4
gpt4 key购买 nike

我是 Python OpenCV 图像处理的新手。我想删除图像的边框/轮廓阴影,如下所示。我检查了' how to remove shadow from scanned images '这对我不起作用。这甚至可能吗?

Shadow image example

最佳答案

你的边框/轮廓阴影问题让我想起了小插图过滤器。你可以看看这个question如果你想了解更多。所以基本上我们的任务是消除晕影滤镜的影响,然后增加亮度。

#####VIGNETTE
import cv2
import numpy as np

img = cv2.imread('Paris.jpg')
height, width = img.shape[:2]
original = img.copy()
# generating vignette mask using Gaussian kernels
kernel_x = cv2.getGaussianKernel(width, 150)
kernel_y = cv2.getGaussianKernel(height, 150)
kernel = kernel_y * kernel_x.T
mask = 255 * kernel / np.linalg.norm(kernel)

# applying the mask to each channel in the input image
for i in range(3):
img[:, :, i] = img[:, :, i] * mask


cv2.imshow('Original', original)
cv2.imshow('Vignette', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

对抗效果变化 img[:, :, i] = img[:, :, i] * maskimg[:, :, i] = img[:, :, i] / mask
Result

现在我们需要增加图像的亮度。为此,我们将图像转换为 HSV 并增加饱和度和值矩阵的值。要更详细地了解它,您可以引用此 article .
#THE FULL CODE
import cv2
import numpy as np

img = cv2.imread('shadow.jpg')
original = cv2.imread('bright.jpg')
height, width = img.shape[:2]
# generating vignette mask using Gaussian kernels
kernel_x = cv2.getGaussianKernel(width, 150)
kernel_y = cv2.getGaussianKernel(height, 150)
kernel = kernel_y * kernel_x.T
mask = 255 * kernel / np.linalg.norm(kernel)

test = img.copy()
for i in range(3):
test[:, :, i] = test[:, :, i] / mask

hsv = cv2.cvtColor(test, cv2.COLOR_BGR2HSV)
hsv = np.array(hsv, dtype = np.float64)
hsv[:,:,1] = hsv[:,:,1]*1.3 ## scale pixel values up or down for channel 1(Lightness)
hsv[:,:,1][hsv[:,:,1]>255] = 255
hsv[:,:,2] = hsv[:,:,2]*1.3 ## scale pixel values up or down for channel 1(Lightness)
hsv[:,:,2][hsv[:,:,2]>255] = 255
hsv = np.array(hsv, dtype = np.uint8)
test = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)


cv2.imshow('Original_bright', original)
cv2.imshow('Original_dark', img)
cv2.imshow('Result', test)
cv2.waitKey(0)
cv2.destroyAllWindows()

Result

结果与原始明亮图像进行了比较。

Result without vignette

如果没有反向小插图过滤器,结果会是什么样子。

关于python - 删除彩色图像的晕影滤镜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62080675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com