【数字图像处理】实验二 图像增强

1 实验目的

  1. 熟悉并掌握MATLAB图像处理工具臬的使用;
  2. 理解并掌握常用的图像增强技术。

2 实验环境

matlab

3 实验内容

1、为图像分别加高斯噪声和椒盐噪声,采用中值滤波方法对受噪声干扰的图像滤波,窗口分别采用33、55、7*7。

%1)lab21.m
close all;
clear;
clc;

I=imread('lab2.jpeg');
I=rgb2gray(I);
I=im2double(I);

% J1=imnoise(I,'gaussian',0,0.02);  	%高斯噪声
J1=imnoise(I,'salt & pepper',0.02); 	%椒盐噪声

%自定义函数
J2=middle_filter(J1,3);     %3*3窗口
J3=middle_filter(J1,5);     %5*5窗口
J4=middle_filter(J1,7);     %7*7窗口

%系统函数
% J2=medfilt2(J1,[3 3]);     %3*3窗口
% J3=medfilt2(J1,[5 5]);     %5*5窗口
% J4=medfilt2(J1,[7 7]);     %7*7窗口

figure('NumberTitle','off','name','中值滤波'),
subplot(3,2,1);imshow(I);title('原始图像','FontName','宋体');
subplot(3,2,2);imshow(J1);title('添加噪声','FontName','宋体');
subplot(3,2,3);imshow(J2);title('中值滤波3*3','FontName','宋体');
subplot(3,2,4);imshow(J3);title('中值滤波5*5','FontName','宋体');
subplot(3,2,5);imshow(J4);title('中值滤波7*7','FontName','宋体');
%2)自定义中值滤波函数middle_filter.m
function j=middle_filter(i,n)

[width,height]=size(i);%得到图像的长和宽
j=i;
for row=1:width-(n-1)
    for col=1:height-(n-1)%height表示的个数为可完整滤波的格子数
        mark=i(row:row+(n-1),col:col+(n-1));%取出要滤波的n*n的方阵
        temp=mark(:);%将n*n矩阵转换为列向量,便于排序
        
        %冒泡排序,排一半取中值
        for loc1=1:(n*n+1)/2
            for loc2=1:n*n-loc1
                if temp(loc2)>temp(loc2+1)
                    point=temp(loc2);
                    temp(loc2)=temp(loc2+1);
                    temp(loc2+1)=point;
                end
            end
        end
        j(row+(n-1)/2,col+(n-1)/2)=temp((n*n+1)/2);
    end
end

缺省值下的高斯噪声、自定义函数下的中值滤波
缺省值下的高斯噪声、自定义函数下的中值滤波
缺省值下的椒盐噪声、自定义函数下的中值滤波
缺省值下的椒盐噪声、自定义函数下的中值滤波

2、对受噪声干扰的图像进行均值滤波。

%1)主函数lab22.m
close all;
clear;
clc;

I=imread('lab2.jpeg');

%imfilter不需要
I=rgb2gray(I);
I=im2double(I);

%J1=imnoise(I,'gaussian');  %高斯噪声
J1=imnoise(I,'salt & pepper'); %椒盐噪声

%自定义均值滤波函数
J2=average_filter(J1,3);
J3=average_filter(J1,5);
J4=average_filter(J1,7);

%均值滤波模板
% H3=fspecial('average',3);
% H5=fspecial('average',5);
% H7=fspecial('average',7);

%filter2函数滤波
% % J2=filter2(H3,J1);
% % J3=filter2(H5,J1);
% % J4=filter2(H7,J1);

%imfilter函数滤波
% J2=imfilter(J1,H3);
% J3=imfilter(J1,H5);
% J4=imfilter(J1,H7);

figure('NumberTitle','off','name','均值滤波'),
subplot(3,2,1);imshow(I);title('原始图像','FontName','宋体');
subplot(3,2,2);imshow(J1);title('添加噪声','FontName','宋体');
subplot(3,2,3);imshow(J2);title('均值滤波3*3','FontName','宋体');
subplot(3,2,4);imshow(J3);title('均值滤波5*5','FontName','宋体');
subplot(3,2,5);imshow(J4);title('均值滤波7*7','FontName','宋体');
%2)自定义均值滤波函数average_filter.m
function j=average_filter(i,n)

[width,height]=size(i);%得到图像的长和宽
j=i;

for row=1:width-(n-1)
    for col=1:height-(n-1)%height表示的个数为可完整滤波的格子数
        mark=i(row:row+(n-1),col:col+(n-1));%取出要滤波的n*n的方阵
        temp=mark(:);
        
        %计算均值
        point=0;
        for loc=1:n*n
            point=point+temp(loc);
        end
        average=point/n/n;
        j(row+(n-1)/2,col+(n-1)/2)=average;
    end
end

