MATLAB:影像角点坐标&引用单元格数组 [英] MATLAB: image corner coordinates & referncing to cell arrays
问题描述
比较不同单元阵列中的元素时遇到一些问题.
I am having some problems comparing the elements in different cell arrays.
此问题的上下文是我正在使用MATLAB中的bwboundaries
函数来跟踪图像的轮廓.该图像具有结构性横截面,我试图确定整个横截面是否连续(即bwboundaries
命令仅产生一个轮廓).
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).
完成此操作后,发现跟踪了多个部分(即不连续)的位置,我使用了cornermetric
命令来查找每个部分的角.
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.
我的代码是:
%% 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 中的任意一对坐标?我知道可以通过矩阵比较某个元素是否与另一个矩阵中的任何元素相匹配.我希望能够在此处执行相同的操作,但要针对单元格数组中的一对坐标.
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.
之所以我不仅仅使用 cornercellarray 单元格数组本身,是因为它列出了所有角坐标,并且没有将它们与特定的跟踪边界相关联.
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.
推荐答案
使用等号不能进行多对多比较.您需要改用ismember.
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
此外,这段代码看起来确实有些优化.例如,您可以使用bwlabel标记数组,读取带有角坐标的标签以将角与要素相关联.
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.
像这样:
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);
这篇关于MATLAB:影像角点坐标&引用单元格数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!