def plate_with_hole(size=.05): fd = lambda p: dm.ddiff(dm.drectangle(p,-1,1,-1,1), dm.dcircle(p,0,0,0.5)) fh = lambda p: 0.05+0.3*dm.dcircle(p,0,0,0.5) p, t = dm.distmesh2d(fd, fh, size, (-1,-1,1,1), [(-1,-1),(-1,1),(1,-1),(1,1)]) return p, t
def plate_with_hole(size=.05): fd = lambda p: dm.ddiff(dm.drectangle(p, -1, 1, -1, 1), dm.dcircle(p, 0, 0, 0.5)) fh = lambda p: 0.05 + 0.3 * dm.dcircle(p, 0, 0, 0.5) p, t = dm.distmesh2d(fd, fh, size, (-1, -1, 1, 1), [(-1, -1), (-1, 1), (1, -1), (1, 1)]) return p, t
def naca0012_airfoil(): """NACA0012 airfoil""" hlead = 0.01 htrail = 0.04 hmax = 2 circx = 2 circr = 4 a = .12 / .2 * np.array([0.2969, -0.1260, -0.3516, 0.2843, -0.1036]) a0 = a[0] a1 = np.hstack((a[5:0:-1], 0.0)) fd = lambda p: dm.ddiff(dm.dcircle(p, circx, 0, circr), (abs( p[:, 1]) - np.polyval(a1, p[:, 0]))**2 - a0**2 * p[:, 0]) fh = lambda p: np.minimum( np.minimum(hlead + 0.3 * dm.dcircle(p, 0, 0, 0), htrail + 0.3 * dm. dcircle(p, 1, 0, 0)), hmax) fixx = 1.0 - htrail * np.cumsum(1.3**np.arange(5)) fixy = a0 * np.sqrt(fixx) + np.polyval(a1, fixx) fix = np.vstack( (np.array([(circx - circr, 0), (circx + circr, 0), (circx, -circr), (circx, circr), (0, 0), (1, 0)]), np.vstack( (fixx, fixy)).T, np.vstack((fixx, -fixy)).T)) box = (circx - circr, -circr, circx + circr, circr) h0 = min(hlead, htrail, hmax) return dm.distmesh2d(fd, fh, h0, box, fix)
def rectangle_with_circular_hole(): """Rectangle with circular hole, refined at circle boundary""" fd = lambda p: dm.ddiff(dm.drectangle(p, -1, 1, -1, 1), dm.dcircle(p, 0, 0, 0.5)) fh = lambda p: 0.05 + 0.3 * dm.dcircle(p, 0, 0, 0.5) return dm.distmesh2d(fd, fh, 0.05, (-1, -1, 1, 1), [(-1, -1), (-1, 1), (1, -1), (1, 1)])
def fd10(p): r = np.sqrt(p[:,0]**2 + p[:,1]**2) z = p[:,2] d1 = r-1.0 d2 = z-1.0 d3 = -z-1.0 d4 = np.sqrt(d1**2+d2**2) d5 = np.sqrt(d1**2+d3**2) d = dm.dintersect(dm.dintersect(d1, d2), d3) ix = (d1>0)*(d2>0) d[ix] = d4[ix] ix = (d1>0)*(d3>0) d[ix] = d5[ix] return dm.ddiff(d, dm.dsphere(p, 0,0,0, 0.5))
def fd10(p): r = np.sqrt(p[:, 0]**2 + p[:, 1]**2) z = p[:, 2] d1 = r - 1.0 d2 = z - 1.0 d3 = -z - 1.0 d4 = np.sqrt(d1**2 + d2**2) d5 = np.sqrt(d1**2 + d3**2) d = dm.dintersect(dm.dintersect(d1, d2), d3) ix = (d1 > 0) * (d2 > 0) d[ix] = d4[ix] ix = (d1 > 0) * (d3 > 0) d[ix] = d5[ix] return dm.ddiff(d, dm.dsphere(p, 0, 0, 0, 0.5))
def naca0012_airfoil(): """NACA0012 airfoil""" hlead=0.01; htrail=0.04; hmax=2; circx=2; circr=4 a=.12/.2*np.array([0.2969,-0.1260,-0.3516,0.2843,-0.1036]) a0=a[0]; a1=np.hstack((a[5:0:-1], 0.0)) fd = lambda p: dm.ddiff( dm.dcircle(p,circx,0,circr), (abs(p[:,1])-np.polyval(a1, p[:,0]))**2-a0**2*p[:,0]) fh = lambda p: np.minimum(np.minimum( hlead+0.3*dm.dcircle(p,0,0,0), htrail+0.3*dm.dcircle(p,1,0,0)),hmax) fixx = 1.0-htrail*np.cumsum(1.3**np.arange(5)) fixy = a0*np.sqrt(fixx)+np.polyval(a1, fixx) fix = np.vstack(( np.array([(circx-circr,0),(circx+circr,0), (circx,-circr),(circx,circr), (0,0),(1,0)]), np.vstack((fixx, fixy)).T, np.vstack((fixx, -fixy)).T)) box = (circx-circr,-circr, circx+circr,circr) h0 = min(hlead, htrail, hmax) return dm.distmesh2d(fd, fh, h0, box, fix)
def fd(p): return dm.ddiff(dm.ddiff(fdout(p), dm.dcircle(p,0,0,vfr)), dm.dcircle(p,2*rad,0,vfr))
def __init__(self, lx, ly, cx, cy, r): fd = lambda p: dm.ddiff(dm.drectangle(p,-lx,lx,-ly,ly), dm.dcircle(p,cx,cx,r)) fh = lambda p: 0.05+0.03*dm.dcircle(p,cx,cy,r) p, t = dm.distmeshnd(fd, fh, 0.5, (-lx,-ly,lx,ly),[(-lx,-ly),(-lx,ly),(lx,-ly),(lx,ly)]) self.nodes = p self.conn = t
def rectangle_with_circle_at_corner(): # Does not terminate! fd = lambda p: dm.ddiff(dm.drectangle(p, 0, 10, 0, 6), dm.dcircle(p, 10, 0, 4)) return dm.distmesh2d(fd, dm.huniform, 0.9, (0, 0, 10, 6))
import distmesh as dm fd = lambda p: dm.ddiff(dm.drectangle(p,-1,1,-1,1), dm.dcircle(p,0,0,0.5)) fh = lambda p: 0.05+0.3*dm.dcircle(p,0,0,0.5) p, t = dm.distmesh2d(fd, fh, 0.05, (-1,-1,1,1), [(-1,-1),(-1,1),(1,-1),(1,1)])
def findObjectThreshold(img, threshold=7): """Find object within image using simple thresholding Input: -img: input image object -threshold: threshold intensity to apply (can be chosen from looking at histograms) Output: -mask: mask containing object -contours: contours outlining mask -hierarchy: hierarchy of contours """ #Test code: #img = cv2.imread('./video/testcontours.jpg') #Just try simple thresholding instead: (quick!, seems to work fine) if len(img.shape) == 3: frame_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) else: frame_gray = img frame_gray = cv2.blur(frame_gray, (5, 5)) frame_gray = cv2.blur(frame_gray, (5, 5)) frame_gray = cv2.blur(frame_gray, (5, 5)) #Global threshold ret1, mask = cv2.threshold(frame_gray, threshold, 255, cv2.THRESH_TOZERO) mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8') #Find contours of mask if is_cv3(): im2, c, h = cv2.findContours(mask2.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) else: c, h = cv2.findContours(mask2.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) ctrs = Contours(c, h) #Remove contours that are smaller than 40 square pixels, or that are above #level one changed = True maxarea = 0 for (ct, level) in ctrs.traverse(): maxarea = max(maxarea, cv2.contourArea(ct)) while changed: changed = False #print 'length: %d' % len(ctrs.contours) for idx, (ct, level) in enumerate(ctrs.traverse()): area = cv2.contourArea(ct) if area < maxarea and level == 0: ctrs.remove(idx) changed = True break if area < 40: ctrs.remove(idx) changed = True break if level > 1: ctrs.remove(idx) changed = True break #Make the signed diff function #All of them if len(ctrs.contours) > 1: fd = lambda p: dm.ddiff(ppt(ctrs.contours[0], p, True), ddunion(ctrs.contours[1:], p, True)) else: fd = lambda p: ppt(ctrs.contours[0], p, True) #Two of them #fd = lambda p: dm.dunion(ppt(ctrs.contours[2], p, True), ppt(ctrs.contours[1], p, True)) #One of them #fd = lambda p: ppt(ctrs.contours[0], p, True) #Diff of two of them #fd = lambda p: dm.ddiff(ppt(ctrs.contours[0], p, True), ppt(ctrs.contours[1], p, True)) #Draw contours #cv2.drawContours(grid, ctrs.contours[0:2], -1, (0,255,0), 3) #cv2.imshow('image', grid) #cv2.waitKey(0) return (mask2, ctrs, fd)
def rectangle_with_circle_at_corner(): # Does not terminate! fd = lambda p: dm.ddiff(dm.drectangle(p,0,10,0,6), dm.dcircle(p,10,0,4)) return dm.distmesh2d(fd, dm.huniform, 0.9, (0, 0, 10, 6))
def rectangle_with_circle_at_centre(): # Does not terminate! fd = lambda p: dm.ddiff(dm.drectangle(p,0,20,0,12), dm.dcircle(p,10,6,4)) return dm.distmesh2d(fd, dm.huniform, 0.5, (0, 0, 20, 12), max_iter=100)
def rectangle_with_circular_hole(): """Rectangle with circular hole, refined at circle boundary""" fd = lambda p: dm.ddiff(dm.drectangle(p,-1,1,-1,1), dm.dcircle(p,0,0,0.5)) fh = lambda p: 0.05+0.3*dm.dcircle(p,0,0,0.5) return dm.distmesh2d(fd, fh, 0.05, (-1,-1,1,1), [(-1,-1),(-1,1),(1,-1),(1,1)])
def findObjectThreshold(img, threshold = 7): """Find object within image using simple thresholding Input: -img: input image object -threshold: threshold intensity to apply (can be chosen from looking at histograms) Output: -mask: mask containing object -contours: contours outlining mask -hierarchy: hierarchy of contours """ #Test code: #img = cv2.imread('./video/testcontours.jpg') #Just try simple thresholding instead: (quick!, seems to work fine) if len(img.shape) == 3: frame_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) else: frame_gray = img #Global threshold ret1, mask = cv2.threshold(frame_gray, threshold, 255, cv2.THRESH_TOZERO) mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8') #Find contours of mask if is_cv3(): im2, c, h = cv2.findContours(mask2.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) else: c, h = cv2.findContours(mask2.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) ctrs = Contours(c, h) #Remove contours that are smaller than 40 square pixels, or that are above #level one changed = True maxarea = 0 for (ct, level) in ctrs.traverse(): maxarea = max(maxarea, cv2.contourArea(ct)) while changed: changed = False for idx, (ct, level) in enumerate(ctrs.traverse()): area = cv2.contourArea(ct) if area < maxarea and level == 0: ctrs.remove(idx) changed = True break if area < 40: ctrs.remove(idx) changed = True break if level > 1: ctrs.remove(idx) changed = True break #Make the signed diff function #All of them if len(ctrs.contours) > 1: fd = lambda p: dm.ddiff(ppt(ctrs.contours[0], p, True), ddunion(ctrs.contours[1:], p, True)) else: fd = lambda p: ppt(ctrs.contours[0], p, True) #Two of them #fd = lambda p: dm.dunion(ppt(ctrs.contours[2], p, True), ppt(ctrs.contours[1], p, True)) #One of them #fd = lambda p: ppt(ctrs.contours[0], p, True) #Diff of two of them #fd = lambda p: dm.ddiff(ppt(ctrs.contours[0], p, True), ppt(ctrs.contours[1], p, True)) #Draw contours #cv2.drawContours(grid, ctrs.contours[0:2], -1, (0,255,0), 3) #cv2.imshow('image', grid) #cv2.waitKey(0) return (mask2, ctrs, fd)
def rectangle_with_circle_at_centre(): # Does not terminate! fd = lambda p: dm.ddiff(dm.drectangle(p, 0, 20, 0, 12), dm.dcircle(p, 10, 6, 4)) return dm.distmesh2d(fd, dm.huniform, 0.5, (0, 0, 20, 12), max_iter=100)