2010-03-17 69 views
2

我在比较不同单元阵列中的元素时遇到了一些问题。MATLAB:图像角坐标和单元阵列的参考

这个问题的上下文是我使用MATLAB中的bwboundaries函数来跟踪图像的轮廓。图像是一个结构横截面,我试图找出整个部分是否具有连续性(即只有一个由bwboundaries命令产生的轮廓)。

已经完成了这一步,发现其中有多于一个区段被跟踪(即它不是连续的),我使用了cornermetric命令来查找每个区段的角落。

我的代码是:

%% Define the structural section as a binary matrix (Image is an I-section with the web broken) 
bw(20:40,50:150) = 1; 
bw(160:180,50:150) = 1; 
bw(20:60,95:105) = 1; 
bw(140:180,95:105) = 1; 

Trace = bw; 
[B] = bwboundaries(Trace,'noholes'); %Traces the outer boundary of each section 

L = length(B); % Finds number of boundaries 
if L > 1 
    disp('Multiple boundaries') % States whether more than one boundary found 
end 

%% Obtain perimeter coordinates 
for k=1:length(B) %For all the boundaries 
    perim = B{k}; %Obtains perimeter coordinates (as a 2D matrix) from the cell array 
end 

%% Find the corner positions 
C = cornermetric(bw); 

Areacorners = find(C == max(max(C))) % Finds the corner coordinates of each boundary 

[rowindexcorners,colindexcorners] = ind2sub(size(Newgeometry),Areacorners) 
% Convert corner coordinate indexes into subcripts, to give x & y coordinates (i.e. the same format as B gives) 

%% Put these corner coordinates into a cell array 
Cornerscellarray = cell(length(rowindexcorners),1); % Initialises cell array of zeros 
for i =1:numel(rowindexcorners) 
    Cornerscellarray(i) = {[rowindexcorners(i) colindexcorners(i)]}; 
    %Assigns the corner indicies into the cell array 
    %This is done so the cell arrays can be compared 
end 

for k=1:length(B) %For all the boundaries found 
    perim = B{k}; %Obtains coordinates for each perimeter 
    Z = perim; % Initialise the matrix containing the perimeter corners 

    Sectioncellmatrix = cell(length(rowindexcorners),1); 
    for i =1:length(perim) 
     Sectioncellmatrix(i) = {[perim(i,1) perim(i,2)]}; 
    end 

    for i = 1:length(perim) 
     if Sectioncellmatrix(i) ~= Cornerscellarray 
      Sectioncellmatrix(i) = []; 
      %Gets rid of the elements that are not corners, but keeps them associated with the relevent section 
     end 
    end 
end 

这在过去的for循环创建了一个错误。有没有办法可以检查数组中的每个单元格(包含x和y坐标)是否等于cornercellarray中的任何坐标对?我知道用矩阵可以比较某个元素是否与另一个矩阵中的任何元素相匹配。我希望能够在这里做同样的事情,但是对于单元阵列中的坐标对。

我之所以不单单使用单元阵列本身,是因为它列出了所有的角坐标,并且不会将它们与特定的跟踪边界关联起来。

回答

3

无法用等号进行多对多比较。您需要使用ismember。

%# catenate all corners in one big corner array 
Cornerscellarray = cat(1,Cornerscellarray{:}); 

%# loop through each section cell and remove all that is not corners 
for i = 1:length(perim) 
    %# check for corners 
    cornerIdx = ismember(Sectioncellmatrix{i},Cornerscellarray,'rows'); 

    %# only keep good entries 
    Sectioncellmatrix{i} = Sectioncellmatrix{i}(cornerIdx,:); 
end 

此外,这段代码真的看起来可能会有点优化。例如,您可以使用bwlabel来标记数组,使用角坐标读取标签以将角点与特征相关联。

像这样:

bw(20:40,50:150) = 1; 
bw(160:180,50:150) = 1; 
bw(20:60,95:105) = 1; 
bw(140:180,95:105) = 1; 

%# get corners 
cornerProbability = cornermetric(bw); 
cornerIdx = find(cornerProbability==max(cornerProbability(:))); 

%# Label the image. bwlabel puts 1 for the first feature, 2 for the second, etc. 
%# Since concave corners are placed just outside the feature, grow the features 
%# a little before labeling 
bw2 = imdilate(bw,ones(3)); 
labeledImage = bwlabel(bw2); 

%# read the feature number associated with the corner 
cornerLabels = labeledImage(cornerIdx); 

%# find all corners that are associated with feature 1 
corners_1 = cornerIdx(cornerLabels==1); 
+0

谢谢,那是真正有用的,我以前不知道的bwlabel功能。 – James 2010-03-17 15:08:09

+0

@James:这是关于Matlab的好东西之一:由于许多函数和工具箱,即使对于复杂的操作,也可以很少打字。 – Jonas 2010-03-19 14:26:55