图像去雾python代码
时间: 2023-11-09 19:03:23 浏览: 164
以下是一个简单的图像去雾的 Python 代码示例:
```python
import cv2
import numpy as np
def dehaze(img, t=0.1, w=0.95):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.medianBlur(gray, 3)
dcp = cv2.divide(gray, img[:,:,0] + 1.0)
I = cv2.pow(dcp, t)
mean_I = cv2.blur(I, (15, 15))
q = np.percentile(mean_I, 100*w)
max_I = np.max(mean_I)
A = (max_I - mean_I)/(max_I - q)
A = np.uint8(cv2.normalize(A, None, 0, 255, cv2.NORM_MINMAX))
B = np.zeros_like(img)
for i in range(3):
B[:,:,i] = cv2.divide((img[:,:,i] - A), I) * 255
return B
# 测试代码
img = cv2.imread('input.jpg')
result = dehaze(img)
cv2.imwrite('output.jpg', result)
```
相关问题
aod图像去雾python代码
好的,以下是 aod 图像去雾的 Python 代码:
```python
import cv2
import numpy as np
def AOD(image, omega=0.95, lambda_=1.0, L=128):
# 将图像转换为灰度图
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 计算导向场
guide = cv2.ximgproc.guidedFilter(gray, gray, radius=15, eps=0.001)
# 计算梯度场
gradient = np.gradient(guide)
gradient_norm = np.sqrt(gradient[0]**2 + gradient[1]**2)
# 计算大气光
atm_light = np.percentile(image, L)
# 计算透射率
trans_map = 1 - omega * np.minimum(gradient_norm / np.mean(gradient_norm), 1.0)
# 求解场
J = (image - atm_light) / trans_map + atm_light
# 对 J 进行导向滤波
J_filtered = cv2.ximgproc.guidedFilter(guide, J, radius=15, eps=lambda_)
return J_filtered.astype(np.uint8)
# 读取图像
image = cv2.imread('input.jpg')
# 去雾处理
result = AOD(image)
# 显示结果
cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
深度学习图像去雾python代码
以下是使用深度学习进行图像去雾的Python代码示例,使用的是AOD-Net去雾网络的PyTorch实现:
```python
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from PIL import Image
# 定义AOD-Net去雾网络
class AODNet(nn.Module):
def __init__(self):
super(AODNet, self).__init__()
self.conv1 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(3)
self.relu1 = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(3)
self.relu2 = nn.ReLU(inplace=True)
self.conv3 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn3 = nn.BatchNorm2d(3)
self.relu3 = nn.ReLU(inplace=True)
self.conv4 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn4 = nn.BatchNorm2d(3)
self.relu4 = nn.ReLU(inplace=True)
self.conv5 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn5 = nn.BatchNorm2d(3)
self.relu5 = nn.ReLU(inplace=True)
self.conv6 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn6 = nn.BatchNorm2d(3)
self.relu6 = nn.ReLU(inplace=True)
self.conv7 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn7 = nn.BatchNorm2d(3)
self.relu7 = nn.ReLU(inplace=True)
self.conv8 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn8 = nn.BatchNorm2d(3)
self.relu8 = nn.ReLU(inplace=True)
self.conv9 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn9 = nn.BatchNorm2d(3)
self.relu9 = nn.ReLU(inplace=True)
self.conv10 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn10 = nn.BatchNorm2d(3)
self.relu10 = nn.ReLU(inplace=True)
self.conv11 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn11 = nn.BatchNorm2d(3)
self.relu11 = nn.ReLU(inplace=True)
self.conv12 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn12 = nn.BatchNorm2d(3)
self.relu12 = nn.ReLU(inplace=True)
self.conv13 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn13 = nn.BatchNorm2d(3)
self.relu13 = nn.ReLU(inplace=True)
self.conv14 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn14 = nn.BatchNorm2d(3)
self.relu14 = nn.ReLU(inplace=True)
self.conv15 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn15 = nn.BatchNorm2d(3)
self.relu15 = nn.ReLU(inplace=True)
self.conv16 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn16 = nn.BatchNorm2d(3)
self.relu16 = nn.ReLU(inplace=True)
self.conv17 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn17 = nn.BatchNorm2d(3)
self.relu17 = nn.ReLU(inplace=True)
self.conv18 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn18 = nn.BatchNorm2d(3)
self.relu18 = nn.ReLU(inplace=True)
self.conv19 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn19 = nn.BatchNorm2d(3)
self.relu19 = nn.ReLU(inplace=True)
self.conv20 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn20 = nn.BatchNorm2d(3)
self.relu20 = nn.ReLU(inplace=True)
self.conv21 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn21 = nn.BatchNorm2d(3)
self.relu21 = nn.ReLU(inplace=True)
self.conv22 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn22 = nn.BatchNorm2d(3)
self.relu22 = nn.ReLU(inplace=True)
self.conv23 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn23 = nn.BatchNorm2d(3)
self.relu23 = nn.ReLU(inplace=True)
self.conv24 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn24 = nn.BatchNorm2d(3)
self.relu24 = nn.ReLU(inplace=True)
self.conv25 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn25 = nn.BatchNorm2d(3)
self.relu25 = nn.ReLU(inplace=True)
self.conv26 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn26 = nn.BatchNorm2d(3)
self.relu26 = nn.ReLU(inplace=True)
self.conv27 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn27 = nn.BatchNorm2d(3)
self.relu27 = nn.ReLU(inplace=True)
self.conv28 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn28 = nn.BatchNorm2d(3)
self.relu28 = nn.ReLU(inplace=True)
self.conv29 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn29 = nn.BatchNorm2d(3)
self.relu29 = nn.ReLU(inplace=True)
self.conv30 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn30 = nn.BatchNorm2d(3)
self.relu30 = nn.ReLU(inplace=True)
self.conv31 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn31 = nn.BatchNorm2d(3)
self.relu31 = nn.ReLU(inplace=True)
self.conv32 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn32 = nn.BatchNorm2d(3)
self.relu32 = nn.ReLU(inplace=True)
self.conv33 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn33 = nn.BatchNorm2d(3)
self.relu33 = nn.ReLU(inplace=True)
self.conv34 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn34 = nn.BatchNorm2d(3)
self.relu34 = nn.ReLU(inplace=True)
self.conv35 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn35 = nn.BatchNorm2d(3)
self.relu35 = nn.ReLU(inplace=True)
self.conv36 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn36 = nn.BatchNorm2d(3)
self.relu36 = nn.ReLU(inplace=True)
self.conv37 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn37 = nn.BatchNorm2d(3)
self.relu37 = nn.ReLU(inplace=True)
self.conv38 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn38 = nn.BatchNorm2d(3)
self.relu38 = nn.ReLU(inplace=True)
self.conv39 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn39 = nn.BatchNorm2d(3)
self.relu39 = nn.ReLU(inplace=True)
self.conv40 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn40 = nn.BatchNorm2d(3)
self.relu40 = nn.ReLU(inplace=True)
self.conv41 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn41 = nn.BatchNorm2d(3)
self.relu41 = nn.ReLU(inplace=True)
self.conv42 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn42 = nn.BatchNorm2d(3)
self.relu42 = nn.ReLU(inplace=True)
self.conv43 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn43 = nn.BatchNorm2d(3)
self.relu43 = nn.ReLU(inplace=True)
self.conv44 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn44 = nn.BatchNorm2d(3)
self.relu44 = nn.ReLU(inplace=True)
self.conv45 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn45 = nn.BatchNorm2d(3)
self.relu45 = nn.ReLU(inplace=True)
self.conv46 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn46 = nn.BatchNorm2d(3)
self.relu46 = nn.ReLU(inplace=True)
self.conv47 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn47 = nn.BatchNorm2d(3)
self.relu47 = nn.ReLU(inplace=True)
self.conv48 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn48 = nn.BatchNorm2d(3)
self.relu48 = nn.ReLU(inplace=True)
self.conv49 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn49 = nn.BatchNorm2d(3)
self.relu49 = nn.ReLU(inplace=True)
self.conv50 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn50 = nn.BatchNorm2d(3)
self.relu50 = nn.ReLU(inplace=True)
self.conv51 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn51 = nn.BatchNorm2d(3)
self.relu51 = nn.ReLU(inplace=True)
self.conv52 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn52 = nn.BatchNorm2d(3)
self.relu52 = nn.ReLU(inplace=True)
self.conv53 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn53 = nn.BatchNorm2d(3)
self.relu53 = nn.ReLU(inplace=True)
self.conv54 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn54 = nn.BatchNorm2d(3)
self.relu54 = nn.ReLU(inplace=True)
self.conv55 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn55 = nn.BatchNorm2d(3)
self.relu55 = nn.ReLU(inplace=True)
self.conv56 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
self.bn56 = nn.BatchNorm2d(3)
self.relu56 = nn.ReLU(inplace=True)
self.conv57 = nn.Conv2d(3, 3, kernel_size=3, stride=1, padding=1, bias=False)
阅读全文