基于MATLAB的图像平滑处理(完美运行)
资源内容介绍
图像平滑处理是一种常用的图像处理方法,用于去除图像中的噪声,使得图像变得更加平滑和清晰。图像平滑处理的目标是在尽量保留图像的细节的同时,去除图像中的噪声。图像平滑处理通常基于图像的局部特征,通过对图像中的像素进行加权平均来实现。常用的图像平滑处理方法有以下几种:1. 均值滤波:将图像中的每个像素替换为其周围像素的平均值。均值滤波可以有效去除图像中的高频噪声,但会导致图像的细节模糊。2. 中值滤波:将图像中的每个像素替换为其周围像素的中值。中值滤波适用于去除图像中的椒盐噪声等脉冲噪声,能够保持图像的边缘和细节。3. 高斯滤波:通过对图像中的像素进行加权平均来平滑图像。高斯滤波可以有效去除图像中的高频噪声,同时保持图像的细节和边缘特征。4. 双边滤波:在进行像素平均时,考虑到图像的空间距离和像素之间的灰度差异。双边滤波可以有效平滑图像,同时保持图像的边缘和纹理细节。图像平滑处理在很多图像处理应用中都有广泛的应用,如图像降噪、图像增强、图像压缩等。通过选择合适的平滑处理方法,可以使得图像在去噪的同时保持更好的视觉效果。 function varargout = test(varargin)% TEST MATLAB code for test.fig% TEST, by itself, creates a new TEST or raises the existing% singleton*.%% H = TEST returns the handle to a new TEST or the handle to% the existing singleton*.%% TEST('CALLBACK',hObject,eventData,handles,...) calls the local% function named CALLBACK in TEST.M with the given input arguments.%% TEST('Property','Value',...) creates a new TEST or raises the% existing singleton*. Starting from the left, property value pairs are% applied to the GUI before test_OpeningFcn gets called. An% unrecognized property name or invalid value makes property application% stop. All inputs are passed to test_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 test% Last Modified by GUIDE v2.5 27-Jun-2023 17:12:12% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @test_OpeningFcn, ... 'gui_OutputFcn', @test_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []);if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1});endmainfc;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 test is made visible.function test_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 test (see VARARGIN)% Choose default command line output for testhandles.output = hObject;mainfc;% Update handles structureguidata(hObject, handles);% UIWAIT makes test wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.function varargout = test_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 structurevarargout{1} = handles.output;% --- Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles) [filename, pathname] = uigetfile({'*.jpg';'*.bmp';'*.jpeg'},'选择图像'); if isequal(filename,0) | isequal(pathname,0) disp('取消,重选'); else disp(['选择 ', fullfile(pathname, filename)]) endx=imread(filename);axes(handles.axes1)imshow(x)title('原图')handles.x=xguidata(hObject, handles);set(handles.text2,'string','图像选择完毕')% 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)set(handles.text2,'string','梯度倒数加权平均平滑算法。。。')pause(1)ticx=handles.xI=x; H=sqrt(double(I));%把数据类型转换成double,然后进行平方根变换H=uint8(H);%sqrt函数不支持uint8类型,把数据类型转换成uint8类型 %对图象p1-03施加高斯噪声和椒盐噪声并实施梯度倒数加权平滑法(公式4.2.3、4.2.4、4.2,利用方差评价空域平滑的效果a=imnoise(I,'salt & pepper'); axes(handles.axes2); imshow(a); title('椒盐噪声图'); a=double(a); [dep,wide]=size(a); new_image=ones(size(a)); r=0.5; for i=2:dep-1 for j=2:wide-1 g=0; for m=-1:1 for n=-1:1 if(a(i+m,j+n)-a(i,j)==0) g(m+2,n+2)=0; else g(m+2,n+2)=1/abs(a(i+m,j+n)-a(i,j)); end end end G=sum(sum(g)); for m=-1:1 for n=-1:1 w(m+2,n+2)=g(m+2,n+2)/G; end end new_image(i,j)=a(i,j)*r+(1-r)*(w(-1+2,-1+2)*a(i-1,j-1)+w(-1+2,0+2)*a(i-1,j)+w(-1+2,1+2)*a(i-1,j+1)+w(0+2,-1+2)*a(i,j-1)+w(0+2,1+2)*a(i,j+1)+w(1+2,-1+2)*a(i+1,j-1)+w(1+2,0+2)*a(i+1,j)+w(1+2,1+2)*a(i+1,j+1)); end end for i=2:dep-1 new_image(i,1)=new_image(i,2); new_image(i,wide)=new_image(i,wide-1); end new_image(1,:)=new_image(2,:); new_image(dep,:)=new_image(dep-1,:); axes(handles.axes3); imshow(x); title('最后梯度倒数加权平滑'); time=tocset(handles.edit1,'string',time)set(handles.text2,'string','梯度倒数加权平均平滑处理完毕')% 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)set(handles.text2,'string','最大均匀性平滑滤波开始,约需等待95s。。。')pause(1)ticx=handles.x%**********************最大均匀性平滑法滤波*************************%im=x;[m,n]=size(im);noise_pic=imnoise(im,'salt & pepper',0.02); axes(handles.axes2)imshow(noise_pic);title('椒盐噪声图')I=im2double(im) ;for h=1:m X(1,h)=I(1,h); X(2,h)=I(2,h); X(n-1,h)=I(n-1,h); X(n,h)=I(n,h); end for j=1:n X(j,1)=I(j,1); X(j,2)=I(j,2); X(j,m-1)=I(j,m-1); X(j,m)=I(j,m); end for i=3:n-2 for j=3:m-2 F1=[I(i-2,j-2),I(i-2,j-1),I(i-2,j),I(i-1,j-2),I(i-1,j-1),I(i-1,j),I(i,j-2),I(i,j-1),I(i,j)];%取出每个邻域的像素值 F2=[I(i-2,j+1),I(i-2,j-1),I(i-2,j),I(i-1,j+1),I(i-1,j-1),I(i-1,j),I(i,j+1),I(i,j-1),I(i,j)]; F3=[I(i-2,j+1),I(i-2,j+2),I(i-2,j),I(i-1,j+1),I(i-1,j+2),I(i-1,j),I(i,j+1),I(i,j+2),I(i,j)]; F4=[I(i+1,j-2),I(i+1,j-1),I(i+1,j),I(i-1,j-2),I(i-1,j-1),I(i-1,j),I(i,j-2),I(i,j-1),I(i,j)]; F5=[I(i+1,j+1),I(i+1,j-1),I(i+1,j),I(i-1,j+1),I(i-1,j-1),I(i-1,j),I(i,j+1),I(i,j-1),I(i,j)]; F6=[I(i+1,j+1),I(i+1,j+2),I(i+1,j),I(i-1,j+1),I(i-1,j+2),I(i-1,j),I(i,j+1),I(i,j+2),I(i,j)]; F7=[I(i+1,j-2),I(i+1,j-1),I(i+1,j),I(i+2,j-2),I(i+2,j-1),I(i+2,j),I(i,j-2),I(i,j-1),I(i,j)]; F8=[I(i+1,j+1),I(i+1,j-1),I(i+1,j),I(i+2,j+1),I(i+2,j-1),I(i+2,j),I(i,j+1),I(i,j-1),I(i,j)]; F9=[I(i+1,j+1),I(i+1,j+2),I(i+1,j),I(i+2,j+1),I(i+2,j+2),I(i+2,j),I(i,j+1),I(i,j+2),I(i,j)]; I1=var(F1); I2=var(F2); I3=var(F3); I4=var(F4); I5=var(F5); I6=var(F6); I7=var(F7); I8=var(F8); I9=var(F9); %求方差 array=[I1,I2,I3,I4,I5,I6,I7,I8,I9]; %将方差放在一个数组中 A=sort(array); %对方差排序 switch A(1) %判断最小方差属于哪一个邻域 case I1 average=(I(i-2,j-2)+I(i-2,j-1)+I(i-2,j)+I(i-1,j-2)+I(i-1,j-1)+I(i-1,j)+I(i,j-2)+I(i,j-1)+I(i,j))/9; case I2 average=(I(i-2,j+1)+I(i-2,j-1)+I(i-2,j)+I(i-1,j+1)+I(i-1,j-1)+I(i-1,j)+I(i,j+1)+I(i,j-1)+I(i,j))/9; case I3 average=(I(i-2,j+1)+I(i-2,j+2)+I(i-2,j)+I(i-1,j+1)+I(i-1,j+2)+I(i-1,j)+I(i,j+1)+I(i,j+2)+I(i,j))/9; case I4 average=(I(i+1,j-2)+I(i+1,j-1)+I(i+1,j)+I(i-1,j-2)+I(i-1,j-1)+I(i-1,j)+I(i,j-2)+I(i,j-1)+I(i,j))/9; c