A = imread('kobi.png'); [L,N] = superpixels(A,500); figure BW = boundarymask(L); imshow(imoverlay(A,BW,'cyan')) % Set color of each pixel in output image to the mean RGB color of the % superpixel region. outputImage = zeros(size(A),'like',A); idx = label2idx(L); numRows = size(A,1); numCols = size(A,2); for labelVal = 1:N redIdx = idx{labelVal}; greenIdx = idx{labelVal}+numRows*numCols; blueIdx = idx{labelVal}+2*numRows*numCols; outputImage(redIdx) = mean(A(redIdx)); outputImage(greenIdx) = mean(A(greenIdx)); outputImage(blueIdx) = mean(A(blueIdx)); end figure imshow(outputImage) % 2. he = imread('hestain.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)); nrows = size(ab,1); ncols = size(ab,2); ab = reshape(ab,nrows*ncols,2); nColors = 3; % repeat the clustering 3 times to avoid local minima [cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ... 'Replicates',3); pixel_labels = reshape(cluster_idx,nrows,ncols); imshow(pixel_labels,[]), title('image labeled by cluster index'); segmented_images = cell(1,3); rgb_label = repmat(pixel_labels,[1 1 3]); for k = 1:nColors color = he; color(rgb_label ~= k) = 0; segmented_images{k} = color; end imshow(segmented_images{1}), title('objects in cluster 1'); imshow(segmented_images{2}), title('objects in cluster 2'); imshow(segmented_images{3}), 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_cluster_num); L_blue = L(blue_idx); is_light_blue = imbinarize(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');