二、数字图像处理简介

基于matlab GUI数字图像处理:灰度化、二值化、中值滤波、低通滤波、均值滤波、高斯滤波、直方图、腐蚀、canny、sobel。

1、灰度化:提取jpg图像各个像素点的R、G、B三个类型的值,再对其进行加权平均。最后得到一个通道红绿蓝三个类型的加权平均。

公式为:ima=0.299ima_red+0.587ima_green+0.114*ima_blue。

2、二值化:二值化是在图像灰度化的基础上进行的,通过比较图像每个像素点的灰度值与阈值的大小关系来设置该点为0或1。

3、中值滤波:基于排序统计理论的一种能有效抑制噪声的非线性平滑滤波信号处理技术。中值滤波的特点即是首先确定一个以某个像素为中心点的邻域,一般为方形邻域,也可以为圆形、十字形等等,然后将邻域中各像素的灰度值排序,取其中间值作为中心像素灰度的新值,这里领域被称为窗口,当窗口移动时,利用中值滤波可以对图像进行平滑处理。其算法简单,时间复杂度低,但其对点、线和尖顶多的图像不宜采用中值滤波。本系统中值滤波设置的核为长宽都是6的正方形,在此邻域内来找中值。

4、低通滤波:在低通滤波中,低频信号能正常通过,而超过设定临界值的高频信号则被阻隔、减弱。首先对图像进行傅里叶变换,接着将FFT的DC分量移到频谱中心,指定滤波器截止频率和滤波半径后,通过低通滤波转换函数与该频率进行比较d=sqrt((i-n1)2+(j-n2)2)。若信号大于该频率,则置为0。

5、均值滤波:均值滤波是典型的线性滤波算法,它是指在图像上对目标像素给一个滤波核,该核包括了其周围的临近像素,再用核中的全体像素的平均值来代替原来像素值。在本项目中,需先给出设计的正方形核的边长,之后便可以用指定大小的滤波核进行均值滤波。

6、高斯滤波:高斯滤波是一种线性平滑滤波,适用于消除高斯噪声,广泛应用于图像处理的减噪过程。高斯滤波是对整幅图像进行加权平均的过程,每一个像素点的值,都由其本身和邻域内的其他像素值经过加权平均后得到。高斯滤波的具体操作是:用一个模板(或称卷积、掩模)扫描图像中的每一个像素,用模板确定的邻域内像素的加权平均灰度值去替代模板中心像素点的值用。高斯平滑滤波器对于抑制服从正态分布的噪声非常有效。

7、形态学-腐蚀:一般腐蚀操作对二值图进行处理,腐蚀操作如左图,当像素点(i,j+1)和(i+1,j)为1时则(i,j)为1。腐蚀是一种消除边界点,使边界向内部收缩的过程。可以用来消除小且无意义的目标物。如果两目标物间有细小的连通,可以选取足够大的结构元素,将细小连通腐蚀掉。

8、直方图统计:灰度直方图是关于灰度级分布的函数,是对图像中灰度级分布的统计。灰度直方图是将数字图像中的所有像素,按照灰度值的大小,统计其出现的频率。图像直方图由于其计算代价较小,且具有图像平移、旋转、缩放不变性等众多优点,广泛地应用于图像处理的各个领域。

9、sobel:图像的边缘是灰度或结构等信息的突变处,边缘是一个区域的结束,也是另一个区域的开始,利用该特征可以分割图像。Sobel算子是一阶导数的边缘检测算子,在算法实现过程中,通过3×3模板作为核与图像中的每个像素点做卷积和运算,然后选取合适的阈值以提取边缘。由于只采用2个方向的模板。

10、canny:Canny检测主要分为四个步骤。首先为防止噪声干扰,先对图像进行去噪。接着可按照sobel的方式来计算图像梯度的幅值和方向。由于梯度大的不一定就是边缘,可以通过每个像素点的方向来确定该像素点在此邻域范围内是否是最大值,若是,则保留该像素点,不是则舍弃。最后用双阈值法再剔除一部分像素点并连接边缘。

三、源代码

