资源描述:
《kmeans 图像分割代码.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、he=imread('02.png');%读入图像imshow(he),title('H&Eimage');text(size(he,2),size(he,1)+15,...'ImagecourtesyofAlanPartin,JohnsHopkinsUniversity',...'FontSize',7,'HorizontalAlignment','right');cform=makecform('srgb2lab');%色彩空间转换lab_he=applycform(he,cform);ab=double(lab_he(:,:,2:3)
2、);%数据类型转换nrows=size(ab,1);%求矩阵尺寸ncols=size(ab,2);%求矩阵尺寸ab=reshape(ab,nrows*ncols,2);%矩阵形状变换nColors=3;%重复聚类3次,以避免局部最小值[cluster_idxcluster_center]=kmeans(ab,nColors,'distance','sqEuclidean',...'Replicates',3);pixel_labels=reshape(cluster_idx,nrows,ncols);%矩阵形状改变imshow(pixel_
3、labels,[]);%显示图像title('imagelabeledbyclusterindex');%设置图像标题segmented_images=cell(1,3);%细胞型数组rgb_label=repmat(pixel_labels,[113]);%矩阵平铺fork=1:nColorscolor=he;color(rgb_label~=k)=0;segmented_images{k}=color;endimshow(segmented_images{1}),%显示处理后的图像title('objectsincluster1');%
4、设置图像标题imshow(segmented_images{2}),%显示处理后的图像title('objectsincluster2');%设置图像标题imshow(segmented_images{3}),%显示处理后的图像title('objectsincluster3');%设置图像标题mean_cluster_value=mean(cluster_center,2);[tmp,idx]=sort(mean_cluster_value);blue_cluster_num=idx(1);L=lab_he(:,:,1);blue_idx
5、=find(pixel_labels==blue_cluster_num);L_blue=L(blue_idx);is_light_blue=im2bw(L_blue,graythresh(L_blue));%图像黑白转换nuclei_labels=repmat(uint8(0),[nrowsncols]);%矩阵平铺nuclei_labels(blue_idx(is_light_blue==false))=1;nuclei_labels=repmat(nuclei_labels,[113]);%矩阵平铺blue_nuclei=he;blu
6、e_nuclei(nuclei_labels~=1)=0;imshow(blue_nuclei),title('bluenuclei');%显示处理后的图像