title: Finding edge and corner values of an image in MATLAB
body: <p>I am tracing the outline of an image and plotting this to a <a href="http://en.wikipedia.org/wiki/AutoCAD_DXF" rel="nofollow noreferrer">DXF</a> file. I would like to use the <code>bwboundaries</code> function to find the coordinates of the edges of the image, find the corner coordinates using the <code>cornermetric</code> function and then remove any edge coordinates that are not a corner.</p>
  
  <p>The important thing I need to be able to do is keep the order of the corner elements obtained from <code>bwboundaries</code>, so that the section traces properly. The DXF function I have, that draws from the coordinates, draws lines between coordinates that are next to each other, so the line has to be drawn "around" the section rather than straight between the corner points.</p>
  
  <p>The reason I am doing this is because there are less coordinates obtained this way, so it is easier to amend the DXF file (as there are less points to manipulate).</p>
  
  <p>The code I have so far is:</p>
  
  <pre><code>%# Shape to be traced
  bw = zeros(200);
  
  bw(20:40,20:180) = 1;
  bw(20:180,90:110) = 1;
  bw(140:180,20:185) = 1;
  
  %# Boundary finding section
  [Boundary] = bwboundaries(bw); %Traces the boundary of each section.
  
  figure, imshow(bw); hold on;
  colors=['b' 'g' 'r' 'c' 'm' 'y'];
  
  for k = 1:length(Boundary)
     perim = Boundary{k}; %Obtains perimeter coordinates (as a 2D matrix) from the cell array.
     cidx = mod(k,length(colors))+1;% Obtains colours for the plot
     plot(perim(:,2), perim(:,1),...
            colors(cidx),'LineWidth',2);
  end
  
  Coordmat = cell2mat(Boundary) %Converts the traced regions to a matrix.
  
  X = Coordmat(:,1)
  Y = Coordmat(:,2) % This gives the edge coordinates in matrix form.
  
  %% Corner finding section (from Jonas' answer to a previous question.
  
  %# 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)
  
  [Xcorners, Ycorners] = ind2sub(200,corners_1) % Convert subscripts
  </code></pre>
  
  <p>The code I have is, to give a matrix, <code>Xfin</code>, for the final x coordinates (which are on the edge AND at a corner.</p>
  
  <pre><code>Xfin = zeros(length(X),1)
  
  for i = Xcorners
      XFin(i) = Xcorners
      if i~= Xcorners
          XFin(i) = []
      end
  end
  </code></pre>
  
  <p>However, this does not work correctly, because the values in the solution are sorted into order, and only one of each value remains. As I said, I would like the corner elements to be in the same order as obtained from bwboundaries, to allow the image to trace properly.</p>
  
  <p>(This problem links to two other questions I've asked on here.)</p>