《kmeans-图像分割代码》由会员分享,可在线阅读,更多相关《kmeans-图像分割代码(2页珍藏版)》请在金锄头文库上搜索。
1、精品文档,仅供学习与交流,如有侵权请联系网站删除he = imread(02.png); % 读入图像imshow(he), title(H&E image);text(size(he,2),size(he,1)+15,.Image courtesy of Alan Partin, Johns Hopkins University, .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_idx cluster_center = kmeans(ab,nColors,distance,sqEuclidean, .Replicates,3);pixel_labels = reshape(cluster_idx,nrows,ncols); % 矩阵形状改变imshow(pixel_labels,); %
3、显示图像title(image labeled by cluster index); % 设置图像标题segmented_images = cell(1,3); % 细胞型数组rgb_label = repmat(pixel_labels,1 1 3); % 矩阵平铺for k = 1:nColorscolor = he;color(rgb_label = k) = 0;segmented_imagesk = color;endimshow(segmented_images1), % 显示处理后的图像title(objects in cluster 1); % 设置图像标题imshow(seg
4、mented_images2), % 显示处理后的图像title(objects in cluster 2); % 设置图像标题imshow(segmented_images3), % 显示处理后的图像title(objects in cluster 3); % 设置图像标题mean_cluster_value = mean(cluster_center,2);tmp, idx = sort(mean_cluster_value);blue_cluster_num = idx(1);L = lab_he(:,:,1);blue_idx = find(pixel_labels = blue_cl
5、uster_num);L_blue = L(blue_idx);is_light_blue = im2bw(L_blue,graythresh(L_blue); % 图像黑白转换nuclei_labels = repmat(uint8(0),nrows ncols); % 矩阵平铺nuclei_labels(blue_idx(is_light_blue=false) = 1;nuclei_labels = repmat(nuclei_labels,1 1 3); % 矩阵平铺blue_nuclei = he;blue_nuclei(nuclei_labels = 1) = 0;imshow(blue_nuclei), title(blue nuclei); % 显示处理后的图像【精品文档】第 页