图像处理MATLAB源码

来源:互联网 发布:淘宝试用成功后要钱吗 编辑:程序博客网 时间:2024/04/29 20:23

图像反转

 I=imread('nickyboom.jpg');

J=double(I);
J=-J+(256-1);                 %图像反转线性变换
H=uint8(J);
subplot(1,2,1),imshow(I);

subplot(1,2,2),imshow(H);




直方图均衡化

MATLAB 程序实现如下:

I=imread('nickyboom.jpg);

I=rgb2gray(I);

figure;

subplot(2,2,1);

imshow(I);

subplot(2,2,2);

imhist(I);

I1=histeq(I);

figure;

subplot(2,2,1);

imshow(I1);

subplot(2,2,2);

imhist(I1);




均值滤波器

I=imread('nickyboom.jpg');

subplot(231)

imshow(I)

title('原始图像')

I=rgb2gray(I);

I1=imnoise(I,'salt & pepper',0.02);

subplot(232)

imshow(I1)

title(' 添加椒盐噪声的图像')

k1=filter2(fspecial('average',3),I1)/255;          %进行3*3模板平滑滤波

k2=filter2(fspecial('average',5),I1)/255;          %进行5*5模板平滑滤波

k3=filter2(fspecial('average',7),I1)/255;          %进行7*7模板平滑滤波

k4=filter2(fspecial('average',9),I1)/255;          %进行9*9模板平滑滤波

subplot(233),imshow(k1);title('3*3 模板平滑滤波');

subplot(234),imshow(k2);title('5*5 模板平滑滤波');

subplot(235),imshow(k3);title('7*7 模板平滑滤波');

subplot(236),imshow(k4);title('9*9 模板平滑滤波');




中值滤波器

I=imread('nickyboom.jpg');
I=rgb2gray(I);
J=imnoise(I,'salt & pepper',0.02);
subplot(231),imshow(I);title('原图像');
subplot(232),imshow(J);title('添加椒盐噪声图像');
k1=medfilt2(J);            %进行3*3模板中值滤波
k2=medfilt2(J,[5,5]);      %进行5*5模板中值滤波
k3=medfilt2(J,[7,7]);      %进行7*7模板中值滤波
k4=medfilt2(J,[9,9]);      %进行9*9模板中值滤波
subplot(233),imshow(k1);title('3*3模板中值滤波');
subplot(234),imshow(k2);title('5*5模板中值滤波 ');
subplot(235),imshow(k3);title('7*7模 板中值滤波');
subplot(236),imshow(k4);title('9*9 模板中值滤波');




边缘检测

I=imread('nickyboom.jpg');
subplot(2,3,1);
imshow(I);
title('原始图像');
axis([50,250,50,200]);
grid on;                  %显示网格线
axis on;                  %显示坐标系
I1=im2bw(I);
subplot(2,3,2);
imshow(I1);
title('二值图像');
axis([50,250,50,200]);
grid on;                  %显示网格线
axis on;                  %显示坐标系
I2=edge(I1,'roberts');
subplot(2,3,3);
imshow(I2);
title('roberts算子边缘检测');
axis([50,250,50,200]);
grid on;                  %显示网格线
axis on;                  %显示坐标系
I3=edge(I1,'sobel');
subplot(2,3,4);
imshow(I3);
title('sobel算子边缘检测');
axis([50,250,50,200]);
grid on;                  %显示网格线
axis on;                  %显示坐标系
I4=edge(I1,'Prewitt');
subplot(2,3,5);
imshow(I4);
title('Prewitt算子边缘检测 ');
axis([50,250,50,200]);
grid on;                  %显示网格线
axis on;                  %显示坐标系

I5=edge(I1,'log');

subplot(2,3,6);
imshow(I5);

title('log算子边缘检测');

axis([50,250,50,200]);
grid on;                  %显示网格线
axis on;                  %显示坐标系




自动阈值法:Otsu法

用MATLAB实现Otsu算法:

clc

clear all

I=imread('nickyboom.jpg');

subplot(1,2,1),imshow(I);

title('原始图像')

axis([50,250,50,200]);

grid on;                  %显示网格线

axis on;                  %显示坐标系

level=graythresh(I);     %确定灰度阈值

BW=im2bw(I,level);

subplot(1,2,2),imshow(BW);

title('Otsu 法阈值分割图像')

axis([50,250,50,200]);

grid on;                  %显示网格线

axis on;                  %显示坐标系



膨胀操作

I=imread('nickyboom.jpg');          %载入图像

I1=rgb2gray(I);

subplot(1,2,1);

imshow(I1);

title('灰度图像')     

axis([50,250,50,200]);

grid on;                  %显示网格线

axis on;                  %显示坐标系

se=strel('disk',1);          %生成圆形结构元素

I2=imdilate(I1,se);             %用生成的结构元素对图像进行膨胀

subplot(1,2,2);

imshow(I2);

title(' 膨胀后图像');

axis([50,250,50,200]);

grid on;                  %显示网格线

axis on;                  %显示坐标系



腐蚀操作

MATLAB 实现腐蚀操作

I=imread('xian.bmp');          %载入图像

I1=rgb2gray(I);

subplot(1,2,1);

imshow(I1);

title('灰度图像')     

axis([50,250,50,200]);

grid on;                  %显示网格线

axis on;                  %显示坐标系

se=strel('disk',1);       %生成圆形结构元素

I2=imerode(I1,se);        %用生成的结构元素对图像进行腐蚀

subplot(1,2,2);

imshow(I2);

title('腐蚀后图像');

axis([50,250,50,200]);

grid on;                  %显示网格线

axis on;                  %显示坐标系





4 0