Example #1
0
 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)
Example #2
0
 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 on_lasso(vertices):
     lasso = current_lasso.pop()
     figure.canvas.widgetlock.release(lasso)
     mask = np.zeros(pixel_data.shape[:2], int)
     new_label = np.max(labels) + 1
     for i in range(len(vertices)):
         v0 = (int(vertices[i][1]), int(vertices[i][0]))
         i_next = (i+1) % len(vertices)
         v1 = (int(vertices[i_next][1]), int(vertices[i_next][0]))
         draw_line(mask, v0, v1, new_label)
     mask = fill_labeled_holes(mask)
     labels[mask != 0] = new_label
     draw()
     if labels.max() > 0:
         erase_all_button.Enable()
         erase_last_button.Enable()
    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))
Example #5
0
 def on_lasso(vertices):
     lasso = current_lasso.pop()
     figure.canvas.widgetlock.release(lasso)
     mask = np.zeros(pixel_data.shape[:2], int)
     new_label = np.max(labels) + 1
     vertices = [x for x in vertices 
                 if x[0] is not None and x[1] is not None]
     for i in range(len(vertices)):
         v0 = (int(vertices[i][1]), int(vertices[i][0]))
         i_next = (i+1) % len(vertices)
         v1 = (int(vertices[i_next][1]), int(vertices[i_next][0]))
         draw_line(mask, v0, v1, new_label)
     mask = fill_labeled_holes(mask)
     labels[mask != 0] = new_label
     draw()
     if labels.max() > 0:
         erase_all_button.Enable()
         erase_last_button.Enable()
Example #6
0
    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[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[np.abs(j-line_j) > 1] < .1))
Example #7
0
 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)
Example #8
0
 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)