2017-04-20 82 views
0

我有一个像素等于0或255的配对矩阵,当它是255时,它会使磁盘形状。 我想标签不同的磁盘,并获得每个标签的标签:他们的半径和他们的中心点。我最后两点怎么办?从矩阵获得属性

下面的小矩阵的为例

Mat=zeros(12,12); 
Mat(2,6:7)=255; Mat(3,5)=255; Mat(3,8)=255; Mat(4,5)=255 
Mat(4,8)=255; Mat(5,6:7)=255; 

Mat(10,10)=255; Mat(11,9)=255; Mat(12,10)=255; Mat(11,11)=255; 

CC=bwconncomp(Mat,8); 
MatL=labelmatrix(CC); 

figure, imagesc(Mat) 
+0

怎么办你的意思是“获取标签”?什么是预期的输出? – rayryeng

+1

['regionprops'](https://nl.mathworks.com/help/images/ref/regionprops.html)可能就是你要找的。 – m7913d

回答

0

可以使用regionprops来计算质心和面积,然后用面积来计算近似半径:

% generate matrix 
Mat=zeros(12,12); 
Mat(2,6:7)=255; Mat(3,5)=255; Mat(3,8)=255; Mat(4,5)=255; 
Mat(4,8)=255; Mat(5,6:7)=255; 
Mat(10,10)=255; Mat(11,9)=255; Mat(12,10)=255; Mat(11,11)=255; 
% convert to binary 
MatBin = Mat > 0; 
% fill circles 
MatFull = imfill(MatBin,4,'holes'); 
% get centroids and areas 
props = regionprops(MatFull,{'Area','Centroid'}); 
Area = [props(:).Area]; 
Centroid = reshape([props(:).Centroid],[],2)'; 
% compute radius 
Radius = sqrt(Area ./ pi); 
% plotting 
imshow(MatFull,[],'InitialMagnification','fit') 
hold on 
for ii = 1:numel(Radius) 
    text(Centroid(ii,1),Centroid(ii,2),['r = ' num2str(Radius(ii))],... 
     'VerticalAlignment','middle','HorizontalAlignment',... 
     'center','FontSize',12,'Color','b'); 
end 
hold off 

enter image description here