《图像分割Matlab代码》由会员分享,可在线阅读,更多相关《图像分割Matlab代码(7页珍藏版)》请在金锄头文库上搜索。
1、图像分割Matlab代码(一)图像边缘检测不同方法比较将Roberts、Sobel、Prewitt、LOG、Canny算子等经典图像分割算法对灰度图像分割的结果进行比较。Matlab 代码如下:% 图像边缘检测不同方法比较% Roberts、Sobel、Prewitt、LOG、Canny算子对灰度图像分割的结果比较clc;clear all;close all;f=imread(8_256_lena.bmp,bmp);subplot(2,3,1);subimage(f);title(原始图像);g, t=edge(f,roberts,both);subplot(2,3,2);subimage(
2、g);title(Roberts算子对图像分割的结果);g, t=edge(f,sobel,both);subplot(2,3,3);subimage(g);title(Sobel算子对图像分割的结果);g, t=edge(f,prewitt,both);subplot(2,3,4);subimage(g);title(Prewitt算子对图像分割的结果);g, t=edge(f,log);subplot(2,3,5);subimage(g);title(LOG算子对图像分割的结果);g, t=edge(f,canny);subplot(2,3,6);subimage(g);title(Can
3、ny算子对图像分割的结果);(二)区域生长法分割图像区域生长法分割图像,matlab代码如下:% 区域生长法分割图像clc;clear all;close all;f=imread(rice_1.bmp,bmp);% f=imread(rice.png,png);% f=imread(8_256_lena.bmp,bmp);subplot(1,2,1);subimage(f); %选择三个种子点seedx=63, 10, 85;%rice图的生长点seedy=30, 56, 60;% seedx=100, 150, 227;%lena图的生长点% seedy=56, 130, 189;hold
4、 onplot(seedx,seedy,gs,linewidth,1);title(原始图像及种子点位置);f=double(f);markerim=f=f(seedy(1),seedx(1);for i=2:length(seedx) markerim=markerim|(f=f(seedy(i),seedx(i);end%3个种子点区域的阈值thresh=12,6,12;maskim= zeros(size(f);for i=1:length(seedx) g=abs(f-f(seedy(i),seedx(i)=thresh(i); maskim=maskim|g;endg,nr=bwla
5、bel(imreconstruct(markerim,maskim),8);% g=mat2gray(g);%以灰度级显示,注释掉此行以二值图像显示subplot(1,2,2);subimage(g);title(三个种子点区域生长分割结果);(三)迭代阈值选择法二值化图像与Otsu阈值选择法二值化图像比较迭代阈值选择法二值化图像与Otsu阈值选择法二值化图像比较的matlab代码如下:% 迭代阈值选择法二值化图像与Otsu阈值选择法二值化图像比较clc;clear all;close all;f=imread(8_256_lena.bmp,bmp);subplot(2,2,1);subima
6、ge(f);title(原始图像);f=double(f);T=(min(f(:)+max(f(:)/2;done=false;i=0;while done r1=find(fT); Tnew=(mean(f(r1)+mean(f(r2)/2; done=abs(Tnew-T)1; T=Tnew; i=i+1;endf(r1)=0;f(r2)=1;subplot(2,2,2);subimage(f);title(迭代阈值二值化图像图像);f=imread(8_256_lena.bmp,bmp);subplot(2,2,3);subimage(f);title(原始图像);T=graythresh(f);g=im2bw(f,T);subplot(2,2,4);subimage(g);title(Otsu方法二值化图像);