MATLAB 边缘检测,图像滤波 工具

使用App Designer 写了一款可以实现如下功能的小程序:

  • 卷积运算
  • 中值滤波
  • 高斯滤波
  • 边缘检测
  • 噪声添加
  • 灰度化、二值化、反色

使用方式

1,导入图片在这里插入图片描述
2,图像预处理
点击左上角将图片灰度化,黑白化或者,或添加噪声
注:该步骤为预处理只对原图生效
在这里插入图片描述
在这里插入图片描述
注:二值化及噪声添加的各种数据都可设置:
在这里插入图片描述

3,设置模板大小、横纵梯度算子、方差、卷积次数
注:

  • 模板卷积及中值滤波只需设置横向梯度算子(即模板)模板卷积会自动除以权重和
  • 中值滤波在模板区域输入1
  • 边缘检测需同时设置横向及纵向梯度算子
  • 高斯滤波只需设置space-Sigma及range-Sigma
  • 输入数值可以为公式形式,例如sqrt(5)
  • 空缺位置默认为0

在这里插入图片描述
在这里插入图片描述
4,开始卷积
举边缘检测例子
在这里插入图片描述
在这里插入图片描述
5,存储图像
存储图像按钮存储的为当前图像
批量存储按钮存储的是每次卷积的图像及预处理图像

输入批量存储图片名称及格式:
在这里插入图片描述
批量存储结果:
其中_0的图片为预处理图片
在这里插入图片描述

完整代码

function Template_Convolution_App
%该程序用于实现图像模板卷积实验
%功能包括:
%图像平滑(中值滤波,均值滤波,高斯滤波,双边滤波等)
%图像边缘检测
%图像噪声添加




%全局变量>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
global TCA_Fig TCA_Axes
global TCA_BG_SE TCA_BG_35 TCA_Spinner PGdisplay
global Hx Hy SigmaS SigmaR T33 times
global Hx_ Hy_ Gx_ Gy_ Gxy_ SigmaS_ SigmaR_ Gs_ Gr_ Gsr_ Wx_ Wy_ coe_
global oriPic subOriPic noisePic convolutionPic
global PicSet
times=0;


%界面制作>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
TCA_Fig=uifigure('units','pixels');
TCA_Fig.Position=[10,50,930,600];
TCA_Fig.NumberTitle='off';
TCA_Fig.MenuBar='none';
TCA_Fig.Name='模板卷积App';
TCA_Fig.Color=[1,1,1].*0.98;

