MATLAB Answers

Mariam Sheha

How to skip "roipoly" step?

Asked by Mariam Sheha
on 22 Jul 2013

Hi Everybody... wish you are doing well..

I got nice color segmentation color from your site "File Exchange" that one done by webinar, it's working well but i had a problem that i want to skip the roipoly step , where i am already using a segmented image as attached. Sample image:

I would appreciate your help telling me how can i use it on a segmented image instead of using "roipoly"...

Thanks Alot...

The Code:

% load image

rgb = imread('segmented 3.jpg');

x(:,:,1)=imresize(rgb(:,:,1),[480 640]);

x(:,:,2)=imresize(rgb(:,:,2),[480 640]);

x(:,:,3)=imresize(rgb(:,:,3),[480 640]);


% smooth image (reduce noise/color variation)

rgb = imfilter(rgb,ones(3,3)/9);

% view image and RGB layers (nonuniform illumination)

figure(1), set(1,'position',[99 79 826 589])

subplot(2,2,1), subimage(rgb), title('fabric image'), axis off

subplot(2,2,2), map=gray(256); map(:,2:3)=0; subimage(rgb(:,:,1),map), title('red layer'), axis off

subplot(2,2,3), map=gray(256); map(:,[1 3])=0; subimage(rgb(:,:,2),map), title('green layer'), axis off

subplot(2,2,4), map=gray(256); map(:,1:2)=0; subimage(rgb(:,:,3),map), title('blue layer'), axis off

% RGB histograms (poor separability)

figure(1), set(1,'position',[452 68 560 420])

figure(2), set(2,'position',[16 269 560 420])


c='rgb'; for i=1:3

 n=hist(reshape(double(rgb(:,:,i)),[480*640 1]),0.5:256);


axis tight, xlim([0 255]), box on

xlabel intensity, ylabel population, title histograms

% convert image to L*a*b* color space (transform)

cform = makecform('srgb2lab');

lab = applycform(rgb,cform);

% view components (note illumination free)


subplot(2,2,1), subimage(rgb), title('fabric image'), axis off

subplot(2,2,2), subimage(lab(:,:,1)), title('L* layer'), axis off

subplot(2,2,3), map=gray(256); map(:,3)=0; map(:,2)=map(end:-1:1,2);

subimage(lab(:,:,2),map), title('a* layer'), axis off

subplot(2,2,4), map=gray(256); map(:,3)=map(end:-1:1,3); subimage(lab(:,:,3),map), title('b* layer'), axis off

% select polygon region of interest

figure, imshow(rgb), roipoly

% predefined regions for 6 different colors present

load my_regioncoordinates figure(3), imshow(rgb) for i=1:6



% predefined regions (everything else suppressed)

mask = false([480 640 6]);

for i=1:6

mask(:,:,i) = roipoly(rgb,roi(:,1,i),roi(:,2,i));


bw=repmat(logical(sum(mask,3)),[1 1 3]);

im=rgb; im(~bw)=nan;

figure(14), imshow(im)

% color markers (region average values of L*, a* and b*)

l=lab(:,:,1); a=lab(:,:,2); b=lab(:,:,3);

for i = 1:6

cMark(i,1) = mean2(l(mask(:,:,i)));
cMark(i,2) = mean2(a(mask(:,:,i)));
cMark(i,3) = mean2(b(mask(:,:,i)));


% classify every pixel using nearest neighbor rule

distance = zeros([480 640 6]);

for i=1:6

dx=double(a)-cMark(i,2);  dy=double(b)-cMark(i,3);
distance(:,:,i) = sqrt(dx.^2 + dy.^2);


cLabels=0:5; [value,index]=min(distance,[],3);


% display segmented image


figure('position',[9 344 1005 334])

subplot(121), subimage(rgb), title('Original Image'), axis off,

subplot(122), subimage(label+1,cMap), title('Segmented Image'), axis off


Image Analyst
on 12 Aug 2013

Because you didn't explain what roi you want to use instead.

Walter Roberson
on 12 Aug 2013

Your sample image does not appear to be segmented.

0 Answers

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply today