コード例 #1
0
ファイル: test_filter.py プロジェクト: cagridz/centrosome
 def test_00_01_mask(self):
     '''Prewitt on a masked array should be zero'''
     np.random.seed(0)
     result = F.prewitt(np.random.uniform(size=(10, 10)),
                        np.zeros((10, 10), bool))
     eps = .000001
     self.assertTrue(np.all(np.abs(result) < eps))
コード例 #2
0
    def run(self, workspace):
        image = workspace.image_set.get_image(self.image_name.value,
                                              must_be_grayscale=True)
        orig_pixels = image.pixel_data
        if image.has_mask:
            mask = image.mask
        else:
            mask = np.ones(orig_pixels.shape, bool)
        if self.method == M_SOBEL:
            if self.direction == E_ALL:
                output_pixels = sobel(orig_pixels, mask)
            elif self.direction == E_HORIZONTAL:
                output_pixels = hsobel(orig_pixels, mask)
            elif self.direction == E_VERTICAL:
                output_pixels = vsobel(orig_pixels, mask)
            else:
                raise NotImplementedError(
                    "Unimplemented direction for Sobel: %s",
                    self.direction.value)
        elif self.method == M_LOG:
            sigma = self.get_sigma()
            size = int(sigma * 4) + 1
            output_pixels = laplacian_of_gaussian(orig_pixels, mask, size,
                                                  sigma)
        elif self.method == M_PREWITT:
            if self.direction == E_ALL:
                output_pixels = prewitt(orig_pixels)
            elif self.direction == E_HORIZONTAL:
                output_pixels = hprewitt(orig_pixels, mask)
            elif self.direction == E_VERTICAL:
                output_pixels = vprewitt(orig_pixels, mask)
            else:
                raise NotImplementedError(
                    "Unimplemented direction for Prewitt: %s",
                    self.direction.value)
        elif self.method == M_CANNY:
            high_threshold = self.manual_threshold.value
            low_threshold = self.low_threshold.value
            if (self.wants_automatic_low_threshold.value
                    or self.wants_automatic_threshold.value):
                sobel_image = sobel(orig_pixels, mask)
                low, high = otsu3(sobel_image[mask])
                if self.wants_automatic_low_threshold.value:
                    low_threshold = low * self.threshold_adjustment_factor.value
                if self.wants_automatic_threshold.value:
                    high_threshold = high * self.threshold_adjustment_factor.value
            output_pixels = canny(orig_pixels, mask, self.get_sigma(),
                                  low_threshold, high_threshold)
        elif self.method == M_ROBERTS:
            output_pixels = roberts(orig_pixels, mask)
        else:
            raise NotImplementedError(
                "Unimplemented edge detection method: %s" % self.method.value)

        output_image = cpi.Image(output_pixels, parent_image=image)
        workspace.image_set.add(self.output_image_name.value, output_image)

        if self.show_window:
            workspace.display_data.orig_pixels = orig_pixels
            workspace.display_data.output_pixels = output_pixels
