def test_03_02_rotate_coordinates(self): """Test rotating a line to the horizontal and vertical""" img = np.zeros((20, 20)) pt0 = (2, 2) pt1 = (6, 18) draw_line(img, pt0, pt1, 1) i, j = np.mgrid[0:20, 0:20] for option in (F.C_HORIZONTALLY, F.C_VERTICALLY): def fn(module): self.assertTrue(isinstance(module, F.FlipAndRotate)) module.flip_choice.value = F.FLIP_NONE module.rotate_choice.value = F.ROTATE_COORDINATES module.horiz_or_vert.value = option module.wants_crop.value = False module.first_pixel.value = pt0 module.second_pixel.value = pt1 output_image, angle = self.run_module(img, fn=fn) pixels = output_image.pixel_data if option == F.C_HORIZONTALLY: self.assertAlmostEqual( angle, np.arctan2(pt1[0] - pt0[0], pt1[1] - pt0[1]) * 180.0 / np.pi, 3, ) # # Account for extra pixels due to twisting # line_i = 4 + (pixels.shape[0] - 20) / 2 line_j = 4 + (pixels.shape[1] - 20) / 2 self.assertTrue( np.all(pixels[line_i, line_j:line_j + 12] > 0.2)) self.assertTrue( np.all(pixels[:20, :20][np.abs(i - line_i) > 1] < 0.1)) else: self.assertAlmostEqual( angle, -np.arctan2(pt1[1] - pt0[1], pt1[0] - pt0[0]) * 180.0 / np.pi, 3, ) line_i = 4 + (pixels.shape[0] - 20) / 2 line_j = 15 + (pixels.shape[1] - 20) / 2 self.assertTrue( np.all(pixels[line_i:line_i + 12, line_j] > 0.2)) self.assertTrue( np.all(pixels[:20, :20][np.abs(j - line_j) > 1] < 0.1))
def test_03_03_random_objects_scale(self): np.random.seed(0) y,x = np.mgrid[-20:20,-20:20].astype(float)/20 min = int(20/np.sqrt(2))+1 max = 40-min for points in range(4,12): labels = np.zeros((41,41),int) coords = np.random.uniform(low=min,high=max,size=(points,2)).astype(int) angles = np.array([np.arctan2(y[yi,xi],x[yi,xi]) for xi,yi in coords]) order = np.argsort(angles) for i in range(points-1): draw_line(labels,coords[i],coords[i+1]) draw_line(labels,coords[i],coords[0]) fill_labeled_holes(labels) self.score_scales(labels,2)
def test_03_03_random_objects_scale(self): np.random.seed(0) y, x = np.mgrid[-20:20, -20:20].astype(float) / 20 min = int(20 / np.sqrt(2)) + 1 max = 40 - min for points in range(4, 12): labels = np.zeros((41, 41), int) coords = np.random.uniform(low=min, high=max, size=(points, 2)).astype(int) angles = np.array( [np.arctan2(y[yi, xi], x[yi, xi]) for xi, yi in coords]) order = np.argsort(angles) for i in range(points - 1): draw_line(labels, coords[i], coords[i + 1]) draw_line(labels, coords[i], coords[0]) fill_labeled_holes(labels) self.score_scales(labels, 2)
def test_03_02_rotate_coordinates(self): '''Test rotating a line to the horizontal and vertical''' img = np.zeros((20,20)) pt0 = (2,2) pt1 = (6,18) draw_line(img, pt0, pt1, 1) i,j = np.mgrid[0:20,0:20] for option in (F.C_HORIZONTALLY, F.C_VERTICALLY): def fn(module): self.assertTrue(isinstance(module, F.FlipAndRotate)) module.flip_choice.value = F.FLIP_NONE module.rotate_choice.value = F.ROTATE_COORDINATES module.horiz_or_vert.value = option module.wants_crop.value = False module.first_pixel.value = pt0 module.second_pixel.value = pt1 output_image, angle = self.run_module(img, fn=fn) pixels = output_image.pixel_data if option == F.C_HORIZONTALLY: self.assertAlmostEqual(angle, np.arctan2(pt1[0]-pt0[0], pt1[1]-pt0[1]) * 180.0 / np.pi, 3) # # Account for extra pixels due to twisting # line_i = 4+(pixels.shape[0]-20)/2 line_j = 4+(pixels.shape[1]-20)/2 self.assertTrue(np.all(pixels[line_i,line_j:line_j+12] > .2)) self.assertTrue(np.all(pixels[:20, :20][np.abs(i-line_i) > 1] < .1)) else: self.assertAlmostEqual(angle, -np.arctan2(pt1[1]-pt0[1], pt1[0]-pt0[0]) * 180.0 / np.pi,3) line_i = 4+(pixels.shape[0]-20)/2 line_j = 15+(pixels.shape[1]-20)/2 self.assertTrue(np.all(pixels[line_i:line_i+12,line_j] > .2)) self.assertTrue(np.all(pixels[:20, :20][np.abs(j-line_j) > 1] < .1))
def test_03_02_triangle_scale(self): labels = np.zeros((31,31),int) draw_line(labels, (15,0), (5,25)) draw_line(labels, (5,25),(25,25)) draw_line(labels, (25,25),(15,0)) labels = fill_labeled_holes(labels) labels = labels>0 self.score_scales(labels, 2)
def test_03_02_triangle_scale(self): labels = np.zeros((31, 31), int) draw_line(labels, (15, 0), (5, 25)) draw_line(labels, (5, 25), (25, 25)) draw_line(labels, (25, 25), (15, 0)) labels = fill_labeled_holes(labels) labels = labels > 0 self.score_scales(labels, 2)