How to find pixels of one cluster that are enclosed by pixels of another cluster?

5 ビュー (過去 30 日間)
Ashutosh Gowarikar
Ashutosh Gowarikar 2019 年 5 月 16 日
回答済み: Image Analyst 2019 年 5 月 25 日
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?

回答 (2 件)

KSSV
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')
  1 件のコメント
Ashutosh Gowarikar
Ashutosh Gowarikar 2019 年 5 月 16 日
I probably don't understand your code. But it doesn't really get what I am looking for. Please correct me if I am wrong. But I think it takes cluster boundaries individually for all three clusters and plots all the pixels irrespective of cluster group within a specified boundary (in this case, region 1).
If you see the image 2 attached, will notice that blue some blue pixels are bounded by cyan pixels. I only want to highlight such blue pixels.
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
plot(x(idx==2),y(idx==2),'.b')
plot(x(idx==3),y(idx==3),'.c')

サインインしてコメントする。


Image Analyst
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;

カテゴリ

Help Center および File ExchangeGet Started with Image Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by