Python 图像去雾处理代码示例
下面是一个简单的 Python 代码示例,用于图像去雾处理:
import cv2
import numpy as np
def dehaze(image, t=0.95, w=0.7):
# 计算图像的暗通道
dark_channel = np.min(image, axis=2)
# 使用一个较小的窗口对暗通道进行均值滤波
kernel_size = 15
dark_channel_filtered = cv2.blur(dark_channel, (kernel_size, kernel_size))
# 估计大气光值
num_pixels = image.shape[0] * image.shape[1]
num_brightest_pixels = int(num_pixels * (1 - t))
flat_dark_channel = dark_channel_filtered.flatten()
indices = np.argsort(flat_dark_channel)[::-1][:num_brightest_pixels]
brightest_pixels = image.reshape(num_pixels, 3)[indices]
atmosphere = np.max(brightest_pixels, axis=0)
# 估计透射率
epsilon = 0.001
transmission = 1 - w * (dark_channel / atmosphere)
# 限制透射率的范围
transmission = np.clip(transmission, epsilon, 1)
# 恢复原始图像
recovered = np.zeros_like(image)
for i in range(3):
recovered[:,:,i] = (image[:,:,i] - atmosphere[i]) / transmission + atmosphere[i]
recovered = np.clip(recovered, 0, 255).astype(np.uint8)
return recovered
# 读取图像
image = cv2.imread('input.jpg')
# 进行去雾处理
dehazed_image = dehaze(image)
# 显示原始图像和去雾处理后的图像
cv2.imshow('Original', image)
cv2.imshow('Dehazed', dehazed_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
请确保已经安装了 OpenCV 库,然后将输入图像命名为'input.jpg',并将其与 Python 代码放在同一目录下运行。代码将显示原始图像和去雾处理后的图像。
原文地址: https://www.cveoy.top/t/topic/cbYG 著作权归作者所有。请勿转载和采集!