TCA_Axes=uiaxes('Parent',TCA_Fig);
TCA_Axes.Position=[15,15,570,570];
TCA_Axes.XTick=[];
TCA_Axes.YTick=[];
TCA_Axes.XColor='none';
TCA_Axes.YColor='none';
TCA_Axes.Toolbar.Visible='off';
hold(TCA_Axes,'on');
%按钮 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uibutton(TCA_Fig,'Text','导 入 图 片','BackgroundColor',[0.31 0.58 0.80],'FontColor',[1 1 1],...
'FontWeight','bold','Position',[600,550,120,35],'FontSize',16,'ButtonPushedFcn',@loadPicFcn);
uibutton(TCA_Fig,'Text','返 回 原 图','BackgroundColor',[0.31 0.58 0.80],'FontColor',[1 1 1],...
'FontWeight','bold','Position',[600,505,120,35],'FontSize',16,'ButtonPushedFcn',@selectOriFcn);
uibutton(TCA_Fig,'Text','清 空 数 据','BackgroundColor',[0.31 0.58 0.80],'FontColor',[1 1 1],...
'FontWeight','bold','Position',[600,460,120,35],'FontSize',16,'ButtonPushedFcn',@clearAllFcn);
uibutton(TCA_Fig,'Text','开 始 卷 积','BackgroundColor',[0.31 0.58 0.80],'FontColor',[1 1 1],...
'FontWeight','bold','Position',[600,120,120,35],'FontSize',16,'ButtonPushedFcn',@convolutionFcn);
uibutton(TCA_Fig,'Text','存 储 图 片','BackgroundColor',[0.31 0.58 0.80],'FontColor',[1 1 1],...
'FontWeight','bold','Position',[600,75,120,35],'FontSize',16,'ButtonPushedFcn',@savePicFcn);
uibutton(TCA_Fig,'Text','批 量 存 储','BackgroundColor',[0.31 0.58 0.80],'FontColor',[1 1 1],...
'FontWeight','bold','Position',[600,30,120,35],'FontSize',16,'ButtonPushedFcn',@saveAllPicFcn);
%按钮组- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
%TCA:Template Convolution App
%BG :Button Group
%SE :Smooth or Edge Detection
TCA_BG_SE=uibuttongroup(TCA_Fig);
TCA_BG_SE.Title=' 模板卷积方法';
TCA_BG_SE.Position=[600,310,120,130];
TCA_BG_SE.FontSize=14;
set(TCA_BG_SE,'SelectionChangedFcn',@changeCTFcn);%CT:Convolution Type
%
BG_S=uiradiobutton(TCA_BG_SE);
BG_S.Position=[15 80 100 20];
BG_S.FontSize=13;
BG_S.Text='模板运算';
BG_E=uiradiobutton(TCA_BG_SE);
BG_E.Position=[15 55 100 20];
BG_E.FontSize=13;
BG_E.Text='边缘检测';
BG_M=uiradiobutton(TCA_BG_SE);
BG_M.Position=[15 30 100 20];
BG_M.FontSize=13;
BG_M.Text='中值滤波';
BG_B=uiradiobutton(TCA_BG_SE);
BG_B.Position=[15  5 100 20];
BG_B.FontSize=13;
BG_B.Text='双边滤波';

%35:3*3 size Template or 5*5 size Template
TCA_BG_35=uibuttongroup(TCA_Fig);
TCA_BG_35.Title=' 模板大小';
TCA_BG_35.Position=[600,210,120,80];
TCA_BG_35.FontSize=14;
set(TCA_BG_35,'SelectionChangedFcn',@changeTSFcn);%CT:Template Size
%
BG_3=uiradiobutton(TCA_BG_35);
BG_3.Position=[15 30 100 20];
BG_3.FontSize=13;
BG_3.Text='3*3 模板';
BG_5=uiradiobutton(TCA_BG_35);
BG_5.Position=[15  5 100 20];
BG_5.FontSize=13;
BG_5.Text='5*5 模板';
%模板组- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
for i=1:5
    for j=1:5
        Hx(i,j)=uieditfield(TCA_Fig,'text'); 
        set(Hx(i,j),'Position',[705+35*j,550-35*i,30,30]);
        set(Hx(i,j),'HorizontalAlignment','center');
        set(Hx(i,j),'FontSize',15);
        set(Hx(i,j),'FontColor',[1 1 1].*0.45);
        
        Hy(i,j)=uieditfield(TCA_Fig,'text'); 
        set(Hy(i,j),'Position',[705+35*j,330-35*i,30,30]);
        set(Hy(i,j),'HorizontalAlignment','center');
        set(Hy(i,j),'FontSize',15);
        set(Hy(i,j),'FontColor',[1 1 1].*0.45);
        set(Hy(i,j),'Editable','off');
    end
