Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>you can use <a href="http://pypi.python.org/pypi/Polygon/2.0.4" rel="nofollow noreferrer">http://pypi.python.org/pypi/Polygon/2.0.4</a>, here is an example:</p> <pre><code>&gt;&gt;&gt; import Polygon &gt;&gt;&gt; a = Polygon.Polygon([(0,0),(1,0),(0,1)]) &gt;&gt;&gt; b = Polygon.Polygon([(0.3,0.3), (0.3, 0.6), (0.6, 0.3)]) &gt;&gt;&gt; a &amp; b Polygon: &lt;0:Contour: [0:0.60, 0.30] [1:0.30, 0.30] [2:0.30, 0.60]&gt; </code></pre> <p>To convert the result of cv2.findContours to Polygon point format, you can:</p> <pre><code>points1 = contours[0].reshape(-1,2) </code></pre> <p>This will convert the shape from (N, 1, 2) to (N, 2)</p> <p>Following is a full example:</p> <pre><code>import Polygon import cv2 import numpy as np from scipy.misc import bytescale y, x = np.ogrid[-2:2:100j, -2:2:100j] f1 = bytescale(np.exp(-x**2 - y**2), low=0, high=255) f2 = bytescale(np.exp(-(x+1)**2 - y**2), low=0, high=255) c1, hierarchy = cv2.findContours((f1&gt;120).astype(np.uint8), cv2.cv.CV_RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) c2, hierarchy = cv2.findContours((f2&gt;120).astype(np.uint8), cv2.cv.CV_RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) points1 = c1[0].reshape(-1,2) # convert shape (n, 1, 2) to (n, 2) points2 = c2[0].reshape(-1,2) import pylab as pl poly1 = pl.Polygon(points1, color="blue", alpha=0.5) poly2 = pl.Polygon(points2, color="red", alpha=0.5) pl.figure(figsize=(8,3)) ax = pl.subplot(121) ax.add_artist(poly1) ax.add_artist(poly2) pl.xlim(0, 100) pl.ylim(0, 100) a = Polygon.Polygon(points1) b = Polygon.Polygon(points2) intersect = a&amp;b # calculate the intersect polygon poly3 = pl.Polygon(intersect[0], color="green") # intersect[0] are the points of the polygon ax = pl.subplot(122) ax.add_artist(poly3) pl.xlim(0, 100) pl.ylim(0, 100) pl.show() </code></pre> <p>Output:</p> <p><img src="https://i.stack.imgur.com/mPx4I.png" alt="enter image description here"></p>
 

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