图像处理】实验二 图像增强"/>
【数字图像处理】实验二 图像增强
实验二 图像增强
- 1 实验目的
- 2 实验环境
- 3 实验内容
- 4 实验心得
1 实验目的
- 熟悉并掌握MATLAB图像处理工具臬的使用;
- 理解并掌握常用的图像增强技术。
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)/2for loc2=1:n*n-loc1if temp(loc2)>temp(loc2+1)point=temp(loc2);temp(loc2)=temp(loc2+1);temp(loc2+1)=point;endendendj(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*npoint=point+temp(loc);endaverage=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)%heightmark=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*npointX=pointX+tempX(loc);pointY=pointY+tempY(loc);endj(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*npointX=pointX+tempX(loc);pointY=pointY+tempY(loc);endj(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*npointX=pointX+tempX(loc);pointY=pointY+tempY(loc);endj(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*npoint=point+temp(loc);endpoint=point/(n*n);j(row:row+(n-1),col:col+(n-1))=point;end
end
经过算子锐化后增强的边界
图像锐化
4 实验心得
进一步理解了中值滤波、均值滤波和图像锐化的实现。对于图像锐化的实现还存在疑惑,只是按照自己的理解写了代码。图像增强对于图像的后续处理是非常重要的,必须熟练掌握相关的实现操作。
更多推荐
【数字图像处理】实验二 图像增强
发布评论