缺省值下的椒盐噪声、自定义函数下的均值滤波
缺省值下的椒盐噪声、自定义函数下的均值滤波
缺省值下的高斯噪声、自定义函数下的均值滤波
缺省值下的高斯噪声、自定义函数下的均值滤波

3、分别采用Roberts 算子、Sobel算子、Prewitt算子和Log算子进行图像锐化。

%1)主函数lab23.m
close all;
clear;
clc;

I=imread('lab2.jpeg');%输入图像
I=rgb2gray(I);
I=im2double(I);

J1=Roberts(I);
J1=J1+I;

J2=Sobel(I);
J2=J2+I;

J3=Prewitt(I);
J3=J3+I;

J4=LOG(I);
J4=J4+I;

figure('NumberTitle','off','name','图像锐化'),
subplot(3,2,1);imshow(I);title('原始图像','FontName','宋体');
subplot(3,2,2);imshow(J1);title('Roberts算子','FontName','宋体');
subplot(3,2,3);imshow(J2);title('Sobel算子','FontName','宋体');
subplot(3,2,4);imshow(J3);title('Prewitt算子','FontName','宋体');
subplot(3,2,5);imshow(J4);title('LOG算子','FontName','宋体');
%2)Roberts算子图像锐化函数Roberts.m
function j=Roberts(i)

[width,height]=size(i);%得到图像的长和宽
j=i;

%Roberts算子
Tx=[-1 0;0 1];  
Ty=Tx';

[n,n]=size(Tx);

for row=1:width-(n-1)
    for col=1:height-(n-1)%height
        mark=i(row:row+(n-1),col:col+(n-1));
        temp=Tx.*mark;
        tempX=temp(:);
        temp=Ty.*mark;
        tempY=temp(:);
        pointX=0;
        pointY=0;
        for loc=1:n*n
            pointX=pointX+tempX(loc);
            pointY=pointY+tempY(loc);
        end
        j(row,col)=abs(pointX)+abs(pointY);
    end
end
%3)Sobel算子图像锐化函数Sobel.m
function j=Sobel(i)

[width,height]=size(i);%得到图像的长和宽
j=i;

%Sobel算子
Tx=[-1 -2 -1;0 0 0;1 2 1];
Ty=Tx';
[n,n]=size(Tx);

for row=1:width-(n-1)
    for col=1:height-(n-1)
        mark=i(row:row+(n-1),col:col+(n-1));
        temp=Tx.*mark;
        tempX=temp(:);
        temp=Ty.*mark;
        tempY=temp(:);
        pointX=0;
        pointY=0;
        for loc=1:n*n
            pointX=pointX+tempX(loc);
            pointY=pointY+tempY(loc);
        end
        j(row+(n-1)/2,col+(n-1)/2)=sqrt(pointX^2+pointY^2);
    end
end
%4)Prewitt算子图像锐化函数Prewitt.m
function j=Prewitt(i)

[width,height]=size(i);%得到图像的长和宽
j=i;

%Prewitt算子
Tx=[-1 -1 -1;0 0 0;1 1 1];
Ty=Tx';
[n,n]=size(Tx);

for row=1:width-(n-1)
    for col=1:height-(n-1)
        mark=i(row:row+(n-1),col:col+(n-1));
        temp=Tx.*mark;
        tempX=temp(:);
        temp=Ty.*mark;
        tempY=temp(:);
        pointX=0;
        pointY=0;
        for loc=1:n*n
            pointX=pointX+tempX(loc);
            pointY=pointY+tempY(loc);
        end
        j(row+(n-1)/2,col+(n-1)/2)=sqrt(pointX^2+pointY^2);
    end
end
%5)LOG算子图像锐化函数LOG.m
function j=LOG(i)

[width,height]=size(i);%得到图像的长和宽
j=i;

%LOG算子
T=[-2 -4 -4 -4 -2;-4 0 8 0 -4;-4 8 24 8 -4;-4 0 8 0 -4;-2 -4 -4 -4 -2];
[n,n]=size(T);

for row=1:width-(n-1)
    for col=1:height-(n-1)
        mark=i(row:row+(n-1),col:col+(n-1));
        temp=T.*mark;
        temp=temp(:);
        point=0;
        for loc=1:n*n
            point=point+temp(loc);
        end
        point=point/(n*n);
        j(row:row+(n-1),col:col+(n-1))=point;
    end
end

经过算子锐化后增强的边界
经过算子锐化后增强的边界
图像锐化
图像锐化

4 实验心得

进一步理解了中值滤波、均值滤波和图像锐化的实现。对于图像锐化的实现还存在疑惑,只是按照自己的理解写了代码。图像增强对于图像的后续处理是非常重要的,必须熟练掌握相关的实现操作。

  • 5
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

社恐患者

赚钱不易呜呜呜

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值