matlab暗通道去雾算法
时间: 2023-12-12 14:36:03 浏览: 96
基于暗通道和RETINEX算法的图像去雾matlab仿真,带GUI界面+matlab操作视频
5星 · 资源好评率100%
以下是matlab实现的暗通道去雾算法的代码和步骤:
1.读取待处理的有雾图像
```matlab
I = imread('hazy_image.jpg');
```
2.计算暗通道图像
```matlab
patch_size = 15; % 定义块大小
dark_channel = min(I, [], 3); % 计算RGB三个道的最小值
dark_channel = ordfilt2(dark_channel, 1, ones(patch_size)); % 对最小值图像进行滤波
```
3.计算大气光值
```matlab
top_percent = 0.001; % 定义前0.1%的亮度值为大气光值
[h, w] = size(dark_channel);
num_pixel = h * w;
num_top_pixel = round(num_pixel * top_percent);
dark_channel_vector = reshape(dark_channel, num_pixel, 1);
[~, indices] = sort(dark_channel_vector, 'descend');
top_indices = indices(1:num_top_pixel);
atmospheric_light = zeros(1, 3);
for i = 1:3
channel = I(:, :, i);
atmospheric_light(i) = max(channel(top_indices));
end
```
4.计算透射率
```matlab
omega = 0.95; % 定义透射率的权重
transmission = 1 - omega * (min(I ./ atmospheric_light, [], 3));
```
5.恢复无雾图像
```matlab
t0 = 0.1; % 定义透射率的最小值
J = zeros(size(I));
for i = 1:3
channel = double(I(:, :, i)) ./ 255;
t = max(transmission, t0);
J(:, :, i) = (channel - atmospheric_light(i)) ./ t + atmospheric_light(i);
end
J(J > 1) = 1; % 防止像素值超过1
J(J < 0) = 0; % 防止像素值小于0
J = uint8(J * 255); % 将像素值转换为0-255的整数
```
阅读全文