def draw_normal_map(path, size): ''' Draw a spherical normal map for visualizations ''' # White backdrop im = np.full((size, size, 3), 255, np.uint8) center = size//2 light = np.r_[0,0,1.0] ng = np.r_[0,0,1.0] for i in range(size): di = i - center for j in range(size): dj = j - center r = sqrt(di*di + dj*dj) if r > center: continue r_norm = r/center phi = np.arccos(r_norm) theta = -np.arctan2(dj, di) nx = cos(theta)*cos(phi) ny = sin(theta)*cos(phi) nz = sin(phi) mag = sqrt(nx*nx + ny*ny + nz*nz) normal = np.r_[nx/mag, -ny/mag, nz/mag] color = normal_shade(light, ng, normal) im[i,j] = color IO.imshow(im) pynutmeg.wait_for_nutmeg() IO.imwrite(path, im) return im
def test_contours_rgb(): import Edge_Tracker seq = 'data/synth/plant_out' out_d = os.path.join(seq, 'outlines_out') IO.imshow(zeros((3, 3, 3), np.uint8)) try: os.makedirs(out_d) except FileExistsError: pass h, w = 360, 640 cam = Geom.cam_params(f=29.9, sw=35.0, w=w, h=h) fx, fy, cx, cy = cam def to_homogenious(pts): N = pts.shape[0] out = empty((N, 3)) out[:, 2] = 1 out[:, 0] = (pts[:, 0] - cx) * (1 / fx) out[:, 1] = (pts[:, 1] - cy) * (1 / fy) return out def from_homogenious(pts): x = (fx * pts[:, 0] + cx).astype(int) y = (fy * pts[:, 1] + cy).astype(int) return x, y i = 0 contour_set = [] normal_set = [] P_last = None pose = zeros(6) try: for im, D in load_datas(seq, imext='png'): print("Frame {}".format(i)) P, normals, edge_map = find_contours_rgb(im, D) P_hom = to_homogenious(P) H, W = im.shape[:2] if P_last is not None: P_tr, pose, tree = Edge_Tracker.align(P_last, P_hom, pose=pose) P_tr = from_homogenious(P_tr) np.clip(P_tr[0], 0, W - 1, out=P_tr[0]) np.clip(P_tr[1], 0, H - 1, out=P_tr[1]) align = np.zeros_like(im) last_u, last_v = from_homogenious(P_last) align[P[:, 1], P[:, 0], 2] = 255 align[last_v, last_u, 1] = 255 align[P_tr[1], P_tr[0], 0] = 255 IO.imshow(align, "align") # outlines, contours, normals = find_contours_rgb(im, D, snakes=False) # imio.imsave(os.path.join(out_d, "{:04d}.png".format(i)), outlines.astype(np.uint16)*2**3) # contour_set.append(contours) # normal_set.append(normals) # time.sleep(1) P_last = P_hom i += 1 except Exception as e: print(e) raise np.savez(os.path.join(seq, 'contours.npz'), contours=contour_set, normals=normal_set)