applying colour for clustered image

i have a code below of 2 images
for n=1:length(dirlist);
img = imread([pathname, dirlist(n).name]);
I=img;
HSV = rgb2hsv(I);
%HSV=I;
H = HSV(:,:,1); H = H(:);
S = HSV(:,:,2); S = S(:);
V = HSV(:,:,3); V = V(:);
idx = kmeans([H S V],3);
figure,imshow(ind2rgb(reshape(idx, size(I,1), size(I, 2)), [0 0 1; 0 0.8 0;1 0 0]))
end
the image consist of water,and land
one image consist of 70% of water and other 40% of water is it possible to give same colour for water in two images
plz assist

Antworten (2)

ChristianW
ChristianW am 1 Mär. 2013

0 Stimmen

You can define the water color. Than calculate the mean color for every cluster. Compare the mean cluster color with your defined water color to find your water cluster.
You could also use the mean cluster color directly as cluster color.

11 Kommentare

kash
kash am 1 Mär. 2013
Thanks Christia can you plz tell what is mean cluster color
ChristianW
ChristianW am 1 Mär. 2013
Bearbeitet: ChristianW am 1 Mär. 2013
Mean [Red Green Blue] values for all pixels in a cluster.
Or use rgb2ind with nColor = 1:
RGB = imread('ngc6543a.jpg');
[X,map] = rgb2ind(RGB, 1); % map is the mean color
imtool(permute(map,[3 1 2]))
christain what must be done with this code,can you please tell in brief
My code for mean value for each cluster,plz tell what must be done next
HSV = rgb2hsv(I);
H = HSV(:,:,1); H = H(:);
S = HSV(:,:,2); S = S(:);
V = HSV(:,:,3); V = V(:);
idx = kmeans([H S V], 3);
% imshow(ind2rgb(reshape(idx, size(I,1), size(I, 2)), [0 0 1; 0 0.8 0]))
cluster_idx=idx;
pixel_labels = reshape(cluster_idx,size(I,1),size(I,2));
figure,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:3
color = I;
color(rgb_label ~= k) = 0;
segmented_images{k} = color;
end
figure,imshow(segmented_images{1}), title('objects in cluster 1');
figure,imshow(segmented_images{2}), title('objects in cluster 2');
figure,imshow(segmented_images{3}), title('objects in cluster 2');
Mean1=mean(mean(segmented_images{1}))
Mean2=mean(mean(segmented_images{2}))
Mean3=mean(mean(segmented_images{3}))
Image Analyst
Image Analyst am 1 Mär. 2013
Where can we find 'ngc6543a.jpg' so we can run this?
kash
kash am 1 Mär. 2013
My image consists of water and land
ChristianW
ChristianW am 1 Mär. 2013
Bearbeitet: ChristianW am 2 Mär. 2013
ngc6543a.jpg is a Matlab example image, but I guess IA is communicating the helpfulness of having access to your image.
You falsifies the mean values with a lot of black color. This is what I mean:
I = imread('ngc6543a.jpg');
HSV = I; %rgb2hsv(I);
H = HSV(:,:,1); H = H(:);
S = HSV(:,:,2); S = S(:);
V = HSV(:,:,3); V = V(:);
Y = double([H S V]);
nColors = 3;
idx = kmeans(Y,nColors,'Replicates',3);
% get the mean cluster colors
pixel_labels = reshape(idx,size(I,1),size(I,2)); % imshow(pixel_labels,[]);
[X,map] = rgb2ind(I,65536); % indexed original image
for i = 1:nColors
c(i,:) = mean(map(X(idx==i)+1,:)); %#ok<SAGROW>
end
imshow(pixel_labels,c)
% compare with Water color
Nova = [1 0 0]; % Nova color or Water
[C,Icmin] = min(sum((c-ones(size(c,1),1)*Nova).^2,2)); % distance
c(Icmin,:) = Nova;
imshow(pixel_labels,c)
As a note, kmeans crashed sometimes here, but thats your part of the code or it might be the example image.
Image Analyst
Image Analyst am 1 Mär. 2013
'ngc6543a.jpg' is not one of the official demo images that come with the Image Processing Toolbox, and located in folder C:\Program Files\MATLAB\R2012b\toolbox\images\imdemos. I don't know how/where to obtain the image.
ChristianW
ChristianW am 1 Mär. 2013
Its a Matlab image (not IPToolbox), at least in my version. Its still just an random image I took to run his code. But whatever, here you go: ngc6543a.jpg
kash
kash am 2 Mär. 2013
Christian thanks a lot for showing interest in my project ,I have two images consisting of water and land
i tried your code,but the water portion d assigned with different colours
kindly assist
ChristianW
ChristianW am 2 Mär. 2013
You are messing things up. I took your color/texture segmentation (implemented with kmeans), the segmentation isn't my part of the code. My part just takes a kmeans output and does a cluster affectation. It applies color for the clustered image, thats what you've asked for.
If your question is aimed at improving your segmentation, than Image Analyst is your best bet.
kash
kash am 2 Mär. 2013
my question also deals with this,having two images consisting of water,how can we give same colour for water for different images

Melden Sie sich an, um zu kommentieren.

Image Analyst
Image Analyst am 2 Mär. 2013

0 Stimmen

Looks like you can find the blue water fairly easily with color classification. The green water you'll have to find by adding in a texture filter like stdfilt() or entropyfilt() because the trees are very close to the same color and the only difference is the texture. The white stuff is either sand or surf on sand - either way white stuff is land. If you still need help, say so. I have color classification examples in my File Exchange http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862

9 Kommentare

kash
kash am 2 Mär. 2013
Bearbeitet: Walter Roberson am 2 Mär. 2013
I am stuck here for bit long,can u help me in giving an example please ,as per you instruction i did as
I=imread('water2.jpg');
d=I(:,:,1);
d1=I(:,:,2);
d2=I(:,:,3);
d=d(:);
d1=d1(:);
d2=d2(:);
d=stdfilt(d);
d1=stdfilt(d1);
d2=stdfilt(d2);
idx=kmeans([d d1 d2],3);
imshow(ind2rgb(reshape(idx, size(I,1), size(I, 2)), [0 0 1; 0 0.8 0]))
now how we can appply same colour for different images consisting of water
kindly assist
Walter Roberson
Walter Roberson am 2 Mär. 2013
Please stop indenting your text when you post. Only indent your code. I have fixed your posting yet again.
Walter Roberson
Walter Roberson am 2 Mär. 2013
In the image with the trees, I cannot tell whether the portion to the upper right is water (making the portion with trees either a spit of land or a portion of a lagoon), or if that portion is instead grass or something similar.
kash
kash am 2 Mär. 2013
Actuall walter colouring must be mase with more or less same properties ,
is it possible
kash
kash am 2 Mär. 2013
from the above two images is it possible to say ,each image consists of how much percentahe of water for each image,(only water)
Walter Roberson
Walter Roberson am 2 Mär. 2013
So in that image with the trees, is the upper right portion calm water or is it vegetation ?
kash
kash am 2 Mär. 2013
its water
Image Analyst
Image Analyst am 2 Mär. 2013
Exactly what does "Walter colouring must be mase" mean?
kash
kash am 5 Mär. 2013
I cannot understand what u said,what is walter colouring

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Mathematics finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 1 Mär. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by