end
%
T33=[1 1;1 2;1 3;1 4;1 5;5 1;5 2;5 3;5 4;5 5;2 1;3 1;4 1;2 5;3 5;4 5];
for i=1:size(T33,1),set(Hx(T33(i,1),T33(i,2)),'Editable','off');end
%
uilabel(TCA_Fig,'Text','     横 向 梯 度 算 子 (Hx)','HorizontalAlignment','left',...
'BackgroundColor',[0 0.2510 0.4510],'FontColor',[1 1 1],'FontWeight','bold',...
'Position',[740,555,170,30],'FontSize',13);
uilabel(TCA_Fig,'Text','     纵 向 梯 度 算 子 (Hy)','HorizontalAlignment','left',...
'BackgroundColor',[0 0.2510 0.4510],'FontColor',[1 1 1],'FontWeight','bold',...
'Position',[740,335,170,30],'FontSize',13);
%- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
uilabel(TCA_Fig,'Text',' 卷 积 次 数','HorizontalAlignment','left',...
'BackgroundColor',[0.31 0.58 0.80],'FontColor',[1 1 1],'FontWeight','bold',...
'Position',[600,170,80,30],'FontSize',13);
TCA_Spinner=uispinner(TCA_Fig);
TCA_Spinner.Position=[675,170,50,30];
TCA_Spinner.Limits = [0 200];
TCA_Spinner.RoundFractionalValues='on';
%- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uilabel(TCA_Fig,'Text','  space-Sigma','HorizontalAlignment','left',...
'BackgroundColor',[0 0.2510 0.4510],'FontColor',[1 1 1],'FontWeight','bold',...
'Position',[740,110,120,30],'FontSize',13);
uilabel(TCA_Fig,'Text','  range-Sigma','HorizontalAlignment','left',...
'BackgroundColor',[0 0.2510 0.4510],'FontColor',[1 1 1],'FontWeight','bold',...
'Position',[740,70,120,30],'FontSize',13);
SigmaS=uieditfield(TCA_Fig,'text');
SigmaS.Position=[860,110,50,30];
SigmaS.HorizontalAlignment='center';
SigmaS.FontSize=15;
SigmaS.Editable='off';
SigmaR=uieditfield(TCA_Fig,'text');
SigmaR.Position=[860,70,50,30];
SigmaR.HorizontalAlignment='center';
SigmaR.FontSize=15;
SigmaR.Editable='off';
%噪声与通道- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Menu=uimenu(TCA_Fig);
Menu.Text='图像预处理';
%
Menu_1=uimenu(Menu);
Menu_1.Text='色彩操作';
%
Menu_1_1=uimenu(Menu_1);
Menu_1_1.Text='灰度图像(多通道)';
set(Menu_1_1,'MenuSelectedFcn',@C_1_1_Fcn);
Menu_1_2=uimenu(Menu_1);
Menu_1_2.Text='灰度图像(单通道)';
set(Menu_1_2,'MenuSelectedFcn',@C_1_2_Fcn);
Menu_1_3=uimenu(Menu_1);
Menu_1_3.Text='二值图像(多通道)';
set(Menu_1_3,'MenuSelectedFcn',@C_1_3_Fcn);
Menu_1_4=uimenu(Menu_1);
Menu_1_4.Text='二值图像(单通道)';
set(Menu_1_4,'MenuSelectedFcn',@C_1_4_Fcn);
Menu_1_5=uimenu(Menu_1);
Menu_1_5.Text='反色图像';
set(Menu_1_5,'MenuSelectedFcn',@C_1_5_Fcn);
%
Menu_2=uimenu(Menu);
Menu_2.Text='噪声操作';
%
Menu_2_1=uimenu(Menu_2);
Menu_2_1.Text='添加椒盐噪声';
set(Menu_2_1,'MenuSelectedFcn',@N_2_1_Fcn);
Menu_2_2=uimenu(Menu_2);
Menu_2_2.Text='添加高斯噪声';
set(Menu_2_2,'MenuSelectedFcn',@N_2_2_Fcn);
Menu_2_3=uimenu(Menu_2);
Menu_2_3.Text='添加泊松噪声';
set(Menu_2_3,'MenuSelectedFcn',@N_2_3_Fcn);
%进程指示器- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
%'  正在进行第i次卷积 . . .'
PGdisplay=uilabel(TCA_Fig,'Text','','HorizontalAlignment','left',...
'BackgroundColor',[0.8912 0.9727 1],'FontColor',[0.1 0.1 0.3],'FontWeight','bold',...
'Position',[740,30,170,35],'FontSize',15);


