def render(self, phi, theta): """ Returns a template where max intensity is 1.0, min is 0.0, float32. The center of the returned image is the center of the diode array """ s = max(self.front_size, self.back_size) T_D = self.length + 4*s template = np.ma.zeros((2, T_D, T_D), dtype=np.float32) D, W, H = template.shape front_pos, back_pos = util.compute_pos(self.length, W/2., H/2., phi, theta) def pos_to_int(p): return np.rint(p).astype(int) front_pos = pos_to_int(front_pos) back_pos = pos_to_int(back_pos) for i, size, pos in [(0, self.front_size, front_pos), (1, self.back_size, back_pos)]: template[i] = util.render_hat_ma_fast(H, W, pos[1], pos[0], size, self.border, self.nocare_border) t = np.sum(template, axis=0) t[t > 0.1] = 1.0 return t
def render(self, phi, theta): """ Returns a template where max intensity is 1.0, min is 0.0, float32. The center of the returned image is the center of the diode array """ s = max(self.front_size, self.back_size) T_D = self.length + 4*s template = np.ma.zeros((2, T_D, T_D), dtype=np.float32) template_bool = np.ma.zeros((2, T_D, T_D), dtype=np.bool) D, W, H = template.shape front_pos, back_pos = util.compute_pos(self.length, W/2., H/2., phi, theta) def pos_to_int(p): return np.rint(p).astype(int) front_pos = pos_to_int(front_pos) back_pos = pos_to_int(back_pos) front_disk = skimage.morphology.disk(self.front_size) back_disk = skimage.morphology.disk(self.back_size) # copy them to the target positions # initial spots template_bool[0, :, :] = util.paste_array(template_bool[0], front_pos[1], front_pos[0], front_disk) template_bool[1, :, :] = util.paste_array(template_bool[1], back_pos[1], back_pos[0], back_disk) template_bool = np.sum(template_bool, axis=0) assert template_bool.shape == (W, H) template_bool[template_bool == 0] = np.ma.masked template_bool[template_bool > 0] = 1 # now the border dialations border_pix = int(np.ceil(self.border * s)) border_nocare_pix = int(np.ceil(self.nocare_border * s)) d = skimage.morphology.binary_dilation(template_bool, skimage.morphology.disk(border_nocare_pix)) border = d - template_bool e = skimage.morphology.binary_dilation(d, skimage.morphology.disk(border_pix)) template_bool[(e - d) == 1] = False template_bool[border] = np.ma.masked return template_bool.astype(np.float32)
def render_source(self, x, y, phi, theta): """ Returns an image where max intensity is 1.0, min is 0.0, float32 Faster version of original rendersource, comparing the two The render image is designed to support some minor out-of-frame rendering. """ front_pos, back_pos = util.compute_pos(self.length, x, y, phi, theta) front_pos = pos_to_int(front_pos) back_pos = pos_to_int(back_pos) # slightly larger tile_size = self.pre_img[0].shape[0] tile_side = int(int((tile_size - 1)) / 2) border = tile_size img = np.zeros( (self.IMGHEIGHT + 2 * border, self.IMGWIDTH + 2 * border), dtype=np.float32) front_pix_center_x = border + front_pos[0] front_pix_center_y = border + front_pos[1] if img[front_pix_center_y - tile_side:front_pix_center_y + tile_side + 1, front_pix_center_x - tile_side:front_pix_center_x + tile_side + 1].shape == self.pre_img[0].shape: img[front_pix_center_y - tile_side:front_pix_center_y + tile_side + 1, front_pix_center_x - tile_side:front_pix_center_x + tile_side + 1] += self.pre_img[0] back_pix_center_x = border + back_pos[0] back_pix_center_y = border + back_pos[1] if img[back_pix_center_y - tile_side:back_pix_center_y + tile_side + 1, back_pix_center_x - tile_side:back_pix_center_x + tile_side + 1].shape == self.pre_img[1].shape: img[back_pix_center_y - tile_side:back_pix_center_y + tile_side + 1, back_pix_center_x - tile_side:back_pix_center_x + tile_side + 1] += self.pre_img[1] subimg = img[border:-border, border:-border] subimg = np.minimum(subimg, 1.0) # flat[subimg.flat > 1.0] = 1.0 assert subimg.shape == (self.IMGHEIGHT, self.IMGWIDTH) return subimg
def render_source(self, x, y, phi, theta): """ Returns an image where max intensity is 1.0, min is 0.0, float32 Faster version of original rendersource, comparing the two The render image is designed to support some minor out-of-frame rendering. """ front_pos, back_pos = util.compute_pos(self.length, x, y, phi, theta) front_pos = pos_to_int(front_pos) back_pos = pos_to_int(back_pos) # slightly larger tile_size = self.pre_img[0].shape[0] tile_side = int(int((tile_size -1))/2) border = tile_size img = np.zeros((self.IMGHEIGHT + 2*border, self.IMGWIDTH + 2*border), dtype = np.float32) front_pix_center_x = border + front_pos[0] front_pix_center_y = border + front_pos[1] if img[front_pix_center_y - tile_side:front_pix_center_y+tile_side+1, front_pix_center_x - tile_side:front_pix_center_x+tile_side+1].shape == self.pre_img[0].shape: img[front_pix_center_y - tile_side:front_pix_center_y+tile_side+1, front_pix_center_x - tile_side:front_pix_center_x+tile_side+1] += self.pre_img[0] back_pix_center_x = border + back_pos[0] back_pix_center_y = border + back_pos[1] if img[back_pix_center_y - tile_side:back_pix_center_y+tile_side+1, back_pix_center_x - tile_side:back_pix_center_x+tile_side+1].shape == self.pre_img[1].shape: img[back_pix_center_y - tile_side:back_pix_center_y+tile_side+1, back_pix_center_x - tile_side:back_pix_center_x+tile_side+1] += self.pre_img[1] subimg = img[border:-border, border:-border] subimg = np.minimum(subimg, 1.0) # flat[subimg.flat > 1.0] = 1.0 assert subimg.shape == (self.IMGHEIGHT, self.IMGWIDTH) return subimg
def plot_state(ax, state, full_render_skip, length): """ take an axis and plot the state vector on it """ ax.plot(state["x"], state["y"]) front_pos, back_pos = util.compute_pos(length, state["x"], state["y"], state["phi"], state["theta"]) N = len(state) for p in range(0, N, full_render_skip): fp_x = front_pos[0][p] fp_y = front_pos[1][p] bp_x = back_pos[0][p] bp_y = back_pos[1][p] ax.plot([bp_x, fp_x], [bp_y, fp_y], c="k") ax.scatter(bp_x, bp_y, c="r") ax.scatter(fp_x, fp_y, c="b")
def plot_state(ax, state, full_render_skip, length): """ take an axis and plot the state vector on it """ ax.plot(state['x'], state['y']) front_pos, back_pos = util.compute_pos(length, state['x'], state['y'], state['phi'], state['theta']) N = len(state) for p in range(0, N, full_render_skip): fp_x = front_pos[0][p] fp_y = front_pos[1][p] bp_x = back_pos[0][p] bp_y = back_pos[1][p] ax.plot([bp_x, fp_x], [bp_y, fp_y], c='k') ax.scatter(bp_x, bp_y, c='r') ax.scatter(fp_x, fp_y, c='b')
def render(self, phi, theta): """ Returns a template where max intensity is 1.0, min is 0.0, float32. The center of the returned image is the center of the diode array """ s = max(self.front_size, self.back_size) template = np.zeros((2, self.length + 4*s, self.length + 4*s)) D, W, H = template.shape front_pos, back_pos = util.compute_pos(self.length, W/2., H/2., phi, theta) def pos_to_int(p): return np.rint(p).astype(int) front_pos = pos_to_int(front_pos) back_pos = pos_to_int(back_pos) template[0, front_pos[1], front_pos[0]] = 1.0 template[1, back_pos[1], back_pos[0]] = 1.0 template[0] = scipy.ndimage.filters.gaussian_filter(template[0], self.front_size) template[0] = template[0] / np.max(template[0]) template[0][template[0]<0.2] = 0.0 template[1] = scipy.ndimage.filters.gaussian_filter(template[1], self.back_size) template[1] = template[1] / np.max(template[1]) template[1][template[1]<0.2] = 0.0 t = np.sum(template, axis=0) t = t / np.max(t) tm = np.ma.masked_less(t, 0.0001) return tm
def score_state_full(self, state, img): assert len(img.shape)== 2 x = state['x'] y = state['y'] theta = state['theta'] phi = state['phi'] img_thold = img.copy() img_thold[img_thold < self.params['pix-threshold']] = 0 if self.img_cache == None or (self.img_cache != img_thold).any(): self.img_cache = img_thold.copy() self.coordinates = skimage.feature.peak_local_max(img_thold, min_distance=10, threshold_abs=220) # coordinates = self.coordinates # pylab.imshow(img_thold, interpolation='nearest', cmap=pylab.cm.gray) # pylab.plot([p[1] for p in coordinates], [p[0] for p in coordinates], 'r.') # pylab.show() # get the points coordinates = self.coordinates x_pix, y_pix = self.env.gc.real_to_image(x, y) x_pix = int(x_pix) y_pix = int(y_pix) front_pos_pix, back_pos_pix = util.compute_pos(self.template_obj.length, x_pix, y_pix, phi, theta) ppc = template.PointCloudCount(front_pos_pix[:2], self.template_obj.front_size, back_pos_pix[:2], self.template_obj.back_size) fpi, bpi, bi = ppc.get_points(np.fliplr(coordinates)) count = len(fpi) + len(bpi) - len(bi) # if count != 0: # print x_pix, y_pix, "Points:", len(fpi), len(bpi), len(bi) return np.exp(count)
def score_state_full(self, state, img): assert len(img.shape) == 2 x = state['x'] y = state['y'] theta = state['theta'] phi = state['phi'] img_thold = img.copy() img_thold[img_thold < self.params['pix-threshold']] = 0 if self.img_cache == None or (self.img_cache != img_thold).any(): self.img_cache = img_thold.copy() self.coordinates = skimage.feature.peak_local_max( img_thold, min_distance=10, threshold_abs=220) # coordinates = self.coordinates # pylab.imshow(img_thold, interpolation='nearest', cmap=pylab.cm.gray) # pylab.plot([p[1] for p in coordinates], [p[0] for p in coordinates], 'r.') # pylab.show() # get the points coordinates = self.coordinates x_pix, y_pix = self.env.gc.real_to_image(x, y) x_pix = int(x_pix) y_pix = int(y_pix) front_pos_pix, back_pos_pix = util.compute_pos( self.template_obj.length, x_pix, y_pix, phi, theta) ppc = template.PointCloudCount(front_pos_pix[:2], self.template_obj.front_size, back_pos_pix[:2], self.template_obj.back_size) fpi, bpi, bi = ppc.get_points(np.fliplr(coordinates)) count = len(fpi) + len(bpi) - len(bi) # if count != 0: # print x_pix, y_pix, "Points:", len(fpi), len(bpi), len(bi) return np.exp(count)
def score_state_full(self, state, img): assert len(img.shape)== 2 x = state['x'] y = state['y'] theta = state['theta'] phi = state['phi'] img_thold = img.copy() img_thold[img_thold < self.params['pix-threshold']] = 0 if self.img_cache == None or (self.img_cache != img_thold).any(): self.img_cache = img_thold.copy() coordinates = skimage.feature.peak_local_max(img, min_distance=30, threshold_rel=0.8) frame_regions = filters.label_regions(img_thold) filtered_regions = filters.filter_regions(frame_regions, max_width = 30, max_height=30) fc = filters.points_in_mask(filtered_regions > 0, coordinates) self.coordinates = fc # pylab.imshow(img, interpolation='nearest', cmap=pylab.cm.gray) # pylab.plot([p[1] for p in self.coordinates], # [p[0] for p in self.coordinates], 'r.') # pylab.show() # get the points coordinates = self.coordinates if len(coordinates) == 0: return 0 x_pix, y_pix = self.env.gc.real_to_image(x, y) x_pix = int(x_pix) y_pix = int(y_pix) front_pos_pix, back_pos_pix = util.compute_pos(self.template_obj.length, x_pix, y_pix, phi, theta) front_deltas = np.abs((coordinates - np.array(front_pos_pix)[:2][::-1])) front_dists = np.sqrt(np.sum(front_deltas**2, axis=1)) assert len(front_dists) == len(coordinates) back_deltas = np.abs((coordinates - np.array(back_pos_pix)[:2][::-1])) back_dists = np.sqrt(np.sum(back_deltas**2, axis=1)) dist_thold = self.params.get('dist-thold', None) if dist_thold != None: front_deltas= front_deltas[front_deltas < dist_thold] back_deltas = back_deltas[back_deltas < dist_thold] closest_n = self.params.get('closest-n', None) if closest_n != None: front_deltas = np.sort(front_deltas)[::-1] back_deltas = np.sort(back_deltas)[::-1] front_deltas= front_deltas[:closest_n] back_deltas = back_deltas[:closest_n] power = self.params.get('power', 1) delta_sum = np.sum(front_dists**power) + np.sum(back_dists**power) if self.params.get('normalize', True): delta_sum = delta_sum / len(coordinates) if len(coordinates) == 0: score = -1e10 elif self.params.get('log', False): score = -np.log(delta_sum) elif self.params.get('exp', False): score = -np.exp(delta_sum) else: score = -delta_sum return score
y = sv[s_i]['y'] phi = sv[s_i]['phi'] theta = sv[s_i]['theta'] x_pix, y_pix = env.gc.real_to_image(x, y) # render the fake image ax_generated = pylab.subplot2grid((TOP_R * 2, TOP_C * 2), (row * 2 + 1, col * 2 + 1)) rendered_img = tp.render(phi, theta) ax_generated.imshow(rendered_img * 255, interpolation='nearest', cmap=pylab.cm.gray, vmin=0, vmax=255) # now compute position of diodes front_pos, back_pos = util.compute_pos(tp.length, x_pix, y_pix, phi, theta) cir = pylab.Circle(front_pos, radius=EO_PARAMS[1], ec='g', fill=False, linewidth=2) ax.add_patch(cir) cir = pylab.Circle(back_pos, radius=EO_PARAMS[2], ec='r', fill=False, linewidth=2) ax.add_patch(cir) ax.set_title("%2.2f, %2.2f, %1.1f, %1.1f, %4.2f" % (x, y, phi, theta, score),
def score_state_full(self, state, img): assert len(img.shape) == 2 x = state['x'] y = state['y'] theta = state['theta'] phi = state['phi'] img_thold = img.copy() img_thold[img_thold < self.params['pix-threshold']] = 0 if self.img_cache == None or (self.img_cache != img_thold).any(): self.img_cache = img_thold.copy() coordinates = skimage.feature.peak_local_max(img, min_distance=30, threshold_rel=0.8) frame_regions = filters.label_regions(img_thold) filtered_regions = filters.filter_regions(frame_regions, max_width=30, max_height=30) fc = filters.points_in_mask(filtered_regions > 0, coordinates) self.coordinates = fc # pylab.imshow(img, interpolation='nearest', cmap=pylab.cm.gray) # pylab.plot([p[1] for p in self.coordinates], # [p[0] for p in self.coordinates], 'r.') # pylab.show() # get the points coordinates = self.coordinates if len(coordinates) == 0: return 0 x_pix, y_pix = self.env.gc.real_to_image(x, y) x_pix = int(x_pix) y_pix = int(y_pix) front_pos_pix, back_pos_pix = util.compute_pos( self.template_obj.length, x_pix, y_pix, phi, theta) front_deltas = np.abs( (coordinates - np.array(front_pos_pix)[:2][::-1])) front_dists = np.sqrt(np.sum(front_deltas**2, axis=1)) assert len(front_dists) == len(coordinates) back_deltas = np.abs((coordinates - np.array(back_pos_pix)[:2][::-1])) back_dists = np.sqrt(np.sum(back_deltas**2, axis=1)) dist_thold = self.params.get('dist-thold', None) if dist_thold != None: front_deltas = front_deltas[front_deltas < dist_thold] back_deltas = back_deltas[back_deltas < dist_thold] closest_n = self.params.get('closest-n', None) if closest_n != None: front_deltas = np.sort(front_deltas)[::-1] back_deltas = np.sort(back_deltas)[::-1] front_deltas = front_deltas[:closest_n] back_deltas = back_deltas[:closest_n] power = self.params.get('power', 1) delta_sum = np.sum(front_dists**power) + np.sum(back_dists**power) if self.params.get('normalize', True): delta_sum = delta_sum / len(coordinates) if len(coordinates) == 0: score = -1e10 elif self.params.get('log', False): score = -np.log(delta_sum) elif self.params.get('exp', False): score = -np.exp(delta_sum) else: score = -delta_sum return score
true_x_pix, true_y_pix = env.gc.real_to_image(true_x, true_y) est_x = vals_dict['x'][error_frame_i] est_y = vals_dict['y'][error_frame_i] est_phi = vals_dict['phi'][error_frame_i] est_theta = vals_dict['theta'][error_frame_i] est_x_pix, est_y_pix = env.gc.real_to_image(est_x, est_y) rendered_img = tr.render(est_phi, est_theta) # now compute position of diodes front_pos, back_pos = util.compute_pos(tr.length, est_x_pix, est_y_pix, est_phi, est_theta) cir = pylab.Circle(front_pos, radius=eoparams[1], ec='g', fill=False, linewidth=2) ax.add_patch(cir) cir = pylab.Circle(back_pos, radius=eoparams[2], ec='r', fill=False, linewidth=2) ax.add_patch(cir) # true ax.axhline(true_y_pix, c='b') ax.axvline(true_x_pix, c='b') # extimated
cmap=pylab.cm.gray) true_x = truth['x'][abs_frame] true_y = truth['y'][abs_frame] true_x_pix, true_y_pix = env.gc.real_to_image(true_x, true_y) est_x = vals_dict['x'][error_frame_i] est_y = vals_dict['y'][error_frame_i] est_phi = vals_dict['phi'][error_frame_i] est_theta = vals_dict['theta'][error_frame_i] est_x_pix, est_y_pix = env.gc.real_to_image(est_x, est_y) rendered_img = tr.render(est_phi, est_theta) # now compute position of diodes front_pos, back_pos = util.compute_pos(tr.length, est_x_pix, est_y_pix, est_phi, est_theta) cir = pylab.Circle(front_pos, radius=eoparams[1], ec='g', fill=False, linewidth=2) ax.add_patch(cir) cir = pylab.Circle(back_pos, radius=eoparams[2], ec='r', fill=False, linewidth=2) ax.add_patch(cir) # true
vmin=0, vmax=255) x = sv[s_i]['x'] y = sv[s_i]['y'] phi = sv[s_i]['phi'] theta = sv[s_i]['theta'] x_pix, y_pix = env.gc.real_to_image(x, y) # render the fake image ax_generated = pylab.subplot2grid((TOP_R*2, TOP_C*2), (row*2+1, col*2 + 1)) rendered_img = tp.render(phi, theta) ax_generated.imshow(rendered_img*255, interpolation='nearest', cmap=pylab.cm.gray, vmin = 0, vmax=255) # now compute position of diodes front_pos, back_pos = util.compute_pos(tp.length, x_pix, y_pix, phi, theta) cir = pylab.Circle(front_pos, radius=EO_PARAMS[1], ec='g', fill=False, linewidth=2) ax.add_patch(cir) cir = pylab.Circle(back_pos, radius=EO_PARAMS[2], ec='r', fill=False, linewidth=2) ax.add_patch(cir) ax.set_title("%2.2f, %2.2f, %1.1f, %1.1f, %4.2f" % (x, y, phi, theta, score), size="xx-small") for a in [ax, ax_thold]: a.set_xticks([]) a.set_yticks([]) a.set_xlim(x_pix - X_MARGIN, x_pix+X_MARGIN) a.set_ylim(y_pix - Y_MARGIN, y_pix+Y_MARGIN) f.subplots_adjust(bottom=0, left=.01, right=.99, top=.90, hspace=.2, wspace=.1) f.savefig(zoom_outfile, dpi=300)