2

異なるセル配列の要素を比較する際に問題が発生しています。

この問題のコンテキストはbwboundaries、MATLAB の関数を使用して画像の輪郭をトレースしていることです。画像は構造断面であり、断面全体に連続性があるかどうかを確認しようとしています (つまり、bwboundariesコマンドによって生成されるアウトラインは 1 つだけです)。

これを実行して、トレースされた複数のセクション (つまり、連続していない) を見つけたので、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セル配列自体を使用しない理由は、これがすべてのコーナー座標をリストし、それらを特定のトレース境界に関連付けないためです。

4

1 に答える 1

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);
于 2010-03-17T14:37:57.063 に答える