%回调函数>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
%图片导入- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function loadPicFcn(~,~)
    try
        [filename,pathname]=uigetfile({
    
    '*.*';'*.jpg';'*.tif';'*.bmp';'*.png'},'File Selector');
        oriPic=imread([pathname,filename]);
        subOriPic=oriPic;
        noisePic=[];
        convolutionPic=[];
        [XLim,YLim,~]=size(oriPic);
        hold(TCA_Axes,'off');
        imshow(oriPic,'Parent',TCA_Axes);
        hold(TCA_Axes,'on');
        TCA_Axes.XLim=[0,max([XLim,YLim])];
        TCA_Axes.YLim=[0,max([XLim,YLim])];    
    catch
    end
end
%返回原图- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function selectOriFcn(~,~)
    subOriPic=oriPic;noisePic=[];convolutionPic=[];
    hold(TCA_Axes,'off');
    imshow(oriPic,'Parent',TCA_Axes);
    hold(TCA_Axes,'on');
    PGdisplay.Text='';
end
%清空数据- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function clearAllFcn(~,~)
    oriPic=[];
    subOriPic=[];
    noisePic=[];
    convolutionPic=[];
    for ii=1:5
        for jj=1:5
           set(Hx(ii,jj),'Value','');
           set(Hy(ii,jj),'Value','');
        end
    end
    PGdisplay.Text='';
    SigmaS.Value='';
    SigmaR.Value='';
    hold(TCA_Axes,'off');
    imshow(uint8(ones(100,100).*255),'Parent',TCA_Axes);
    TCA_Axes.XLim=[0 100];
    TCA_Axes.YLim=[0 100];
    hold(TCA_Axes,'on');
end
%开始卷积- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function convolutionFcn(~,~)
    selectedButton=TCA_BG_SE.SelectedObject;
    Type35=TCA_BG_35.SelectedObject.Text(1);
    if Type35=='3'
        Hx_=zeros(3,3);Hy_=zeros(3,3);
        for ii=1:3
            for jj=1:3
                if isempty(get(Hx(ii+1,jj+1),'Value'))
                    Hx_(ii,jj)=0;
                else
                    Hx_(ii,jj)=eval(get(Hx(ii+1,jj+1),'Value'));
                end
            end
        end  
        for ii=1:3
            for jj=1:3
                if isempty(get(Hy(ii+1,jj+1),'Value'))
                    Hy_(ii,jj)=0;
                else
                    Hy_(ii,jj)=eval(get(Hy(ii+1,jj+1),'Value'));
                end
            end
        end 
    else
        Hx_=zeros(5,5);Hy_=zeros(5,5);
        for ii=1:5
            for jj=1:5
                if isempty(get(Hx(ii,jj),'Value'))
                    Hx_(ii,jj)=0;
                else
                    Hx_(ii,jj)=eval(get(Hx(ii,jj),'Value'));
                end
            end
        end  
        for ii=1:5
            for jj=1:5
                if isempty(get(Hy(ii,jj),'Value'))
                    Hy_(ii,jj)=0;
                else
                    Hy_(ii,jj)=eval(get(Hy(ii,jj),'Value'));
                end
            end
        end
    end
    
    if isempty(SigmaS.Value)
        SigmaS_=0;
    else
        SigmaS_=eval(SigmaS.Value);
    end
    if isempty(SigmaR.Value)
        SigmaR_=0;
    else
        SigmaR_=eval(SigmaR.Value);
    end
    
    if ~isempty(noisePic)
        convolutionPic=noisePic;
    else
        convolutionPic=subOriPic;
    end
    switch selectedButton.Text     
        case '模板运算'
            for ii=1:TCA_Spinner.Value
                PGdisplay.Text=['  正在进行第',num2str(ii),'次卷积 . . .'];pause(0.05);
                convolutionPic=edgeDetectionOpt(convolutionPic);
                PicSet.(['p',num2str(ii)])=convolutionPic;
            end
        case '边缘检测'
            for ii=1:TCA_Spinner.Value
                PGdisplay.Text=['  正在进行第',num2str(ii),'次卷积 . . .'];pause(0.05);
                convolutionPic=edgeDetectionOpt(convolutionPic);
                PicSet.(['p',num2str(ii)])=convolutionPic;
            end
        case '中值滤波'
            for ii=1:TCA_Spinner.Value
                PGdisplay.Text=['  正在进行第',num2str(ii),'次卷积 . . .'];pause(0.05);
                convolutionPic=midFilterImage(convolutionPic);
                PicSet.(['p',num2str(ii)])=convolutionPic;
            end   
        case '双边滤波'  
            for ii=1:TCA_Spinner.Value
                PGdisplay.Text=['  正在进行第',num2str(ii),'次卷积 . . .'];pause(0.05);
                convolutionPic=bilateralFilterImage(convolutionPic);
                PicSet.(['p',num2str(ii)])=convolutionPic;
            end   
    end
    if TCA_Spinner.Value~=0
        hold(TCA_Axes,'off');
        imshow(convolutionPic,'Parent',TCA_Axes);
        hold(TCA_Axes,'on');
    end
    times=TCA_Spinner.Value;
    PGdisplay.Text='  卷积试验结束';
