communicate with the code
Show older comments
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]
The pill image is uploaded in <http://www.uploadhouse.com/viewfile.php?id=15607729&PHPSESSID=899c99620753e831f26f63c605b06afa>
Answers (0)
Categories
Find more on Images 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!