title: Find the corners of a polygon represented by a region mask
body: <blockquote>
    <p><code>BW = poly2mask(x, y, m, n)</code> computes a
    binary region of interest (ROI) mask,
    BW, from an ROI polygon, represented
    by the vectors x and y. The size of BW
    is m-by-n. </p>
    
    <p><code>poly2mask</code> sets pixels in BW
    that are inside the polygon (X,Y) to 1
    and sets pixels outside the polygon to
    0.</p>
  </blockquote>
  
  <p><strong>Problem:</strong>
  Given such a binary mask <code>BW</code> of a convex quadrilateral, what would be the most efficient way to determine the four corners?</p>
  
  <p>E.g.,</p>
  
  <p><img src="https://imgur.com/oxyjj.png" alt="Example"></p>
  
  <p><strong>Best Solution so far:</strong>
  Use <code>edge</code> to find the bounding lines, the Hough transform to find the 4 lines in the edge image and then find the intersection points of those 4 lines or use a corner detector on the edge image. Seems complicated, and I can't help feeling there's a simpler solution out there. </p>
  
  <p>Btw, <code>convhull</code> doesn't always return 4 points (maybe someone can suggest <code>qhull</code> options to prevent that) : it returns a few points along the edges as well.</p>
  
  <p><strong>EDIT:</strong>
  <a href="https://stackoverflow.com/questions/1711916/find-the-corners-of-a-polygon-represented-by-a-region-mask/1713400#1713400">Amro's answer</a> seems quite elegant and efficient. But there could be multiple "corners" at each real corner since the peaks aren't unique. I could cluster them based on <strong>θ</strong> and average the "corners" around a real corner but the main problem is the use of <code>order(1:10)</code>. </p>
  
  <p>Is <code>10</code> enough to account for all the corners or will this exclude a "corner" at a real corner?</p>