def tps_warp(width, height, ctlpts, stdev): npts = len(ctlpts['_x'][0]) tps_pts = [] for i in range(npts): for j in range(npts): _varx = int((random.random()-0.5) * width * stdev) _vary = int((random.random()-0.5) * height * stdev) #tps_pts.append((ctlpts['_x'][i][j], ctlpts['_y'][i][j], _varx, _vary)) tps_pts.append((ctlpts['_x'][i][j], ctlpts['_y'][i][j], ctlpts['_x'][i][j] + _varx, ctlpts['_y'][i][j] + _vary)) # t = TPS(tps_pts,) t_back = from_control_points(tps_pts, backwards=True) # pdb.set_trace() _wfx = np.zeros((width,height), dtype='float32') _wfy = np.zeros((width,height), dtype='float32') for w in range(width): for h in range(height): _wfx[w][h] = t_back.transform(w,h)[0] - w _wfy[w][h] = t_back.transform(w,h)[1] - h warpfield_back = {'_wfx':_wfx, '_wfy':_wfy} t_fwd = from_control_points(tps_pts, backwards=False) _wfx = np.zeros((width,height), dtype='float32') _wfy = np.zeros((width,height), dtype='float32') for w in range(width): for h in range(height): _wfx[w][h] = t_fwd.transform(w,h)[0] - w _wfy[w][h] = t_fwd.transform(w,h)[1] - h warpfield_fwd = {'_wfx' : _wfx, '_wfy' : _wfy} return warpfield_back, warpfield_fwd
def test_from_control_points_list(self): t = from_control_points([ (0, 0, 50, 50), (10, 10, 100, 100), (0, 10, 70, 100)]) self.assertEquals(t.transform(4, 5), (72.0, 75.0))
def test_from_control_points_list_backwards(self): t = from_control_points([ (0, 0, 50, 50), (10, 10, 100, 100), (0, 10, 70, 100)], backwards=True) results = t.transform(72, 75) self.assertAlmostEquals(results[0], 4.0) self.assertAlmostEquals(results[1], 5.0)
def calc_tps(shape_src_outline, shape_dst_outline): t = 0 for i in range(len(shape_src_outline)): if t == 0: t = 1 cp = from_control_points([ (shape_src_outline[i][0], shape_src_outline[i][1], shape_dst_outline[i][0], shape_dst_outline[i][1]) ]) else: cp.add(shape_src_outline[i][0], shape_src_outline[i][1], shape_dst_outline[i][0], shape_dst_outline[i][1]) return cp
def aug_mask(self, ins_mask): if np.count_nonzero(ins_mask == 1) == 0: ins_mask[0][0] = 1 [y, x] = np.where(ins_mask == 1) wmax = max(x) wmin = min(x) hmax = max(y) hmin = min(y) _, contour, _ = cv2.findContours(ins_mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) if contour != []: contour = np.squeeze(np.vstack(contour)) match_point = 5 if (contour.shape[0] > match_point * 10): control_points = [] for j in range(match_point): idx = random.randint( int(contour.shape[0] * (2 * j) / (2 * match_point)), int(contour.shape[0] * (2 * j + 1) / (2 * match_point))) sx, sy = contour[idx, :] tx = sx + random.uniform(-0.1 * (wmax - wmin + 1), 0.1 * (wmax - wmin + 1)) ty = sy + random.uniform(-0.1 * (hmax - hmin + 1), 0.1 * (hmax - hmin + 1)) control_points.append((sx, sy, tx, ty)) try: t = from_control_points(control_points, backwards=True) ins_mask2 = ins_mask.copy() [x, y] = np.meshgrid(range(ins_mask.shape[1]), range(ins_mask.shape[0])) xy = np.vstack((x.flatten(), y.flatten())) # newxy = xy.copy() # for i in range(xy.shape[1]): newxy = np.round( np.array([ t.transform(*xy[:, i]) for i in range(xy.shape[1]) ])).astype(int) newxy[:, 0] = np.clip(newxy[:, 0], 0, ins_mask.shape[1] - 1) newxy[:, 1] = np.clip(newxy[:, 1], 0, ins_mask.shape[0] - 1) ins_mask[xy[1, :], xy[0, :]] = ins_mask2[newxy[:, 1], newxy[:, 0]] except Exception: print('warp error!') patch_num = random.randint(1, 5) for i in range(patch_num): tot = np.count_nonzero(ins_mask) x = random.randint(0, ins_mask.shape[1] - 1) y = random.randint(0, ins_mask.shape[0] - 1) tot = int(tot * random.uniform(0.02, 0.05)) key = (1 if ins_mask[y, x] == 0 else 0) for j in range(tot): cv2.circle(ins_mask, (x, y), (2 if ins_mask[y, x] == 0 else 8), key, thickness=-1) x = x + random.randint(-1, 1) y = y + random.randint(-1, 1) x = np.clip(x, 0, ins_mask.shape[1] - 1) y = np.clip(y, 0, ins_mask.shape[0] - 1) dilation_size = random.randint(0, 2) * 2 + 1 element = cv2.getStructuringElement( cv2.MORPH_ELLIPSE, (dilation_size * 2 + 1, dilation_size * 2 + 1), (dilation_size, dilation_size)) ins_mask = cv2.dilate(ins_mask, element) return ins_mask
def __init__(self, initial_points = []): self.points = initial_points if initial_points: self.control_points = from_control_points(self.points) else: self.control_points = None
def update(self, new_point): self.points.append(new_point) self.control_points = from_control_points(self.points)