end
%存储图像- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function savePicFcn(~,~)
    try
        [filename,pathname]=uiputfile({
    
    '*.*';'*.jpg';'*.tif';'*.bmp';'*.png'});
        if ~isempty(convolutionPic)
            imwrite(convolutionPic,[pathname,filename]);
        elseif ~isempty(noisePic)
            imwrite(noisePic,[pathname,filename]);
        else
            imwrite(subOriPic,[pathname,filename]);
        end
    catch
    end
end
%批量存储- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function saveAllPicFcn(~,~)
    if ~isempty(convolutionPic)
        try
        [filename,pathname]=uiputfile({
    
    '*.*';'*.jpg';'*.tif';'*.bmp';'*.png'});
        num=abs(filename);
        pos=find(num==46);
        for ii=1:times
            imwrite(PicSet.(['p',num2str(ii)]),[pathname,filename(1:pos-1),'_',num2str(ii),filename(pos:end)]);
        end  
        if ~isempty(noisePic)
            imwrite(noisePic,[pathname,filename(1:pos-1),'_0',filename(pos:end)]);
        else
            imwrite(subOriPic,[pathname,filename(1:pos-1),'_0',filename(pos:end)]);
        end    
        catch
        end
    end
end
%TCA_BG_SE- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function changeCTFcn(~,~)
    selectedButton=TCA_BG_SE.SelectedObject;
    switch selectedButton.Text
        case '模板运算',onOff1='on';onOff2='off';onOff3='off';clear1=0;clear2=1;clear3=1;
        case '边缘检测',onOff1='on';onOff2='on';onOff3='off';clear1=0;clear2=0;clear3=1;
        case '中值滤波',onOff1='on';onOff2='off';onOff3='off';clear1=0;clear2=1;clear3=1;
        case '双边滤波',onOff1='off';onOff2='off';onOff3='on';clear1=1;clear2=1;clear3=0;       
    end
    for ii=1:5
        for jj=1:5
            set(Hx(ii,jj),'Editable',onOff1);
            set(Hy(ii,jj),'Editable',onOff2);
            if clear1==1,set(Hx(ii,jj),'Value','');end
            if clear2==1,set(Hy(ii,jj),'Value','');end
        end
    end
    SigmaS.Editable=onOff3;
    SigmaR.Editable=onOff3;
    if clear3==1,SigmaS.Value='';SigmaR.Value='';end
    changeTSFcn();
