def flist(self): nx, ny = self.size() fl = [] for i in range(0, nx): for j in range(0, ny): p1 = vector.vector(self.xl[i], self.yl[j]) if i+1<nx: p2 = vector.vector(self.xl[i+1], self.yl[j]) fl.append(lineseg.lineseg(p1, p2)) if j+1<ny: p3 = vector.vector(self.xl[i], self.yl[j+1]) fl.append(lineseg.lineseg(p1, p3)) return fl
def __init__(self, *pts): self.pts = [] self.fl = [] if isinstance(pts[0], list): self.pts = pts else: for p in pts: self.pts.append(p) self.size_ = len(self.pts) self.pts.append(self.pts[0]) for i in range(0, self.size_): self.fl.append(lineseg.lineseg(self.pts[i], self.pts[i+1]))
def _check_inside(self, ps, pe): ldet = lineseg.lineseg(ps, pe) nsects = 0 psects = [] for l in self.ls: isectd, psect = lineseg.intersect(ldet, l) if isectd: append = False if len(psects)>0: for pchk in psects: chk = vector.mag(pchk - psect) if chk > 1e-10: append = True else: append = True if append: nsects += 1 psects.append(psect) return nsects
# Display the image. cv2.imshow("window", im) cv2.waitKey(0) cv2.destroyAllWindows() return contours if __name__ == '__main__': img = cv2.imread('canny_img2.png') data = np.asarray(find_contours(img)) # print 'data shape ', data.shape[0] seglist = lineseg(data, tol=2) # ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() # print seglist # for index, item in enumerate(seglist): # print index drawedgelist(seglist, rowscols=[480, 640]) # for i in seglist[0][:, 0]: # x.append(seglist[0][i, 0]) # x = seglist[1][:, 0] # y = seglist[1][:, 1] # print 'x ', x[0] # print 'y ', seglist[0][:, 1]
def intersectedArea(self, g, Debug=False): if not self.intersect(g): return -1 #--- search the pivot point ist = 0 poly = [] for i in range(0, self.size()): if g.inside(self.pts[i]): poly.append(self.pts[ist]) ist = i break lseg = lineseg.lineseg(self.pts[i], self.pts[i+1]) isectd, psect, iseg = g.intersect(lseg) if isectd: poly.append(psect) ist = i break #--- Walk along boundary of intersected area # cag : cell as geometry cag = geometry(self.points(False)) # Loop over the cell sides for i in range(ist+1, self.size()+1): p = self.pts[i] # f: a side of this cell f = lineseg.lineseg(self.pts[i-1], self.pts[i]) # Check if the side f intersect with given geometry g # isectd: boolean if g and f intersected or not # psect : intersected point # iseg : index of intersected segment of g isectd, psect, iseg = g.intersect(f) if isectd: # Check if psect has been added to poly already if not iselem(psect, poly): poly.append(psect) # Walk along the geometry g to find another intersect point of # this cell and g. The geometry points inbetween the two # intersected points are added to the poly for i in range(iseg+1, g.size()): pchk = g.pts[i] if cag.inside(pchk) and \ not iselem(pchk, poly): poly.append(pchk) if g.inside(p) and not iselem(p, poly): poly.append(p) if Debug: print('\nPoints in this poly:') for l in poly: print(l) # Estimate the area if len(poly) == 3: v1 = poly[1] - poly[0] v2 = poly[2] - poly[1] return 0.5*vector.mag(vector.cross(v1, v2)) else: sumA = 0.0 for i in range(0, len(poly)-2): v1 = poly[i+1] - poly[0] v2 = poly[i+2] - poly[0] sumA += 0.5*vector.mag(vector.cross(v1, v2)) return sumA
def getseg(self, ind): ps = self.pts[ind] pe = self.pts[ind+1] return lineseg.lineseg(ps, pe)