communicate with the code

2 views (last 30 days)
lin
lin on 5 Mar 2012
I wrote this code based on a matlab demo. The first part of the code segmented the pills from the background and the second part, I wanted to measure th pill's mean color in Lab space, so applied the function "roipoly" to select a pill to calculate its mean color. But I wanna ask how to automatically measure the pill's mean color not to manually select the pill? Someone can change my code and thank you!
close all
clear all
clc
RGB = imread('pill.bmp');
figure,imshow(RGB);
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;
[cluster_idx cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
'Replicates',5);
pixel_labels = reshape(cluster_idx,nrows,ncols);
if sum(pixel_labels(:)==1)>sum(pixel_labels(:)==2)
clus_idx = 2;
else
clus_idx = 1;
end
se=strel('disk',13);
pixel_labels=imopen(pixel_labels,se);
figure,imshow(pixel_labels,[]);
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
figure,imshow(segmented_images{clus_idx});
RGB2=im2double(segmented_images{clus_idx});
Z=roipoly(RGB2);
[d1,d2]=size(Z);
c=0;
L=[];
for a=1:d1
for b=1:d2
if Z(a,b)==1
n=1;
c=c+1;
L(n,c)=a;
n=2;
L(n,c)=b;
end
end
end
cform=makecform('srgb2lab');
lab=applycform(RGB2,cform);
P=[];
for n=1:c
P(n,:)=impixel(lab,L(2*n),L(2*n-1));
end
roil=[];roia=[];roib=[];
sum_l=0;sum_a=0;sum_b=0;
for n=1:c
sum_l=sum_l+P(n,1);
sum_a=sum_a+P(n,2);
sum_b=sum_b+P(n,3);
end
roil=sum_l/c;
roia=sum_a/c;
roib=sum_b/c;
Lab_value=[roil roia roib]
  2 Comments
lin
lin on 6 Mar 2012
Does anyone have any ideas?
Ernesto
Ernesto on 14 Oct 2012
After selecting the image that I do to continue the program...Thanks for responding

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!