一、简介

1 暗通道先验图像去雾方法

1.1 光线透射率模型

光在传播中由于散射使得从光源发出的辐射只有部分能到达接收传感器,其他则被散射到传播介质中。假设距离较小时散射光强与距离是线性关系,当光源距离传感器无限接近时,光的衰减值可近似为:Br,其中β为空气的散射系数;r为光源与传感器间的距离。大气密度均匀时,光线透射率的数学模型为:

【图像增强】基于matlab GUI暗通道图像去雾【含Matlab源码 835期】_matlab图像处理

式中:D为场景深度;t为光线透射率,用于量化传感器接收光强与光源表面光强间的比例关系,即没有被散射的辐射与光源辐射间的比例关系。

1.2 暗通道先验理论

基于统计大量清晰图像得到的暗通道先验理论是指大部分不含天空的优质图像的所有像素在R,G,B这3个通道中最少存在一个颜色通道灰度值相当低以至趋近于0[5],也就是在一定的微小区域里最小辐射强度值极低。一幅图像J可定义为:

【图像增强】基于matlab GUI暗通道图像去雾【含Matlab源码 835期】_图像去雾_02

式中:J dark为图像J的暗通道值; J为图像J的c通道灰度值; Q(x) 为以像素x为中心的局部微小区域; y为区域内任一像素。

1.3 暗通道先验图像去雾处理

图像去雾的目标是将传感器接收到的有雾图像利用获得的有用信息通过去雾还原出清晰图像。暗通道先验图像去雾处理是根据暗通道原理获取先验知识,再利用有雾图像退化模型实现图像去雾效果。

1.3.1 图像退化模型

在计算机视觉图形学领域,有雾图像的退化模型为[6]:

【图像增强】基于matlab GUI暗通道图像去雾【含Matlab源码 835期】_图像去雾_03

式中:l(x)为传感器接收到的场景信号,即输入的有雾影像;场景辐射J(x)为信号处理后的清晰图像;A为环境光照强度;t(x)为大气透射率。J(x)t(x)称为直接衰减项,用来量化场景辐射和传播中的信号损失。A(1-t(x))表示图像接收到的大气散射光强,它是引起色彩偏移和云雾效果的直接原因。

1.3.2 估算环境光照强度

暗通道图像中灰度值越高的区域云雾越厚,在输入图像中位于这部分区域的]像素灰度值越接近于环境光照强度。估算环境光照强度首先需要找到暗通道图像中灰度值最高并占图像总像素数量01%的像素点,记录它们对应的坐标索引,然后根据坐标索引在输入的有雾图像中找到对应像素点,计算有雾图像中对应像素点的灰度平均值作为环境光照强度A。

1.3.3估算大气透射率

使用t’(x)表示以像素x为中心的滤波窗口内的大气透射率,假设它局部不变对式(3)最小值运算,分别计算R,G,B这3个颜色通道中的最小值,即

【图像增强】基于matlab GUI暗通道图像去雾【含Matlab源码 835期】_matlab图像处理_04

将3个颜色通道的最小值进行运算,可得出以像素x为中心的滤波窗口内的灰度最小值,即

【图像增强】基于matlab GUI暗通道图像去雾【含Matlab源码 835期】_matlab图像处理_05

根据暗通道先验原理和式(2)可以得出:

【图像增强】基于matlab GUI暗通道图像去雾【含Matlab源码 835期】_灰度值_06

于是,由式(5)和式(6)计算出大气透射率为:

【图像增强】基于matlab GUI暗通道图像去雾【含Matlab源码 835期】_图像去雾_07

1.3.4 去雾处理

暗通道先验条件可用来量化云雾厚度和全部像素的辐射还原量,然后恢复出清晰优质的图像。通过式(3)的有雾图像退化模型和环境光照强度A、大气透射率t(x),可进行单幅图像去雾处理,即

【图像增强】基于matlab GUI暗通道图像去雾【含Matlab源码 835期】_matlab图像处理_08

2 暗通道先验算法去雾效果优化

本文优化流程如图1所示。当输入图像云雾不均时,其大气透射率图层的灰度信息仅处于有限范围内,而整张图像的辐射还原量将被限制在一定区间里,不能分别准确还原云雾厚薄区域的辐射强度,从而影响图像去雾效果,考虑利用拉伸大气透射率图层对比度的方法来改善云雾厚薄不均时基于暗通道先验得出的无雾图像质量。

