matlab 暗通道图像去雾代码
时间: 2024-05-22 21:08:46 浏览: 141
Matlab中的暗通道先验去雾算法的代码如下:
```
function J = dark_channel(I, w)
% 计算暗通道图像
[m, n, ~] = size(I);
p = floor(w/2);
J = zeros(m, n);
for i = 1:m
for j = 1:n
patch = I(max(i-p,1):min(i+p,m), max(j-p,1):min(j+p,n), :);
J(i,j) = min(min(patch(:,:,1)));
end
end
function [t, A] = atm_light(I, J, p)
% 估计大气光A和全局大气透射率t
[m, n] = size(J);
vec_J = reshape(J, m*n, 1);
vec_I = reshape(I, m*n, 3);
[~, indices] = sort(vec_J, 'descend');
indices = indices(1:floor(p*m*n));
A = mean(vec_I(indices, :), 1);
Y = zeros(length(indices), 3);
for k = 1:length(indices)
Y(k,:) = log(vec_I(indices(k),:)) - log(A);
end
[V, ~] = eig(Y'*Y);
t = exp(Y*V(:,1));
t = reshape(t, m, n);
function J_dark = dehaze(I, omega, w)
% 暗通道先验去雾算法
I = im2double(I);
J_dark = dark_channel(I, w);
[t, A] = atm_light(I, J_dark, omega);
J_dark = (I - repmat(A, size(I,1), size(I,2)))./repmat(t, [1 1 3]) + repmat(A, size(I,1), size(I,2));
```
其中,dark_channel函数用于计算暗通道图像,atm_light函数用于估计全局大气光和大气透射率,dehaze函数是主要的去雾函数,通过输入一张待处理的图像和两个参数omega和w来输出一张去雾后的图像。
阅读全文