MATLAB - 图像处理几个实用代码(2)

2019-06-17 11:13

权威认证

clc

clear all

I=imread('xian.bmp'); figure

imshow(I);

title('原始图像');

I1=rgb2gray(I); %将彩色图像转化灰度图像

threshold=graythresh(I1); %计算将灰度图像转化为二值图像所需的门限 BW=im2bw(I1, threshold); %将灰度图像转化为二值图像 figure

imshow(BW); title('二值图像'); dim=size(BW);

col=round(dim(2)/2)-90; %计算起始点列坐标 row=find(BW(:,col),1); %计算起始点行坐标 connectivity=8; num_points=180;

contour=bwtraceboundary(BW,[row,col],'N',connectivity,num_points); %提取边界 figure

imshow(I1); hold on;

plot(contour(:,2),contour(:,1), 'g','LineWidth' ,2); title('边界跟踪图像'); 12.Hough变换

I= imread('xian.bmp'); rotI=rgb2gray(I); subplot(2,2,1); imshow(rotI); title('灰度图像');

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

BW=edge(rotI,'prewitt'); subplot(2,2,2); imshow(BW);

title('prewitt算子边缘检测 后图像'); axis([50,250,50,200]); grid on; axis on;

[H,T,R]=hough(BW); subplot(2,2,3);

imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit'); title('霍夫变换图');

权威认证

xlabel('\\theta'),ylabel('\\rho'); axis on , axis normal, hold on;

P=houghpeaks(H,5,'threshold',ceil(0.3*max(H(:)))); x=T(P(:,2));y=R(P(:,1)); plot(x,y,'s','color','white');

lines=houghlines(BW,T,R,P,'FillGap',5,'MinLength',7); subplot(2,2,4);,imshow(rotI); title('霍夫变换图像检测'); axis([50,250,50,200]); grid on; axis on; hold on; max_len=0;

for k=1:length(lines)

xy=[lines(k).point1;lines(k).point2];

plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');

plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow'); plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red'); len=norm(lines(k).point1-lines(k).point2); if(len>max_len) max_len=len; xy_long=xy; end end

plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan'); 13.直方图阈值法

用 MATLAB实现直方图阈值法: I=imread('xian.bmp'); I1=rgb2gray(I); figure;

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

title(' 灰度图像') axis([50,250,50,200]);

grid on; %显示网格线 axis on; %显示坐标系

[m,n]=size(I1); %测量图像尺寸参数

GP=zeros(1,256); %预创建存放灰度出现概率的向量 for k=0:255

GP(k+1)=length(find(I1==k))/(m*n); %计算每级灰度出现的概率,将其存入GP中相应位置 end

subplot(2,2,2),bar(0:255,GP,'g') %绘制直方图 title('灰度直方图')

权威认证

xlabel('灰度值') ylabel(' 出现概率') I2=im2bw(I,150/255); subplot(2,2,3),imshow(I2); title('阈值150的分割图像') axis([50,250,50,200]);

grid on; %显示网格线 axis on; %显示坐标系 I3=im2bw(I,200/255); % subplot(2,2,4),imshow(I3); title('阈值200的分割图像') axis([50,250,50,200]);

grid on; %显示网格线 axis on; %显示坐标系 14. 自动阈值法:Otsu法 用MATLAB实现Otsu算法: clc

clear all

I=imread('xian.bmp'); 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; %显示坐标系 15.膨胀操作

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=imdilate(I1,se); %用生成的结构元素对图像进行膨胀 subplot(1,2,2); imshow(I2);

权威认证

title(' 膨胀后图像'); axis([50,250,50,200]);

grid on; %显示网格线 axis on; %显示坐标系 16.腐蚀操作

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; %显示坐标系 17.开启和闭合操作

用 MATLAB实现开启和闭合操作 I=imread('xian.bmp'); %载入图像 subplot(2,2,1),imshow(I); title('原始图像');

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

axis on; %显示坐标系 I1=rgb2gray(I);

subplot(2,2,2),imshow(I1); title('灰度图像');

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

axis on; %显示坐标系

se=strel('disk',1); %采用半径为1的圆作为结构元素 I2=imopen(I1,se); %开启操作 I3=imclose(I1,se); %闭合操作 subplot(2,2,3),imshow(I2); title('开启运算后图像'); axis([50,250,50,200]);

axis on; %显示坐标系 subplot(2,2,4),imshow(I3); title('闭合运算后图像'); axis([50,250,50,200]);

权威认证

axis on; %显示坐标系 18.开启和闭合组合操作

I=imread('xian.bmp'); %载入图像 subplot(3,2,1),imshow(I); title('原始图像');

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

axis on; %显示坐标系 I1=rgb2gray(I);

subplot(3,2,2),imshow(I1); title('灰度图像');

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

axis on; %显示坐标系 se=strel('disk',1);

I2=imopen(I1,se); %开启操作 I3=imclose(I1,se); %闭合操作 subplot(3,2,3),imshow(I2); title('开启运算后图像'); axis([50,250,50,200]);

axis on; %显示坐标系 subplot(3,2,4),imshow(I3); title('闭合运算后图像'); axis([50,250,50,200]);

axis on; %显示坐标系 se=strel('disk',1); I4=imopen(I1,se); I5=imclose(I4,se);

subplot(3,2,5),imshow(I5); %开—闭运算图像 title('开—闭运算图像'); axis([50,250,50,200]);

axis on; %显示坐标系 I6=imclose(I1,se); I7=imopen(I6,se);

subplot(3,2,6),imshow(I7); %闭—开运算图像 title('闭—开运算图像'); axis([50,250,50,200]);

axis on; %显示坐标系 19.形态学边界提取

利用 MATLAB实现如下:

I=imread('xian.bmp'); %载入图像 subplot(1,3,1),imshow(I); title('原始图像');

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

grid on; %显示网格线 axis on; %显示坐标系


MATLAB - 图像处理几个实用代码(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:新视野大学英语第二版第三册listening 7

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: