def test_template_select(): # simple initial checks ae = assert_equal r1 = create_region(10, 5) t1 = create_region(3, 3) r1_a, t1_a = template.template_select(r1, t1, 0, 0) ae(r1_a.shape, (3, 3)) ae(t1_a.shape, (3, 3)) ae(r1_a[0, 0], 0) ae(r1_a[2, 2], 22) ae(t1_a[0, 0], 0) ae(t1_a[2, 2], 8) # simple offset r1 = create_region(10, 5) t1 = create_region(3, 3) r1_a, t1_a = template.template_select(r1, t1, 1, 2) ae(r1_a.shape, (3, 3)) ae(t1_a.shape, (3, 3)) ae(r1_a[0, 0], 21) ae(r1_a[2, 2], 43) ae(t1_a[0, 0], 0) ae(t1_a[2, 2], 8) # lower-right corner, almost r1 = create_region(10, 5) t1 = create_region(3, 3) r1_a, t1_a = template.template_select(r1, t1, 9, 3) ae(r1_a.shape, (2, 1)) ae(t1_a.shape, (2, 1)) ae(r1_a[0, 0], 39) ae(r1_a[1, 0], 49) ae(t1_a[0, 0], 0) ae(t1_a[1, 0], 3) # negative x-y spots r1 = create_region(10, 5) t1 = create_region(3, 4) r1_a, t1_a = template.template_select(r1, t1, -2, -1) ae(r1_a.shape, (3, 1)) ae(t1_a.shape, (3, 1)) ae(r1_a[0, 0], 0) ae(r1_a[2, 0], 20) ae(t1_a[0, 0], 5) ae(t1_a[2, 0], 11) # crazy values r1 = create_region(320, 240) t1 = create_region(34, 34) r1_a, t1_a = template.template_select(r1, t1, -17., -17.)
def score_state_full(self, state, img): assert len(img.shape)== 2 x = state['x'] y = state['y'] theta = state['theta'] phi = state['phi'] if self.cached_img == None or (self.cached_img != img).any(): regions = filters.extract_region_filter(img, self.likeli_params['region-size-thold'], mark_min = self.likeli_params['mark-min'], mark_max = self.likeli_params['mark-max']) img_thold = (regions > 0).astype(np.uint8)*255 # pylab.imshow(img_thold) # pylab.show() self.cached_img = img self.cached_img_thold = img_thold img_thold = self.cached_img_thold x_pix, y_pix = self.env.gc.real_to_image(x, y) x_pix = int(x_pix) y_pix = int(y_pix) template_img = self.template_obj.render(phi, theta) template_pix = template_img*255 img_region, template_region = template.template_select(img_thold, template_pix, x_pix - template_pix.shape[1]/2, y_pix - template_pix.shape[0]/2) img_region = img_region.astype(np.float32) / 255.0 template_region = template_region.astype(np.float32)/255.0 tr_size = template_region.count() if self.similarity == "dist": MINSCORE = -1e80 if tr_size == 0: return MINSCORE delta = (template_region - img_region)*self.likeli_params['delta_scale'] deltatot = np.sum(np.abs(delta)**self.likeli_params['power']) if self.likeli_params['transform'] == 'log': if deltatot > 0: s = - np.log(deltatot / tr_size) * self.likeli_params['multiply'] else: s = 0.0 elif self.likeli_params['transform'] == 'exp': s = - np.exp(deltatot/tr_size) else: s = -deltatot / tr_size # * self.likeli_params['multiply'] # pylab.figure() # pylab.subplot(1, 2, 1) # pylab.imshow(template_region) # pylab.subplot(1, 2, 2) # pylab.imshow(img_region) # pylab.title("score=%f" % s) # pylab.show() return s
def score_state_full(self, state, img): assert len(img.shape) == 2 x = state['x'] y = state['y'] theta = state['theta'] phi = state['phi'] if self.cached_img == None or (self.cached_img != img).any(): regions = filters.extract_region_filter( img, self.likeli_params['region-size-thold'], mark_min=self.likeli_params['mark-min'], mark_max=self.likeli_params['mark-max']) img_thold = (regions > 0).astype(np.uint8) * 255 # pylab.imshow(img_thold) # pylab.show() self.cached_img = img self.cached_img_thold = img_thold img_thold = self.cached_img_thold x_pix, y_pix = self.env.gc.real_to_image(x, y) x_pix = int(x_pix) y_pix = int(y_pix) template_img = self.template_obj.render(phi, theta) template_pix = template_img * 255 img_region, template_region = template.template_select( img_thold, template_pix, x_pix - template_pix.shape[1] / 2, y_pix - template_pix.shape[0] / 2) img_region = img_region.astype(np.float32) / 255.0 template_region = template_region.astype(np.float32) / 255.0 tr_size = template_region.count() if self.similarity == "dist": MINSCORE = -1e80 if tr_size == 0: return MINSCORE delta = (template_region - img_region) * self.likeli_params['delta_scale'] deltatot = np.sum(np.abs(delta)**self.likeli_params['power']) if self.likeli_params['transform'] == 'log': if deltatot > 0: s = -np.log( deltatot / tr_size) * self.likeli_params['multiply'] else: s = 0.0 elif self.likeli_params['transform'] == 'exp': s = -np.exp(deltatot / tr_size) else: s = -deltatot / tr_size # * self.likeli_params['multiply'] # pylab.figure() # pylab.subplot(1, 2, 1) # pylab.imshow(template_region) # pylab.subplot(1, 2, 2) # pylab.imshow(img_region) # pylab.title("score=%f" % s) # pylab.show() return s