コード例 #1
0
 def test_01_smooth_zero(self):
     """Make sure smooth_with_function_and_mask doesn't crash on all-zeros"""
     
     result = cpms.smooth_with_function_and_mask(np.zeros((10,10)),
                                                 self.function, 
                                                 np.zeros((10,10),bool))
     self.assertTrue(np.all(np.abs(result) < .00001))
コード例 #2
0
ファイル: test_smooth.py プロジェクト: philloidin/centrosome
    def test_01_smooth_zero(self):
        """Make sure smooth_with_function_and_mask doesn't crash on all-zeros"""

        result = cpms.smooth_with_function_and_mask(np.zeros((10, 10)),
                                                    self.function,
                                                    np.zeros((10, 10), bool))
        self.assertTrue(np.all(np.abs(result) < 0.00001))
コード例 #3
0
ファイル: smooth.py プロジェクト: shntnu/CellProfiler
    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
ファイル: test_smooth.py プロジェクト: philloidin/centrosome
 def test_03_smooth_unmasked_square(self):
     np.random.seed(0)
     image = np.random.uniform(size=(10, 10))
     mask = np.ones((10, 10), bool)
     image[2:7, 3:8] = 1
     expected = self.function(image)
     result = cpms.smooth_with_function_and_mask(image, self.function, mask)
     self.assertTrue(np.all(np.abs(result - expected) < 0.00001))
コード例 #5
0
 def test_03_smooth_unmasked_square(self):
     np.random.seed(0)
     image = np.random.uniform(size=(10,10))
     mask = np.ones((10,10),bool)
     image[2:7,3:8] = 1
     expected = self.function(image)
     result = cpms.smooth_with_function_and_mask(image, self.function, mask)
     self.assertTrue(np.all(np.abs(result - expected) < .00001))
コード例 #6
0
ファイル: test_smooth.py プロジェクト: philloidin/centrosome
    def test_02_smooth_masked_square(self):
        """smooth_with_function_and_mask on a masked square does nothing"""

        np.random.seed(0)
        image = np.random.uniform(size=(10, 10))
        mask = np.zeros((10, 10), bool)
        mask[2:7, 3:8] = True
        image[mask] = 1
        result = cpms.smooth_with_function_and_mask(image, self.function, mask)
        self.assertTrue(np.all(np.abs(result[mask] - 1) < 0.00001))
コード例 #7
0
 def test_02_smooth_masked_square(self):
     """smooth_with_function_and_mask on a masked square does nothing"""
     
     np.random.seed(0)
     image = np.random.uniform(size=(10,10))
     mask  = np.zeros((10,10),bool)
     mask[2:7,3:8] = True
     image[mask] = 1
     result = cpms.smooth_with_function_and_mask(image, self.function, mask)
     self.assertTrue(np.all(np.abs(result[mask] - 1) < .00001))
コード例 #8
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 = np.float(self.sigma_range.value)

            output_pixels = skimage.restoration.denoise_bilateral(
                image=pixel_data.astype(np.float),
                multichannel=image.multichannel,
                sigma_color=sigma_range,
                sigma_spatial=sigma,
            )
        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 = cellprofiler_core.image.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
コード例 #9
0
    def run_per_layer(self, image, channel):
        if channel >= 0:
            pixel_data = image.pixel_data[:,:,channel].squeeze()
        else:
            pixel_data = image.pixel_data
        mask = image.mask
        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,
                                                          mask)
        elif self.smoothing_method.value == MEDIAN_FILTER:
            output_pixels = median_filter(pixel_data, 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, mask,
                                             sigma, sigma_range)
        elif self.smoothing_method.value == FIT_POLYNOMIAL:
            output_pixels = fit_polynomial(pixel_data, mask,
                                           self.clip.value)
        elif self.smoothing_method.value == CIRCULAR_AVERAGE_FILTER:
            output_pixels = circular_average_filter(pixel_data,
                                                    object_size / 2 + 1, mask)
        elif self.smoothing_method.value == SM_TO_AVERAGE:
            if image.has_mask:
                mean = np.mean(pixel_data[mask])
            else:
                mean = np.mean(pixel_data)
                output_pixels = np.ones(pixel_data.shape, pixel_data.dtype) * mean

        elif self.smoothing_method.value == REMOVE_OUTLIER:
            # TODO: implement how this deals with masks.
            nbhood = self.outlierneighbourhood.value
            output_pixels = self.remove_outlier_pixels(pixel_data,
                                                         threshold=self.treshold.value,
                                                         radius=nbhood,
                                                         mode='max')
        else:
            raise ValueError("Unsupported smoothing method: %s" %
                             self.smoothing_method.value)

        return output_pixels
コード例 #10
0
def test_03_02_gaussian_auto_large():
    """Test the smooth module with Gaussian smoothing in large automatic mode"""
    sigma = 30.0 / 2.35
    image = np.random.uniform(size=(3200, 100)).astype(np.float32)
    mask = np.ones(image.shape, bool)
    mask[40:60, 45:65] = False
    fn = lambda x: gaussian_filter(x, sigma, mode="constant", cval=0.0)
    expected = smooth_with_function_and_mask(image, fn, mask)
    workspace, module = make_workspace(image, mask)
    module.smoothing_method.value = S.GAUSSIAN_FILTER
    module.run(workspace)
    result = workspace.image_set.get_image(OUTPUT_IMAGE_NAME)
    assert result is not None
    np.testing.assert_almost_equal(result.pixel_data, expected)
コード例 #11
0
 def test_03_02_gaussian_auto_large(self):
     '''Test the smooth module with Gaussian smoothing in large automatic mode'''
     sigma = 30.0 / 2.35
     image = np.random.uniform(size=(3200,100)).astype(np.float32)
     mask = np.ones(image.shape,bool)
     mask[40:60,45:65] = False
     fn = lambda x: gaussian_filter(x, sigma, mode='constant', cval = 0.0)
     expected = smooth_with_function_and_mask(image, fn, mask)
     workspace, module = self.make_workspace(image, mask)
     module.smoothing_method.value = S.GAUSSIAN_FILTER
     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)
コード例 #12
0
 def test_03_01_gaussian_auto_small(self):
     '''Test the smooth module with Gaussian smoothing in automatic mode'''
     sigma = 100.0 / 40.0 / 2.35
     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
     fn = lambda x: gaussian_filter(x, sigma, mode='constant', cval=0.0)
     expected = smooth_with_function_and_mask(image, fn, mask)
     workspace, module = self.make_workspace(image, mask)
     module.smoothing_method.value = S.GAUSSIAN_FILTER
     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)
コード例 #13
0
    def run_grayscale(self, pixel_data, image):
        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 = skimage.restoration.denoise_bilateral(
                image=pixel_data,
                multichannel=image.multichannel,
                sigma_color=sigma_range,
                sigma_spatial=sigma)
        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)
        return output_pixels