A question about demo

10 views (last 30 days)
lin
lin on 16 Feb 2012
This is the code of a matlab demo.But when I run it,there is some error. ??? Conversion to cell from uint8 is not possible.
Error in ==> pill at 40 segmented_images(k)=color;
The code is following
clear all
close all
clc
RGB=imread('pill.jpg');
I=rgb2gray(RGB);
Ie=edge(I);
figure
subplot(2,3,1)
imshow(RGB);
title('original image');
subplot(2,3,2)
imshow(Ie)
title('edge detection');
%perform K-means
cform = makecform('srgb2lab');
I_lab = applycform(RGB,cform);
ab = double(I_lab(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
nColors = 2;
% repeat the clustering 5 times to avoid local minima
[cluster_idx cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
'Replicates',3);
%select cluster with least amount to find the pills
pixel_labels = reshape(cluster_idx,nrows,ncols);
if sum(pixel_labels(:)==1)> sum(pixel_labels(:)==2)
cluster_idx==1;
else
cluster_idx==2;
end
subplot(2,3,3);
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=RGB;
color(rgb_label~=k)=0;
segmented_images(k)=color;
end
subplot(2,3,4);
imshow(segmented_images(clus_idx))
title('object in selected cluster');
The pill image is uploaded in <http://www.uploadhouse.com/viewfile.php?id=15607729&PHPSESSID=899c99620753e831f26f63c605b06afa
>

Answers (1)

Jan
Jan on 16 Feb 2012
It is strange that segmented_images is allocated as {1 x 3} cell, but afterwards nColors elements should be set to a vector:
segmented_images=cell(1,3);
rgb_label=repmat(pixel_labels,[1 1 3]);
for k=1:nColors
color=RGB;
color(rgb_label~=k)=0;
segmented_images(k)=color;
end
Did you ask the author already? Perhaps this works:
segmented_images=cell(1,nColors);
rgb_label=repmat(pixel_labels,[1 1 3]);
for k=1:nColors
color=RGB;
color(rgb_label~=k)=0;
segmented_images{k}=color;
end

Categories

Find more on System on Chip (SoC) in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!