def findEllipses(edges):
contours, _ = cv2.findContours(edges.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
ellipseMask = np.zeros(edges.shape, dtype=np.uint8)
contourMask = np.zeros(edges.shape, dtype=np.uint8)
pi_4 = np.pi * 4
for i, contour in enumerate(contours):
if len(contour) < 5:
continue
area = cv2.contourArea(contour)
if area <= 100: # skip ellipses smaller then 10x10
continue
arclen = cv2.arcLength(contour, True)
circularity = (pi_4 * area) / (arclen * arclen)
ellipse = cv2.fitEllipse(contour)
poly = cv2.ellipse2Poly((int(ellipse[0][0]), int(ellipse[0][1])), (int(ellipse[1][0] / 2), int(ellipse[1][1] / 2)), int(ellipse[2]), 0, 360, 5)
# if contour is circular enough
if circularity > 0.6:
cv2.fillPoly(ellipseMask, [poly], 255)
continue
# if contour has enough similarity to an ellipse
similarity = cv2.matchShapes(poly.reshape((poly.shape[0], 1, poly.shape[1])), contour, cv2.cv.CV_CONTOURS_MATCH_I2, 0)
if similarity <= 0.2:
cv2.fillPoly(contourMask, [poly], 255)
return ellipseMask, contourMask
python类ellipse2Poly()的实例源码
def gl_display(self):
"""
use gl calls to render
at least:
the published position of the reference
better:
show the detected postion even if not published
"""
# debug mode within world will show green ellipses around detected ellipses
if self.active and self.detected:
for marker in self.markers:
e = marker[-1] #outermost ellipse
pts = cv2.ellipse2Poly( (int(e[0][0]),int(e[0][1])),
(int(e[1][0]/2),int(e[1][1]/2)),
int(e[-1]),0,360,15)
draw_polyline(pts,1,RGBA(0.,1.,0.,1.))
else:
pass
if self._window:
self.gl_display_in_window()
def gl_display(self):
"""
use gl calls to render
at least:
the published position of the reference
better:
show the detected postion even if not published
"""
if self.active:
draw_points_norm([self.smooth_pos],size=15,color=RGBA(1.,1.,0.,.5))
if self.active and self.detected:
for marker in self.markers:
e = marker[-1]
pts = cv2.ellipse2Poly( (int(e[0][0]),int(e[0][1])),
(int(e[1][0]/2),int(e[1][1]/2)),
int(e[-1]),0,360,15)
draw_polyline(pts,color=RGBA(0.,1.,0,1.))
else:
pass
def gl_display(self):
"""
use gl calls to render
at least:
the published position of the reference
better:
show the detected postion even if not published
"""
if self.active:
draw_points_norm([self.smooth_pos],size=15,color=RGBA(1.,1.,0.,.5))
if self.active and self.detected:
for marker in self.markers:
e = marker[-1]
pts = cv2.ellipse2Poly( (int(e[0][0]),int(e[0][1])),
(int(e[1][0]/2),int(e[1][1]/2)),
int(e[-1]),0,360,15)
draw_polyline(pts,color=RGBA(0.,1.,0,1.))
if self.counter:
# lets draw an indicator on the count
e = self.markers[0][-1]
# cv2 requires integer arguments
pts = cv2.ellipse2Poly( (int(e[0][0]),int(e[0][1])),
(int(e[1][0]/2),int(e[1][1]/2)),
int(e[-1]),0,360,360//self.counter_max)
indicator = [e[0]] + pts[self.counter:].tolist()[::-1] + [e[0]]
draw_polyline(indicator,color=RGBA(0.1,.5,.7,.8),line_type=GL_POLYGON)
if self.auto_stop:
# lets draw an indicator on the autostop count
e = self.markers[0][-1]
# cv2 requires integer arguments
pts = cv2.ellipse2Poly( (int(e[0][0]),int(e[0][1])),
(int(e[1][0]/2),int(e[1][1]/2)),
int(e[-1]),0,360,360//self.auto_stop_max)
indicator = [e[0]] + pts[self.auto_stop:].tolist() + [e[0]]
draw_polyline(indicator,color=RGBA(8.,0.1,0.1,.8),line_type=GL_POLYGON)
else:
pass