end
%TCA_BG_35- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function changeTSFcn(~,~)
    selectedButton=TCA_BG_SE.SelectedObject;
    Type35=TCA_BG_35.SelectedObject.Text(1);
    switch selectedButton.Text
        case '模板运算',onOff1=1;onOff2=0;
        case '边缘检测',onOff1=1;onOff2=1;
        case '中值滤波',onOff1=1;onOff2=0;
        case '双边滤波',onOff1=0;onOff2=0;     
    end
    if onOff1==1
        if Type35=='3'
            for ii=1:size(T33,1)
                set(Hx(T33(ii,1),T33(ii,2)),'Editable','off');
                set(Hx(T33(ii,1),T33(ii,2)),'Value','')
            end
        else
            for ii=1:size(T33,1)
                set(Hx(T33(ii,1),T33(ii,2)),'Editable','on');
            end
        end
    end
    if onOff2==1
        if Type35=='3'
            for ii=1:size(T33,1)
                set(Hy(T33(ii,1),T33(ii,2)),'Editable','off');
                set(Hy(T33(ii,1),T33(ii,2)),'Value','')
            end
        else
            for ii=1:size(T33,1)
                set(Hy(T33(ii,1),T33(ii,2)),'Editable','on');
            end
        end
    end
    PGdisplay.Text='';
end
%噪声与通道- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function C_1_1_Fcn(~,~)
    NChannels=size(subOriPic,3);
    NChannel=uint8(zeros([size(oriPic,1),size(oriPic,2)]));
    for ii=1:NChannels
        NChannel=NChannel+subOriPic(:,:,ii)./NChannels;
    end
    for ii=1:3
        subOriPic(:,:,ii)=NChannel;
    end
    hold(TCA_Axes,'off');
    imshow(subOriPic,'Parent',TCA_Axes);
    hold(TCA_Axes,'on');
    noisePic=[];
    convolutionPic=[];   
    PGdisplay.Text='';
end
%
function C_1_2_Fcn(~,~)
    NChannels=size(subOriPic,3);
    NChannel=uint8(zeros([size(oriPic,1),size(oriPic,2)]));
    for ii=1:NChannels
        NChannel=NChannel+subOriPic(:,:,ii)./NChannels;
    end
    subOriPic=NChannel;
    hold(TCA_Axes,'off');
    imshow(subOriPic,'Parent',TCA_Axes);
    hold(TCA_Axes,'on');
    noisePic=[];
    convolutionPic=[];   
    PGdisplay.Text='';
end
%
function C_1_3_Fcn(~,~)
    createDataExtractObj('二值化阈值','150',@C_1_3_Fcn_part2)
end
function C_1_3_Fcn_part2(~,~)
    NChannels=size(subOriPic,3);
    NChannel=uint8(zeros([size(oriPic,1),size(oriPic,2)]));
    for ii=1:NChannels
        NChannel=NChannel+subOriPic(:,:,ii)./NChannels;
    end
    NChannel(NChannel>=coe_)=255;
    NChannel(NChannel<coe_)=0;
    for ii=1:3
        subOriPic(:,:,ii)=NChannel;
    end
    hold(TCA_Axes,'off');
    imshow(subOriPic,'Parent',TCA_Axes);
    hold(TCA_Axes,'on');
    noisePic=[];
    convolutionPic=[];  
    PGdisplay.Text='';
end
%
function C_1_4_Fcn(~,~)
    createDataExtractObj('二值化阈值','150',@C_1_4_Fcn_part2)
end
function C_1_4_Fcn_part2(~,~)
    NChannels=size(subOriPic,3);
    NChannel=uint8(zeros([size(oriPic,1),size(oriPic,2)]));
    for ii=1:NChannels
        NChannel=NChannel+subOriPic(:,:,ii)./NChannels;
    end
    NChannel(NChannel>=coe_)=255;
    NChannel(NChannel<coe_)=0;
    subOriPic=NChannel;
    hold(TCA_Axes,'off');
    imshow(subOriPic,'Parent',TCA_Axes);
    hold(TCA_Axes,'on');
    noisePic=[];
    convolutionPic=[]; 
    PGdisplay.Text='';
end
%
function C_1_5_Fcn(~,~)
    NChannels=size(subOriPic,3);
    for ii=1:NChannels
        subOriPic(:,:,ii)=255-subOriPic(:,:,ii);
    end
    hold(TCA_Axes,'off');
    imshow(subOriPic,'Parent',TCA_Axes);
    hold(TCA_Axes,'on');
    noisePic=[];
    convolutionPic=[]; 
    PGdisplay.Text='';