function varargout = image_processing(varargin)
% IMAGE_PROCESSING MATLAB code for image_processing.fig
% IMAGE_PROCESSING, by itself, creates a new IMAGE_PROCESSING or raises the existing
% singleton*.
%
% H = IMAGE_PROCESSING returns the handle to a new IMAGE_PROCESSING or the handle to
% the existing singleton*.
%
% IMAGE_PROCESSING('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in IMAGE_PROCESSING.M with the given input arguments.
%
% IMAGE_PROCESSING('Property','Value',...) creates a new IMAGE_PROCESSING or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before image_processing_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to image_processing_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 image_processing

% Last Modified by GUIDE v2.5 22-Apr-2017 15:16:04

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @image_processing_OpeningFcn, ...
'gui_OutputFcn', @image_processing_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 image_processing is made visible.
function image_processing_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 image_processing (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = image_processing_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
axis off;
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
global M;
ima_gray=image_gray(M);
imshow(ima_gray);
% hObject handle to pushbutton1 (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 pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
global M;
ima_gray=image_gray(M);
[i,j]=size(ima_gray);
prompt = {'请输入阈值:'};
dlg_title = '提示';
num_lines = 1;
def = {'5'};
value_i = inputdlg(prompt,dlg_title,num_lines);
threshold_value=str2double(value_i);
for a=1:i
for b=1:j
if ima_gray(a,b)<threshold_value
ima_gray(a,b)=0;
else
ima_gray(a,b)=1;
end
end
end
ima_gray=mat2gray(ima_gray);
imshow(ima_gray);


% 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 pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
global M;
ima_red=M(:,:,1);
ima_green=M(:,:,2);
ima_blue=M(:,:,3);
img_red=mid_filter(ima_red,6);
img_green=mid_filter(ima_green,6);
img_blue=mid_filter(ima_blue,6);
image(:,:,1)=img_red;
image(:,:,2)=img_green;
image(:,:,3)=img_blue;
imshow(image);


% hObject handle to pushbutton3 (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 pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
global M;
if size(M,1)<2;
msgbox('请先打开图片');
end
ima_red=M(:,:,1);
ima_green=M(:,:,2);
ima_blue=M(:,:,3);
processing_red=low_pass_filter(ima_red);
processing_green=low_pass_filter(ima_green);
processing_blue=low_pass_filter(ima_blue);
image(:,:,1)=processing_red;
image(:,:,2)=processing_green;
image(:,:,3)=processing_blue;
imshow(image);

% hObject handle to pushbutton4 (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 pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
global M;
prompt = {'请输入滤波核大小:'};
dlg_title = '提示';
num_lines = 1;
def = {'5'};
value_i = inputdlg(prompt,dlg_title,num_lines);
N=str2double(value_i);
d=avg_filter(M,N);
imshow(d);


% hObject handle to pushbutton5 (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 pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
global M;
%滤波核大小
ima_red=M(:,:,1);
ima_green=M(:,:,2);
ima_blue=M(:,:,3);
prompt = {'请输入滤波器大小:'};
dlg_title = '提示';
num_lines = 1;
value_i = inputdlg(prompt,dlg_title,num_lines);
N=str2double(value_i);
sigma=1.7;
img_red=image_gaussian(ima_red,sigma,N);
img_green=image_gaussian(ima_green,sigma,N);
img_blue=image_gaussian(ima_blue,sigma,N);
img(:,:,1)=img_red;
img(:,:,2)=img_green;
img(:,:,3)=img_blue;
imshow(img);

% hObject handle to pushbutton6 (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 pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
global M;
ima_red=M(:,:,1);
ima_green=M(:,:,2);
ima_blue=M(:,:,3);
histogram_red=histogram(ima_red);
histogram_green=histogram(ima_green);
histogram_blue=histogram(ima_blue);
figure,
subplot(1,3,1);plot(histogram_red),title('红色通道');
xlim([0 255])
subplot(1,3,2),plot(histogram_green),title('绿色通道');
xlim([0 255])
subplot(1,3,3),plot(histogram_blue),title('蓝色通道');
xlim([0 255])
% ima=imread('1.jpg');
% ima_gaussian=image_gaussian(ima,2,500);
% hObject handle to pushbutton7 (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 pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)%每个通道不一样,不能按照一个通道来
global M;
ima_gray=image_gray(M);
[i,j]=size(ima_gray);
threshold_value=150;
for a=1:i
for b=1:j
if ima_gray(a,b)<threshold_value
ima_gray(a,b)=0;
else
ima_gray(a,b)=1;
end
end
end

四、运行结果

【图像处理】基于matlab GUI数字图像处理【含Matlab源码 652期】_计算机视觉

【图像处理】基于matlab GUI数字图像处理【含Matlab源码 652期】_计算机视觉_02

【图像处理】基于matlab GUI数字图像处理【含Matlab源码 652期】_邻域_03

【图像处理】基于matlab GUI数字图像处理【含Matlab源码 652期】_matlab_04

【图像处理】基于matlab GUI数字图像处理【含Matlab源码 652期】_邻域_05

【图像处理】基于matlab GUI数字图像处理【含Matlab源码 652期】_像素点_06

【图像处理】基于matlab GUI数字图像处理【含Matlab源码 652期】_计算机视觉_07

五、matlab版本及参考文献

1 matlab版本

2014a

2 参考文献

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

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

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

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

[5]陈浩,方勇,朱大洲,王成,陈子龙.基于蚁群算法的玉米植株热红外图像边缘检测[J].农机化研究. 2015,37(06)