【图像增强】基于matlab GUI暗通道图像去雾【含Matlab源码 835期】_图像去雾_09

图1 基于暗通道先验的图像去雾处理效果优化流程

二、部分源代码

function varargout = DIPGUI(varargin)
% DIPGUI MATLAB code for DIPGUI.fig
% DIPGUI, by itself, creates a new DIPGUI or raises the existing
% singleton*.
%
% H = DIPGUI returns the handle to a new DIPGUI or the handle to
% the existing singleton*.
%
% DIPGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in DIPGUI.M with the given input arguments.
%
% DIPGUI('Property','Value',...) creates a new DIPGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before DIPGUI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to DIPGUI_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help DIPGUI

% Last Modified by GUIDE v2.5 30-Nov-2018 12:21:38

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @DIPGUI_OpeningFcn, ...
'gui_OutputFcn', @DIPGUI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before DIPGUI is made visible.
function DIPGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to DIPGUI (see VARARGIN)

% Choose default command line output for DIPGUI
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes DIPGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = DIPGUI_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in getpic.
function getpic_Callback(hObject, eventdata, handles)
% hObject handle to getpic (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global I
[filename,pathname]=...
uigetfile({'*.*';'*.bmp';'*.tif';'*.png'},'select picture'); %选择图片路径
str=[pathname filename]; %合成路径+文件名
I=imread(str);
axes(handles.yuantu); %使用第一个axes
imshow(I); %显示图片


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)


% --- Executes on button press in savepic.
function savepic_Callback(hObject, eventdata, handles)
% hObject handle to savepic (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

[FileName,PathName] = uiputfile({'*.jpg','JPEG(*.jpg)';...
'*.bmp','Bitmap(*.bmp)';...
'*.gif','GIF(*.gif)';...
'*.*', 'All Files (*.*)'},...
'Save Picture','Untitled');
if FileName==0
return;
else
h=getframe(handles.chulihou);
imwrite(h.cdata,[PathName,FileName]);
end

% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton1


% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton2


% --- Executes on button press in radiobutton3.
function radiobutton3_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton3


% --- Executes on button press in radiobutton4.
function radiobutton4_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton4


% --- Executes on button press in radiobutton5.
function radiobutton5_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton5


% --- Executes during object creation, after setting all properties.
function uibuttongroup2_CreateFcn(hObject, eventdata, handles)
% hObject handle to uibuttongroup2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called


% --- Executes when selected object is changed in uibuttongroup2.
function uibuttongroup2_SelectionChangedFcn(hObject, eventdata, handles)
% hObject handle to the selected object in uibuttongroup2
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global I

global BW %定义全局变量

str=get(hObject,'string'); %拿到所选按钮的名称

axes(handles.chulihou); %使用第二个axes

switch str %选择语句

case'Initial' %点击原图按钮

imshow(I); %显示原图

function imDst = boxfilter(imSrc, r)

% BOXFILTER O(1) time box filtering using cumulative sum
%
% - Definition imDst(x, y)=sum(sum(imSrc(x-r:x+r,y-r:y+r)));
% - Running time independent of r;
% - Equivalent to the function: colfilt(imSrc, [2*r+1, 2*r+1], 'sliding', @sum);
% - But much faster.

[hei, wid] = size(imSrc);
imDst = zeros(size(imSrc));

%cumulative sum over Y axis
imCum = cumsum(imSrc, 1);
%difference over Y axis
imDst(1:r+1, :) = imCum(1+r:2*r+1, :);
imDst(r+2:hei-r, :) = imCum(2*r+2:hei, :) - imCum(1:hei-2*r-1, :);
imDst(hei-r+1:hei, :) = repmat(imCum(hei, :), [r, 1]) - imCum(hei-2*r:hei-r-1, :);

三、运行结果

【图像增强】基于matlab GUI暗通道图像去雾【含Matlab源码 835期】_图像去雾_10

四、matlab版本及参考文献

1 matlab版本

2014a

2 参考文献

[1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020.

[2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社,2013.

[3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社,2013.

[4]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015.