def draw_subdiv_facet(img, edge): t = edge count = 0 # count number of edges in facet while count == 0 or t != edge: count += 1 t = cv.Subdiv2DGetEdge(t, cv.CV_NEXT_AROUND_LEFT) buf = [] # gather points t = edge for i in range(count): assert t > 4 pt = cv.Subdiv2DEdgeOrg(t) if not pt: break buf.append((cv.Round(pt.pt[0]), cv.Round(pt.pt[1]))) t = cv.Subdiv2DGetEdge(t, cv.CV_NEXT_AROUND_LEFT) if (len(buf) == count): pt = cv.Subdiv2DEdgeDst(cv.Subdiv2DRotateEdge(edge, 1)) cv.FillConvexPoly( img, buf, cv.RGB(random.randrange(256), random.randrange(256), random.randrange(256)), cv.CV_AA, 0) cv.PolyLine(img, [buf], 1, cv.RGB(0, 0, 0), 1, cv.CV_AA, 0) draw_subdiv_point(img, pt.pt, cv.RGB(0, 0, 0))
def get_working(self): (width, height) = cv.GetSize(self.image) dest = cv.CreateMat(height, width, cv.CV_8UC3) mask8x1 = cv.CreateImage(cv.GetSize(self.image), 8, 1) cv.Zero(mask8x1) cv.FillConvexPoly(mask8x1, self.cur_contour, cv.ScalarAll(255)) # Could 8x3 mask copy but histogram mask will take care of it cv.Copy(self.image, dest) return (dest, mask8x1)
def compute_ref(self): '''Compute a reference histogram that matched regions should approximate''' (image, polygon) = self.get_ref() (width, height) = cv.GetSize(image) # (rows, cols,...) dest = cv.CreateMat(height, width, cv.CV_8UC3) mask8x1 = cv.CreateImage(cv.GetSize(image), 8, 1) cv.Zero(mask8x1) cv.FillConvexPoly(mask8x1, polygon, cv.ScalarAll(255)) cv.Copy(image, dest) self.ref_hist = hs_histogram(dest, mask8x1)
def test_2542670(self): xys = [(94, 121), (94, 122), (93, 123), (92, 123), (91, 124), (91, 125), (91, 126), (92, 127), (92, 128), (92, 129), (92, 130), (92, 131), (91, 132), (90, 131), (90, 130), (90, 131), (91, 132), (92, 133), (92, 134), (93, 135), (94, 136), (94, 137), (94, 138), (95, 139), (96, 140), (96, 141), (96, 142), (96, 143), (97, 144), (97, 145), (98, 146), (99, 146), (100, 146), (101, 146), (102, 146), (103, 146), (104, 146), (105, 146), (106, 146), (107, 146), (108, 146), (109, 146), (110, 146), (111, 146), (112, 146), (113, 146), (114, 146), (115, 146), (116, 146), (117, 146), (118, 146), (119, 146), (120, 146), (121, 146), (122, 146), (123, 146), (124, 146), (125, 146), (126, 146), (126, 145), (126, 144), (126, 143), (126, 142), (126, 141), (126, 140), (127, 139), (127, 138), (127, 137), (127, 136), (127, 135), (127, 134), (127, 133), (128, 132), (129, 132), (130, 131), (131, 130), (131, 129), (131, 128), (132, 127), (133, 126), (134, 125), (134, 124), (135, 123), (136, 122), (136, 121), (135, 121), (134, 121), (133, 121), (132, 121), (131, 121), (130, 121), (129, 121), (128, 121), (127, 121), (126, 121), (125, 121), (124, 121), (123, 121), (122, 121), (121, 121), (120, 121), (119, 121), (118, 121), (117, 121), (116, 121), (115, 121), (114, 121), (113, 121), (112, 121), (111, 121), (110, 121), (109, 121), (108, 121), (107, 121), (106, 121), (105, 121), (104, 121), (103, 121), (102, 121), (101, 121), (100, 121), (99, 121), (98, 121), (97, 121), (96, 121), (95, 121)] #xys = xys[:12] + xys[16:] pts = cv.CreateMat(len(xys), 1, cv.CV_32SC2) for i,(x,y) in enumerate(xys): pts[i,0] = (x, y) storage = cv.CreateMemStorage() hull = cv.ConvexHull2(pts, storage) hullp = cv.ConvexHull2(pts, storage, return_points = 1) defects = cv.ConvexityDefects(pts, hull, storage) vis = cv.CreateImage((1000,1000), 8, 3) x0 = min([x for (x,y) in xys]) - 10 x1 = max([x for (x,y) in xys]) + 10 y0 = min([y for (y,y) in xys]) - 10 y1 = max([y for (y,y) in xys]) + 10 def xform(pt): x,y = pt return (1000 * (x - x0) / (x1 - x0), 1000 * (y - y0) / (y1 - y0)) for d in defects[:2]: cv.Zero(vis) # First draw the defect as a red triangle cv.FillConvexPoly(vis, [xform(p) for p in d[:3]], cv.RGB(255,0,0)) # Draw the convex hull as a thick green line for a,b in zip(hullp, hullp[1:]): cv.Line(vis, xform(a), xform(b), cv.RGB(0,128,0), 3) # Draw the original contour as a white line for a,b in zip(xys, xys[1:]): cv.Line(vis, xform(a), xform(b), (255,255,255)) self.snap(vis)