⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wavlet22.m

📁 是有关图象去噪声的课程设计
💻 M
字号:
function varargout = wavlet22(varargin)
% WAVLET22 M-file for wavlet22.fig
%      WAVLET22, by itself, creates a new WAVLET22 or raises the existing
%      singleton*.
%
%      H = WAVLET22 returns the handle to a new WAVLET22 or the handle to
%      the existing singleton*.
%
%      WAVLET22('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in WAVLET22.M with the given input arguments.
%
%      WAVLET22('Property','Value',...) creates a new WAVLET22 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before wavlet22_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to wavlet22_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 wavlet22

% Last Modified by GUIDE v2.5 21-Jun-2004 11:13:32

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @wavlet22_OpeningFcn, ...
                   'gui_OutputFcn',  @wavlet22_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(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 wavlet22 is made visible.
function wavlet22_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 wavlet22 (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = wavlet22_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 during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global pp;
pp=str2double(get(hObject,'String'));
% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% 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)
global pp;
global  psnr_noise_remove1;
load psnr_noise_remove1;
global psnr1;
load psnr1;
global  psnr_noise_remove2;
load psnr_noise_remove2;
global psnr2;
load psnr2;

%读入原始图像并显示
i=imread('lena.bmp');
subplot(2,2,1);
imshow(i);
title('原始图像');
axis square;

%生成含噪图像并显示
j=imnoise(i,'gaussian',0,pp);
subplot(2,2,2);
imshow(j);
title('含噪图像');
axis square;
imwrite(j,'tu1.bmp','bmp');

%将RGB图像转换为灰度图像
l=rgb2gray(j); 
%用sym4小波函数对j进行2层分解
[c,s]=wavedec2(l,2,'sym4');
%实现低通滤波消噪
a1=uint8(wrcoef2('a',c,s,'sym4',2));
%用coif2小波函数对j进行2层分解
[gc,gl]=wavedec2(a1,2,'coif2');
n=[1,2];%设置尺度向量
p=[10.28,24.08];%设置阈值向量
%对三个高频系数进行阈值处理
nc=wthcoef2('h',gc,gl,n,p,'s');
nc=wthcoef2('v',gc,gl,n,p,'s');
nc=wthcoef2('d',gc,gl,n,p,'s');
mc=wthcoef2('h',gc,gl,n,p,'s');
mc=wthcoef2('v',gc,gl,n,p,'s');
mc=wthcoef2('d',gc,gl,n,p,'s');
%对更新后的小波分解结构进行阈值处理
x2=waverec2(mc,gl,'coif2');


%进行空间均值滤波
for ii=2:255;
    for jj=2:255;
         temp=0;
         for m=-1:1;
             for n=-1:1;
                 temp=temp+double(x2(ii+m,jj+n));
             end
          end
          temp=temp/9;
          x4(ii,jj)=temp;
     end
end

for ii=1:256;
     x4(ii,1)=double(i(ii,1));
     %x4(ii,2)=double(i(ii,2));
end
for ii=1:256;
    x4(ii,256)=double(i(ii,256));
    %x4(ii,255)=double(i(ii,255));
end
for jj=1:256;
    x4(1,jj)=double(i(1,jj));
    %x4(2,jj)=double(i(2,jj));
end
for jj=1:256;
    x4(256,jj)=double(i(256,jj));
    %x4(255,jj)=double(i(255,jj));
end

%显示去噪图像    
subplot(2,2,3);
imshow(uint8(x4));
title('均值消噪图像');
axis square;
imwrite(uint8(x4),'tu2.bmp','bmp');

%进行中值滤波
for ii=2:255;
    for jj=2:255;
         temp=0;
         A=[];
         for ll=1:9;
                   A(ll)=0;
         end
         for m=1:3;
             for n=1:3;
                 A(m*n)=double(x2(ii+m-2,jj+n-2));
             end
         end
         for ll=1:9;
             for kk=ll+1:9;
                  if A(ll)<=A(kk)  
                      temp=A(ll);
                      A(ll)=A(kk);
                      A(kk)=temp;
                  end
             end
         end
         x5(ii,jj)=A(5);
     end
end

for ii=1:256;
     x5(ii,1)=double(i(ii,1));
     %x5(ii,2)=double(i(ii,2));
end
for ii=1:256;
    x5(ii,256)=double(i(ii,256));
    %x5(ii,255)=double(i(ii,255));
end
for jj=1:256;
    x5(1,jj)=double(i(1,jj));
    %x5(2,jj)=double(i(2,jj));
end
for jj=1:256;
    x5(256,jj)=double(i(256,jj));
    %x5(255,jj)=double(i(255,jj));
end
%显示去噪图像    
subplot(2,2,4);
imshow(uint8(x5));
title('中值消噪图像');
imwrite(uint8(x5),'tu3.bmp','bmp');

%计算psnr1
q=0.0;
for x=1:256;
    for j=1:256;
        diff=x4(x,j)-double(i(x,j));     
        q=q+diff*diff;
    end
end
    psnr1_y = q /  (256*256); 
    %psnr1_y =  (255*255)/psnr1_y;
set(psnr_noise_remove1,'String',psnr1_y);

save psnr1 psnr1;

%计算psnr2
q=0.0;
for x=1:256;
    for j=1:256;
        diff=x5(x,j)-double(i(x,j));     
        q=q+diff*diff;
    end
end
    psnr2_y = q /  (256*256); 
    %psnr2_y =  (255*255)/psnr2_y;
set(psnr_noise_remove2,'String',psnr2_y);
%for ii=1:10;
    %if psnr2(ii)==0
     %   psnr2(ii)=psnr2_y;
      %  break;
    %end
%end
save psnr2 psnr2;

% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
global psnr_noise_remove1;
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

psnr_noise_remove1=hObject;
save psnr_noise_remove1 psnr_noise_remove1;

function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.

function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
global psnr_noise_remove2;
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end
psnr_noise_remove2=hObject;
save psnr_noise_remove2 psnr_noise_remove2;


function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double
% --- Executes during object creation, after setting all properties.


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -