예제 #1
0
 def test_02_01_fit_polynomial(self):
     '''Test the smooth module with polynomial fitting'''
     np.random.seed(0)
     image = np.random.uniform(size=(100,100)).astype(np.float32)
     mask = np.ones(image.shape,bool)
     mask[40:60,45:65] = False
     expected = fit_polynomial(image, mask)
     workspace, module = self.make_workspace(image, mask)
     module.smoothing_method.value = S.FIT_POLYNOMIAL
     module.run(workspace)
     result = workspace.image_set.get_image(OUTPUT_IMAGE_NAME)
     self.assertFalse(result is None)
     np.testing.assert_almost_equal(result.pixel_data, expected)
예제 #2
0
 def test_02_01_fit_polynomial(self):
     '''Test the smooth module with polynomial fitting'''
     np.random.seed(0)
     image = np.random.uniform(size=(100, 100)).astype(np.float32)
     mask = np.ones(image.shape, bool)
     mask[40:60, 45:65] = False
     expected = fit_polynomial(image, mask)
     workspace, module = self.make_workspace(image, mask)
     module.smoothing_method.value = S.FIT_POLYNOMIAL
     module.run(workspace)
     result = workspace.image_set.get_image(OUTPUT_IMAGE_NAME)
     self.assertFalse(result is None)
     np.testing.assert_almost_equal(result.pixel_data, expected)
예제 #3
0
    def run(self, workspace):
        image = workspace.image_set.get_image(self.image_name.value,
                                              must_be_grayscale=True)
        pixel_data = image.pixel_data
        if self.wants_automatic_object_size.value:
            object_size = min(30, max(1, np.mean(pixel_data.shape) / 40))
        else:
            object_size = float(self.object_size.value)
        sigma = object_size / 2.35
        if self.smoothing_method.value == GAUSSIAN_FILTER:

            def fn(image):
                return scind.gaussian_filter(image,
                                             sigma,
                                             mode='constant',
                                             cval=0)

            output_pixels = smooth_with_function_and_mask(
                pixel_data, fn, image.mask)
        elif self.smoothing_method.value == MEDIAN_FILTER:
            output_pixels = median_filter(pixel_data, image.mask,
                                          object_size / 2 + 1)
        elif self.smoothing_method.value == SMOOTH_KEEPING_EDGES:
            sigma_range = float(self.sigma_range.value)
            output_pixels = bilateral_filter(pixel_data, image.mask, sigma,
                                             sigma_range)
        elif self.smoothing_method.value == FIT_POLYNOMIAL:
            output_pixels = fit_polynomial(pixel_data, image.mask,
                                           self.clip.value)
        elif self.smoothing_method.value == CIRCULAR_AVERAGE_FILTER:
            output_pixels = circular_average_filter(pixel_data,
                                                    object_size / 2 + 1,
                                                    image.mask)
        elif self.smoothing_method.value == SM_TO_AVERAGE:
            if image.has_mask:
                mean = np.mean(pixel_data[image.mask])
            else:
                mean = np.mean(pixel_data)
            output_pixels = np.ones(pixel_data.shape, pixel_data.dtype) * mean
        else:
            raise ValueError("Unsupported smoothing method: %s" %
                             self.smoothing_method.value)
        output_image = cpi.Image(output_pixels, parent_image=image)
        workspace.image_set.add(self.filtered_image_name.value, output_image)
        workspace.display_data.pixel_data = pixel_data
        workspace.display_data.output_pixels = output_pixels
예제 #4
0
 def run(self, workspace):
     image = workspace.image_set.get_image(self.image_name.value,
                                           must_be_grayscale=True)
     pixel_data = image.pixel_data
     if self.wants_automatic_object_size.value:
         object_size = min(30,max(1,np.mean(pixel_data.shape)/40))
     else:
         object_size = float(self.object_size.value)
     sigma = object_size / 2.35
     if self.smoothing_method.value == GAUSSIAN_FILTER:
         def fn(image):
             return scind.gaussian_filter(image, sigma, 
                                          mode='constant', cval=0)
         output_pixels = smooth_with_function_and_mask(pixel_data, fn,
                                                       image.mask)
     elif self.smoothing_method.value == MEDIAN_FILTER:
         output_pixels = median_filter(pixel_data, image.mask, 
                                       object_size/2+1)
     elif self.smoothing_method.value == SMOOTH_KEEPING_EDGES:
         sigma_range = float(self.sigma_range.value)
         output_pixels = bilateral_filter(pixel_data, image.mask,
                                          sigma, sigma_range)
     elif self.smoothing_method.value == FIT_POLYNOMIAL:
         output_pixels = fit_polynomial(pixel_data, image.mask,
                                        self.clip.value)
     elif self.smoothing_method.value == CIRCULAR_AVERAGE_FILTER:
         output_pixels = circular_average_filter(pixel_data, object_size/2+1, image.mask)
     elif self.smoothing_method.value == SM_TO_AVERAGE:
         if image.has_mask:
             mean = np.mean(pixel_data[image.mask])
         else:
             mean = np.mean(pixel_data)
         output_pixels = np.ones(pixel_data.shape, pixel_data.dtype) * mean
     else:
         raise ValueError("Unsupported smoothing method: %s" %
                          self.smoothing_method.value)
     output_image = cpi.Image(output_pixels, parent_image = image)
     workspace.image_set.add(self.filtered_image_name.value,
                             output_image)
     workspace.display_data.pixel_data = pixel_data
     workspace.display_data.output_pixels = output_pixels
예제 #5
0
 def test_02_01_fit_polynomial(self):
     '''Test the smooth module with polynomial fitting'''
     np.random.seed(0)
     #
     # Make an image that has a single sinusoidal cycle with different
     # phase in i and j. Make it a little out-of-bounds to start to test
     # clipping
     #
     i, j = np.mgrid[0:100, 0:100].astype(float) * np.pi / 50
     image = (np.sin(i) + np.cos(j)) / 1.8 + .9
     image += np.random.uniform(size=(100, 100)) * .1
     mask = np.ones(image.shape, bool)
     mask[40:60, 45:65] = False
     for clip in (False, True):
         expected = fit_polynomial(image, mask, clip)
         self.assertEqual(np.all((expected >= 0) & (expected <= 1)), clip)
         workspace, module = self.make_workspace(image, mask)
         module.smoothing_method.value = S.FIT_POLYNOMIAL
         module.clip.value = clip
         module.run(workspace)
         result = workspace.image_set.get_image(OUTPUT_IMAGE_NAME)
         self.assertFalse(result is None)
         np.testing.assert_almost_equal(result.pixel_data, expected)
예제 #6
0
 def test_02_01_fit_polynomial(self):
     '''Test the smooth module with polynomial fitting'''
     np.random.seed(0)
     #
     # Make an image that has a single sinusoidal cycle with different
     # phase in i and j. Make it a little out-of-bounds to start to test
     # clipping
     #
     i, j = np.mgrid[0:100, 0:100].astype(float) * np.pi / 50
     image = (np.sin(i) + np.cos(j)) / 1.8 + .9
     image += np.random.uniform(size=(100, 100)) * .1
     mask = np.ones(image.shape,bool)
     mask[40:60,45:65] = False
     for clip in (False, True):
         expected = fit_polynomial(image, mask, clip)
         self.assertEqual(np.all((expected >= 0) & (expected <= 1)), clip)
         workspace, module = self.make_workspace(image, mask)
         module.smoothing_method.value = S.FIT_POLYNOMIAL
         module.clip.value = clip
         module.run(workspace)
         result = workspace.image_set.get_image(OUTPUT_IMAGE_NAME)
         self.assertFalse(result is None)
         np.testing.assert_almost_equal(result.pixel_data, expected)