Image Dehazing Using MATLAB
Image Dehazing – Retinex Algorithm
1. Tutorial Introduction
In the field of computer vision, image dehazing is an important preprocessing task, especially in autonomous driving, remote sensing image analysis and monitoring systems. Dehazing can effectively improve image quality and make the target more clearly visible.This project uses the Retinex algorithm for image dehazing and combines it with GPU acceleration to improve computational efficiency.
This tutorial uses MATLAB as the software image and RTX 4090 as the computing resource.
2. Theoretical basis
Retinex Algorithm Principle
Retinex (Retina + Cortex) is an image enhancement algorithm proposed by Land and McCann, which is mainly based on the adaptability of the human visual system to brightness changes. The core idea of the Retinex algorithm is:
- Decomposing reflections and lighting:The image I(x, y) consists of the illumination L(x, y) and the reflectivity R(x, y), that is: I(x, y) = R(x, y) * L(x, y)
- Logarithmic transformation: To eliminate the effect of illumination, take the logarithm: log I(x, y) = log R(x, y) + log L(x, y)
- Smooth Lighting: Use Gaussian filter G(x, y) to estimate the illumination component: L'(x, y) = G(x, y) * I(x, y)
- Calculate Retinex Output: R'(x, y) = log I(x, y) – log L'(x, y) Through this method, the contrast of the image can be enhanced while reducing the impact of lighting.
3. Operation steps
1. Preparation for operation
After cloning and successfully starting the container, open the workspace.
- The software image is MATLAB, the computing resource is RTX 4090, and the access method is "workspace".

2. Loading the image
Find "Matlab" in the "Console" of the "Workspace" and enter the following code:
(This tutorial has provided a sample image "1.png" in the home directory, just enter the code)
f = imread('1.png');
imshow(f);
title('原始图像');


3. Run the dehazing algorithm
Code implementation (the RemoveFogByRetinex.m code file has been prepared and saved in the home directory, and can be used and run directly in the next step)
function In = RemoveFogByRetinex_GPU(f, flag)
% 启用 CUDA 前向兼容性
parallel.gpu.enableCUDAForwardCompatibility(true);
if nargin < 2
flag = 1;
end
% 转换到 GPU
f = gpuArray(im2double(f));
% 拆分颜色通道
fr = f(:, :, 1);
fg = f(:, :, 2);
fb = f(:, :, 3);
% 归一化
mr = mat2gray(fr);
mg = mat2gray(fg);
mb = mat2gray(fb);
% 设置滤波参数
alpha = 200;
n = floor(min([size(f, 1) size(f, 2)]) * 0.5);
n1 = floor((n + 1) / 2);
% 创建滤波核
[X, Y] = meshgrid(1:n, 1:n);
b = exp(-((X - n1).^2 + (Y - n1).^2) / (4 * alpha)) / (pi * alpha);
b = gpuArray(b);
% 进行滤波
nr1 = imfilter(mr, b, 'conv', 'replicate');
ng1 = imfilter(mg, b, 'conv', 'replicate');
nb1 = imfilter(mb, b, 'conv', 'replicate');
% 计算 Retinex 公式(避免 log(0) 问题)
ur1 = log(max(nr1, 0.01));
ug1 = log(max(ng1, 0.01));
ub1 = log(max(nb1, 0.01));
tr1 = log(max(mr, 0.01));
tg1 = log(max(mg, 0.01));
tb1 = log(max(mb, 0.01));
% 计算 Retinex 输出
yr1 = tr1 - ur1;
yg1 = tg1 - ug1;
yb1 = tb1 - ub1;
% 归一化(手动调整范围)
min_val = min([min(yr1(:)), min(yg1(:)), min(yb1(:))]);
max_val = max([max(yr1(:)), max(yg1(:)), max(yb1(:))]);
yr1 = (yr1 - min_val) / (max_val - min_val);
yg1 = (yg1 - min_val) / (max_val - min_val);
yb1 = (yb1 - min_val) / (max_val - min_val);
% 转换到 uint8
cr = gather(im2uint8(yr1));
cg = gather(im2uint8(yg1));
cb = gather(im2uint8(yb1));
% 合并通道
In = cat(3, cr, cg, cb);
% 显示结果
if flag
figure;
subplot(2, 2, 1);
imshow(gather(f)); title('原图像', 'FontWeight', 'Bold');
subplot(2, 2, 2);
imshow(In); title('处理后的图像', 'FontWeight', 'Bold');
Q = rgb2gray(gather(f));
M = rgb2gray(In);
subplot(2, 2, 3);
imhist(Q, 64); title('原灰度直方图', 'FontWeight', 'Bold');
subplot(2, 2, 4);
imhist(M, 64); title('处理后的灰度直方图', 'FontWeight', 'Bold');
end
end
4. Run the dehazing process
Continue to enter the following command in the "Matlab" console to run the dehazing process:
In = RemoveFogByRetinex(f, 1);

5. Further enhance the contrast
Continue to enter the following command in the Matlab console to further enhance the contrast:
lab = rgb2lab(In);
L = lab(:, :, 1) / 100;
L = adapthisteq(L, 'ClipLimit', 0.02, 'Distribution', 'rayleigh');
lab(:, :, 1) = L * 100;
In = lab2rgb(lab);
In = imadjust(In, stretchlim(In, [0.01, 0.99]), []);
In = imsharpen(In, 'Radius', 2, 'Amount', 1.5);
imshow(In);
