【问题标题】:MATLAB: image corner coordinates & referncing to cell arraysMATLAB:图像角坐标和引用元胞数组
【发布时间】:2010-03-17 14:18:02
【问题描述】:

我在比较不同元胞数组中的元素时遇到了一些问题。

这个问题的背景是我在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 中的任何坐标对?我知道矩阵可以比较某个元素是否与另一个矩阵中的任何元素匹配。我希望能够在这里做同样的事情,但对于单元格数组中的一对坐标。

我不只使用 cornercellarray 元胞数组本身的原因是因为它列出了所有角坐标并且没有将它们与特定的跟踪边界相关联。

【问题讨论】:

    标签: matlab image-processing cell-array


    【解决方案1】:

    等号不能进行多对多比较。您需要改用 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);
    

    【讨论】:

    • 谢谢,这真的很有帮助,我之前不知道 bwlabel 功能。
    • @James:这是 Matlab 的优点之一:由于有许多函数和工具箱,即使是复杂的操作,您也只需很少的输入即可。
    猜你喜欢
    • 2014-11-17
    • 2011-07-17
    • 2018-02-03
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    相关资源
    最近更新 更多