end
%
function N_2_1_Fcn(~,~)
    createDataExtractObj('椒盐噪声密度','0.1',@N_2_1_Fcn_part2)
end
function N_2_1_Fcn_part2(~,~)
    noisePic=imnoise(subOriPic,'salt & pepper',coe_);
    hold(TCA_Axes,'off');
    imshow(noisePic,'Parent',TCA_Axes);
    hold(TCA_Axes,'on');
    convolutionPic=[]; 
    PGdisplay.Text='';
end
%
function N_2_2_Fcn(~,~)
    createDataExtractObj('高斯噪声标准差','0.1',@N_2_2_Fcn_part2)
end
function N_2_2_Fcn_part2(~,~)
    noisePic=imnoise(subOriPic,'gaussian',0,coe_);
    hold(TCA_Axes,'off');
    imshow(noisePic,'Parent',TCA_Axes);
    hold(TCA_Axes,'on');
    convolutionPic=[]; 
    PGdisplay.Text='';
end
%
function N_2_3_Fcn(~,~)
    noisePic=imnoise(subOriPic,'poisson');
    hold(TCA_Axes,'off');
    imshow(noisePic,'Parent',TCA_Axes);
    hold(TCA_Axes,'on');
    convolutionPic=[]; 
    PGdisplay.Text='';
end




%功能函数>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
%图像扩张- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
function expandImage=expandImageFcn(oriImage)
    NChannels=size(oriImage,3);
    rows=size(oriImage,1);
    cols=size(oriImage,2);
    expandImage=uint8(zeros([rows+2,cols+2,NChannels]));
    for ii=1:NChannels
        tempMat=uint8(zeros([rows+2,cols+2]));
        tempMat(2:rows+1,2:cols+1)=oriImage(:,:,ii);
        tempMat(2:rows+1,1)=oriImage(:,1,ii);
        tempMat(2:rows+1,cols+2)=oriImage(:,cols,ii);
        tempMat(1,2:cols+1)=oriImage(1,:,ii);
        tempMat(rows+2,2:cols+1)=oriImage(rows,:,ii);
        tempMat(1,1)=oriImage(1,1,ii);
        tempMat(rows+2,1)=oriImage(rows,1,ii);
        tempMat(1,cols+2)=oriImage(1,cols,ii);
        tempMat(rows+2,cols+2)=oriImage(rows,cols,ii);
        expandImage(:,:,ii)=tempMat;
    end
end
%梯度算子、拉普拉斯算子、模板运算- - - - - - - - - - - - - - - - - - - - -
function newPic=edgeDetectionOpt(oriImage)
    NChannels=size(oriImage,3);
    rows=size(oriImage,1);
    cols=size(oriImage,2);
    sep=(size(Hx_,1)-1)/2;
    Wx_=sum(sum(Hx_));
    Wy_=sum(sum(Hy_));
    Wx_(Wx_==0)=1;
    Wy_(Wy_==0)=1;
    newPic=uint8(zeros([rows,cols,NChannels]));
    expandImage=expandImageFcn(oriImage);
    for ii=1:sep-1
        expandImage=expandImageFcn(expandImage);
    end
    for ii=1+sep:rows+sep
        for jj=1+sep:cols+sep
            for k=1:NChannels
                tempMat=expandImage(ii-sep:ii+sep,jj-sep:jj+sep,k);
                Gx_=sum(sum(double(tempMat).*Hx_));
                Gy_=sum(sum(double(tempMat).*Hy_));
                Gxy_=sqrt(Gx_^2+Gy_^2)/Wx_/Wy_;
                newPic(ii-sep,jj-sep,k)=uint8(Gxy_);
            end
        end
    end
