How to find pixels of one cluster that are enclosed by pixels of another cluster?
5 ビュー (過去 30 日間)
古いコメントを表示
I have this image that I have clustered using k-means method. Here, k = 3. Code provided by KSSV (contributor from matlab community). This is the code:
I = imread('image 2.jpg') ;
[y,x] = find(I) ;
idx = I>0 ;
z = I(idx) ;
% Use kmeans
G = 3 ; % number of groups
idx = kmeans(z,G) ;
% plot
imshow(I)
hold on
for i = 1:G
plot(x(idx==i),y(idx==i),'.')
end
% figure
% scatter(x,y,10,z,'s','filled') ; colorbar
So, after clustering I get 'image 3.jpg' attach. There are 3 clusters in the image: cluster 1 (light orange), cluster 2 (dark orange) and cluster 3 (blue).
I want to scan the image column by column to get the coordinates for the pixels which belong to cluster 2 and are enclosed by cluster 1 pixels.
Can someone please help with the code?
0 件のコメント
回答 (2 件)
KSSV
2019 年 5 月 16 日
What you need to use is boundary and inpolygon. You need not to scan through every column. Check the below code.
I = imread('image 2.jpg') ;
[y,x] = find(I) ;
idx = I>0 ;
z = I(idx) ;
% Use kmeans
G = 3 ; % number of groups
idx = kmeans(z,G) ;
% Sort the groups
[val,idx] = sort(idx) ;
x = x(idx) ; y = y(idx) ; z = z(idx) ;
idx = val ;
% plot
imshow(I)
hold on
for i = 1:G
plot(x(idx==i),y(idx==i),'.')
end
%% Get boundary of each cluster
xx = cell(G,1) ; yy = cell(G,1) ;
for i = 1:G
xi = x(idx==i) ; yi = y(idx==i) ;
bd = boundary(x(idx==i),y(idx==i)) ;
xx{i} = xi(bd) ;
yy{i} = yi(bd) ;
end
%% Get the points lying inside region 1
idx_1 = inpolygon(x,y,xx{1},yy{1}) ;
figure
hold on
plot(xx{1},yy{1})
plot(x(idx_1),y(idx_1),'.r')
title('Points lying inside Region 1')
Image Analyst
2019 年 5 月 25 日
Get a binary image of each cluster by itself. Then to find which in cluster2 are also in cluster1, do an AND operation
inBoth = cluster2 & cluster1;
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!