AOD-NET 去雾网络结合 ACE 自适应对比度增强代码示例
以下是 AOD-NET 去雾网络与 ACE 自适应对比度增强结合的示例代码:
import cv2
import numpy as np
# Load AOD-NET model
aod_net = cv2.dnn.readNetFromTensorflow('aod_net.pb')
# Load ACE model
ace_model = cv2.createTonemapDurand(gamma=2.2)
# Load input image
image = cv2.imread('input.jpg')
# Preprocess input image for AOD-NET
blob = cv2.dnn.blobFromImage(image, scalefactor=1.0, size=(512, 512), mean=(0, 0, 0), swapRB=True, crop=False)
# Pass the input image through AOD-NET
aod_net.setInput(blob)
aod_net_output = aod_net.forward()
# Extract the transmission map from AOD-NET output
transmission_map = aod_net_output[0, 0, :, :]
# Apply ACE contrast enhancement to transmission map
enhanced_transmission_map = ace_model.process(transmission_map)
# Normalize enhanced transmission map to [0, 1]
enhanced_transmission_map = cv2.normalize(enhanced_transmission_map, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
# Calculate atmospheric light
atmospheric_light = np.max(image, axis=(0, 1))
# Calculate refined transmission map
refined_transmission_map = np.power(enhanced_transmission_map, 0.8)
# Calculate haze-free image
haze_free_image = np.zeros_like(image, dtype=np.float32)
for c in range(3):
haze_free_image[:, :, c] = (image[:, :, c].astype(np.float32) - atmospheric_light[c]) / refined_transmission_map + atmospheric_light[c]
# Clamp pixel values to [0, 255]
haze_free_image = np.clip(haze_free_image, 0, 255).astype(np.uint8)
# Display and save result
cv2.imshow('Haze-Free Image', haze_free_image)
cv2.imwrite('output.jpg', haze_free_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
请注意,上述代码中的'aod_net.pb'是已经训练好的 AOD-NET 模型文件,可以从相关资源中获取。同样,ACE 模型需要事先准备好,可以使用 OpenCV 中的'createTonemapDurand' 函数创建。
此代码将输入图像传递给 AOD-NET 网络,然后提取传输图并将其传递给 ACE 对比度增强模型。然后,计算大气光和细化的传输图,并使用它们来生成去雾图像。最后,结果图像将显示并保存为'output.jpg'。
原文地址: https://www.cveoy.top/t/topic/pmy9 著作权归作者所有。请勿转载和采集!