end
%中值滤波- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function newPic=midFilterImage(oriImage)
    NChannels=size(oriImage,3);
    rows=size(oriImage,1);
    cols=size(oriImage,2);
    sep=(size(Hx_,1)-1)/2;
    Hx_(Hx_~=0)=1;
    Hx_(Hx_==0)=nan;
    newPic=uint8(zeros([rows,cols,NChannels]));
    expandImage=expandImageFcn(oriImage);
    for ii=1:sep-1
        expandImage=expandImageFcn(expandImage);
    end
    for ii=1+sep:rows+sep
        for jj=1+sep:cols+sep
            for k=1:NChannels
                tempMat=expandImage(ii-sep:ii+sep,jj-sep:jj+sep,k);
                tempMat2=double(tempMat).*Hx_;
                newPic(ii-sep,jj-sep,k)=uint8(median(tempMat2(~isnan(tempMat2))));
            end
        end
    end
end
%双边滤波- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function newPic=bilateralFilterImage(oriImage)
    NChannels=size(oriImage,3);
    rows=size(oriImage,1);
    cols=size(oriImage,2);
    sep=(size(Hx_,1)-1)/2;
    [xmesh,ymesh]=meshgrid(1:size(Hx_,1));
    Ds2=(xmesh-sep-1).^2+(ymesh-sep-1).^2;
    
    newPic=uint8(zeros([rows,cols,NChannels]));
    expandImage=expandImageFcn(oriImage);
    for ii=1:sep-1
        expandImage=expandImageFcn(expandImage);
    end
    for ii=1+sep:rows+sep
        for jj=1+sep:cols+sep
            for k=1:NChannels
                tempMat=expandImage(ii-sep:ii+sep,jj-sep:jj+sep,k);
                tempMat2=double(tempMat);
                Dr2=tempMat2-tempMat2(sep+1,sep+1);
                Dr2=Dr2.^2;
                Gs_=exp(-(Ds2)./(2*SigmaS_*SigmaS_));
				Gr_=exp(-(Dr2)./(2*SigmaR_*SigmaR_));
                Gsr_=Gs_.*Gr_;
                newPic(ii-sep,jj-sep,k)=uint8(sum(sum(tempMat2.*Gsr_))/sum(sum(Gsr_)));
            end
        end
    end
end

%数据返回器>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
function createDataExtractObj(str,presetValue,Fcn)
    DE_Fig=uifigure('units','pixels');
    DE_Fig.Position=[TCA_Fig.Position(1),TCA_Fig.Position(2)+TCA_Fig.Position(4)-100,200,100];
    DE_Fig.NumberTitle='off';
    DE_Fig.MenuBar='none';
    DE_Fig.Name='参数设置';
    DE_Fig.Color=[1,1,1].*0.98;
    uilabel(DE_Fig,'Text',[' ',str],'HorizontalAlignment','left',...
    'BackgroundColor',[0 0.2510 0.4510],'FontColor',[1 1 1],'FontWeight','bold',...
    'Position',[10,52,140,35],'FontSize',15);
    coe=uieditfield(DE_Fig,'text');
    coe.Position=[130,52,60,35];
    coe.HorizontalAlignment='center';
    coe.FontSize=15;
    coe.Value=presetValue;
    uibutton(DE_Fig,'Text','确 认 设 置','BackgroundColor',[0.31 0.58 0.80],'FontColor',[1 1 1],...
    'FontWeight','bold','Position',[10,10,85,35],'FontSize',14,'ButtonPushedFcn',@ensureDEOFcn);
    uibutton(DE_Fig,'Text','取 消 设 置','BackgroundColor',[0.31 0.58 0.80],'FontColor',[1 1 1],...
    'FontWeight','bold','Position',[105,10,85,35],'FontSize',14,'ButtonPushedFcn',@closeDEOFcn);
    function ensureDEOFcn(~,~)
        coe_=eval(coe.Value);
        Fcn();
        delete(DE_Fig)
    end
    function closeDEOFcn(~,~)
        delete(DE_Fig)
    end
end
end

猜你喜欢

转载自blog.csdn.net/slandarer/article/details/113482091
今日推荐