Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is an simple way to compute the overlap between an image and a polygon?
    primarykey
    data
    text
    <p>I have a closed non-self-intersecting polygon. Its vertices are saved in two vectors X, and Y. Finally the values of X and Y are bound between 0 and 22.</p> <p>I'd like to construct a matrix of size 22x22 and set the value of each bin equal to true if part of the polygon overlaps with that bin, otherwise false.</p> <p>My initial thought was to generate a grid of points defined with <code>[a, b] = meshgrid(1:22)</code> and then to use <code>inpolygon</code> to determine which points of the grid were in the polygon.</p> <pre><code>[a b] = meshgrid(1:22); inPoly1 = inpolygon(a,b,X,Y); </code></pre> <p>However this only returns true if if the center of the bin is contained in the polygon, ie it returns the red shape in the image below. However what need is more along the lines of the green shape (although its still an incomplete solution).</p> <p>To get the green blob I performed four calls to <code>inpolygon</code>. For each comparison I shifted the grid of points either NE, NW, SE, or SW by 1/2. This is equivalent to testing if the corners of a bin are in the polygon.</p> <pre><code>inPoly2 = inpolygon(a-.5,b-.5,X,Y) | inpolygon(a+.5,b-.5,X,Y) | inpolygon(a-.5,b+5,X,Y) | inpolygon(a+.5,b+.5,X,Y); </code></pre> <p>While this does provide me with a partial solution it fails in the case when a vertex is contain in a bin but none of the bin corners are.</p> <p>Is there a more direct way of attacking this problem, with preferably a solution that produces more readable code?</p> <p><img src="https://i.stack.imgur.com/xcH9x.png" alt="enter image description here"></p> <p>This plot was drawn with:</p> <pre><code>imagesc(inPoly1 + inPoly2); hold on; line(a, b, 'w.'); line(X, Y, 'y); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload