Image segmentation post conversion of image to HSV space
11 views (last 30 days)
Show older comments
So I'm working on image segmentation and I was able to successfully cluster (via kmeans clustering) the components in the image. I was able to "blue" the segmented parts of the image as seen below:
This is the code I came up with for implementation:
MATLAB code:
clear all;close ALL;
A=imread('stopcup.jpg');
%A = rgb2hsv(A); %converting the image to HSV space
subplot(2,3,1),imshow(A), title('original image');
title('Original Image');
F = makecform('srgb2lab'); %converting the image to L*a*b color space to quantify visual differences.
F1 = applycform(A,F);
img = double(F1(:,:,2:3));
R = size(img,1);
C = size(img,2);
img = reshape(img,R*C,2);
colors = 4;
[cindex, ccenter] = kmeans(img,colors,'distance','sqEuclidean', ...
'Replicates',3); %K means clustering, done 3 times, to group the color regions.
plabels = reshape(cindex,R,C);
subplot(2,3,2),imshow(plabels,[]), title('cluster index');
pause;
img_seg = cell(1,4); %creating four cluster images
rgbl = repmat(plabels,[1 1 3]);
for k = 1:colors
color = A;
color(rgbl ~= k) = 0;
img_seg{k} = color;
end
threshold=20;
for row=1:size(img_seg{1},1)
for col=1:size(img_seg{1},2)
for rgb=1:size(img_seg{1},3)
if(img_seg{1}(row,col,rgb)>=threshold)
if(rgb==3)
img_seg{1}(row,col,rgb)=125;
else
img_seg{1}(row,col,rgb)=0;
end
end
end
end
end
threshold=20;
for row=1:size(img_seg{2},1)
for col=1:size(img_seg{2},2)
for rgb=1:size(img_seg{2},3)
if(img_seg{2}(row,col,rgb)>=threshold)
if(rgb==3)
img_seg{2}(row,col,rgb)=125;
else
img_seg{2}(row,col,rgb)=0;
end
end
end
end
end
threshold=20;
for row=1:size(img_seg{3},1)
for col=1:size(img_seg{3},2)
for rgb=1:size(img_seg{3},3)
if(img_seg{3}(row,col,rgb)>=threshold)
if(rgb==3)
img_seg{3}(row,col,rgb)=125;
else
img_seg{3}(row,col,rgb)=0;
end
end
end
end
end
threshold=20;
for row=1:size(img_seg{4},1)
for col=1:size(img_seg{4},2)
for rgb=1:size(img_seg{4},3)
if(img_seg{4}(row,col,rgb)>=threshold)
if(rgb==3)
img_seg{4}(row,col,rgb)=125;
else
img_seg{4}(row,col,rgb)=0;
end
end
end
end
end
subplot(2,3,3),imshow(img_seg{1}), title('cluster 1');
pause;
subplot(2,3,4),imshow(img_seg{2}), title('cluster 2');
pause;
subplot(2,3,5),imshow(img_seg{3}), title('cluster 3');
pause;
subplot(2,3,6),imshow(img_seg{4}), title('cluster 4');
I tried implementing the same procedure by converting the image to HSV space
MATLAB code:
clear all;close ALL;
A=imread('stopcup.jpg');
A = rgb2hsv(A); %converting the image to HSV space
imhist(A(:,:,1)),figure, imshow(A(:,:,1));
I know the concept of ranging the threshold values for each of the four colors from the image histogram:
My question is how do I add the respective threshold value ranges in to the mentioned coding (that I used prior to image conversion to HSV) thereby getting the corresponding output? Could someone just highlight the section of my coding, by including threshold range coding for at least one threshold range, where I should add the threshold values please?
Note: A "threshold" of the hue component of pixels must be an interval, because the hue actually wraps around and is best envisioned as a circle. Thus, to segment a blue region, I need to accept only hues around 2/3 (0 = red, 1/3 = green, 2/3 = blue).
Thanks, Shyam
0 Comments
Answers (1)
Image Analyst
on 29 Oct 2015
Run the color thresholder app on the Apps tab and have it generate code for you.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!