# MATLAB: image corner coordinates & referncing to cell arrays

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.

## Answers

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);