コード例 #3
0
    def run(self, workspace):
        image = workspace.image_set.get_image(self.image_name.value,
                                              must_be_grayscale=True)
        orig_pixels = image.pixel_data
        if image.has_mask:
            mask = image.mask
        else:
            mask = np.ones(orig_pixels.shape, bool)
        if self.method == M_SOBEL:
            if self.direction == E_ALL:
                output_pixels = sobel(orig_pixels, mask)
            elif self.direction == E_HORIZONTAL:
                output_pixels = hsobel(orig_pixels, mask)
            elif self.direction == E_VERTICAL:
                output_pixels = vsobel(orig_pixels, mask)
            else:
                raise NotImplementedError("Unimplemented direction for Sobel: %s", self.direction.value)
        elif self.method == M_LOG:
            sigma = self.get_sigma()
            size = int(sigma * 4) + 1
            output_pixels = laplacian_of_gaussian(orig_pixels, mask, size, sigma)
        elif self.method == M_PREWITT:
            if self.direction == E_ALL:
                output_pixels = prewitt(orig_pixels)
            elif self.direction == E_HORIZONTAL:
                output_pixels = hprewitt(orig_pixels, mask)
            elif self.direction == E_VERTICAL:
                output_pixels = vprewitt(orig_pixels, mask)
            else:
                raise NotImplementedError("Unimplemented direction for Prewitt: %s", self.direction.value)
        elif self.method == M_CANNY:
            high_threshold = self.manual_threshold.value
            low_threshold = self.low_threshold.value
            if (self.wants_automatic_low_threshold.value or
                    self.wants_automatic_threshold.value):
                sobel_image = sobel(orig_pixels, mask)
                low, high = otsu3(sobel_image[mask])
                if self.wants_automatic_low_threshold.value:
                    low_threshold = low * self.threshold_adjustment_factor.value
                if self.wants_automatic_threshold.value:
                    high_threshold = high * self.threshold_adjustment_factor.value
            output_pixels = canny(orig_pixels, mask, self.get_sigma(),
                                  low_threshold,
                                  high_threshold)
        elif self.method == M_ROBERTS:
            output_pixels = roberts(orig_pixels, mask)
        elif self.method == M_KIRSCH:
            output_pixels = kirsch(orig_pixels)
        else:
            raise NotImplementedError("Unimplemented edge detection method: %s" %
                                      self.method.value)

        output_image = cpi.Image(output_pixels, parent_image=image)
        workspace.image_set.add(self.output_image_name.value, output_image)

        if self.show_window:
            workspace.display_data.orig_pixels = orig_pixels
            workspace.display_data.output_pixels = output_pixels
コード例 #4
0
ファイル: test_filter.py プロジェクト: cagridz/centrosome
 def test_01_02_vertical(self):
     '''Prewitt on a vertical edge should be a vertical line'''
     i, j = np.mgrid[-5:6, -5:6]
     image = (j >= 0).astype(float)
     result = F.prewitt(image)
     eps = .000001
     j[np.abs(i) == 5] = 10000
     self.assertTrue(np.all(result[j == 0] == 1))
     self.assertTrue(np.all(np.abs(result[np.abs(j) > 1]) < eps))
コード例 #5
0
 def test_03_03_prewitt_all(self):
     '''Test the prewitt transform'''
     np.random.seed(0)
     image = np.random.uniform(size=(20, 20)).astype(np.float32)
     workspace, module = self.make_workspace(image)
     module.method.value = F.M_PREWITT
     module.direction.value = F.E_ALL
     module.run(workspace)
     output = workspace.image_set.get_image(OUTPUT_IMAGE_NAME)
     self.assertTrue(np.all(output.pixel_data == FIL.prewitt(image)))
コード例 #6
0
ファイル: test_filter.py プロジェクト: cagridz/centrosome
 def test_01_01_horizontal(self):
     '''Prewitt on an edge should be a horizontal line'''
     i, j = np.mgrid[-5:6, -5:6]
     image = (i >= 0).astype(float)
     result = F.prewitt(image)
     # Fudge the eroded points
     i[np.abs(j) == 5] = 10000
     eps = .000001
     self.assertTrue(np.all(result[i == 0] == 1))
     self.assertTrue(np.all(np.abs(result[np.abs(i) > 1]) < eps))
コード例 #7
0
 def test_03_03_prewitt_all(self):
     '''Test the prewitt transform'''
     np.random.seed(0)
     image = np.random.uniform(size=(20, 20)).astype(np.float32)
     workspace, module = self.make_workspace(image)
     module.method.value = F.M_PREWITT
     module.direction.value = F.E_ALL
     module.run(workspace)
     output = workspace.image_set.get_image(OUTPUT_IMAGE_NAME)
     self.assertTrue(np.all(output.pixel_data == FIL.prewitt(image)))
コード例 #8
0
ファイル: test_filter.py プロジェクト: cagridz/centrosome
 def test_00_00_zeros(self):
     '''Prewitt on an array of all zeros'''
     result = F.prewitt(np.zeros((10, 10)), np.ones((10, 10), bool))
     self.assertTrue(np.all(result == 0))