MATLAB:图像角坐标&引用元胞数组

发布于 2024-08-25 15:56:51 字数 2332 浏览 3 评论 0原文

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

这个问题的背景是我正在使用 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 元胞数组本身的原因是因为它列出了所有角坐标并且不将它们与特定的跟踪边界相关联。

I am having some problems comparing the elements in different cell arrays.

The context of this problem is that I am using the bwboundaries function in MATLAB to trace the outline of an image. The image is of a structural cross section and I am trying to find if there is continuity throughout the section (i.e. there is only one outline produced by the bwboundaries command).

Having done this and found where the is more than one section traced (i.e. it is not continuous), I have used the cornermetric command to find the corners of each section.

The code I have is:

%% 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

This creates an error in the last for loop. Is there a way I can check whether each cell of the array (containing an x and y coordinate) is equal to any pair of coordinates in cornercellarray? I know it is possible with matrices to compare whether a certain element matches any of the elements in another matrix. I want to be able to do the same here, but for the pair of coordinates within the cell array.

The reason I don't just use the cornercellarray cell array itself, is because this lists all the corner coordinates and does not associate them with a specific traced boundary.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半衬遮猫 2024-09-01 15:56:51

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

Many-to-many comparisons cannot be done with the equal sign. You need to use ismember instead.

%# 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

Also, this code really looks like it could be a bit optimized. For example, you could use bwlabel to label your arrays, read the label with the corner coordinates to associate corners with the features.

Like so:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文