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
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_VARIANCE: sigma = self.get_sigma() size = int(sigma)+1 output_pixels = variance_transform(orig_pixels, size, mask) 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
def test_06_01_canny(self): '''Test the canny method''' i,j = np.mgrid[-20:20,-20:20] image = np.logical_and(i > j, i**2+j**2 < 300).astype(np.float32) np.random.seed(0) image = image *.5 + np.random.uniform(size=image.shape)*.3 image = np.ascontiguousarray(image, np.float32) workspace, module = self.make_workspace(image) module.method.value = F.M_CANNY module.wants_automatic_threshold.value = True module.wants_automatic_low_threshold.value = True module.wants_automatic_sigma.value = True module.run(workspace) output = workspace.image_set.get_image(OUTPUT_IMAGE_NAME) t1,t2 = otsu3(FIL.sobel(image)) result = FIL.canny(image, np.ones(image.shape,bool), 1.0, t1, t2) self.assertTrue(np.all(output.pixel_data == result))
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) if not workspace.frame is None: figure = workspace.create_or_find_figure(title="EnhanceEdges, image cycle #%d"%( workspace.measurements.image_set_number),subplots=(2,2)) figure.subplot_imshow_grayscale(0,0, orig_pixels, "Original: %s"% self.image_name.value) if self.method == M_CANNY: # Canny is binary figure.subplot_imshow_bw(0,1, output_pixels, self.output_image_name.value, sharex = figure.subplot(0,0), sharey = figure.subplot(0,0)) else: figure.subplot_imshow_grayscale(0,1,output_pixels, self.output_image_name.value, sharex = figure.subplot(0,0), sharey = figure.subplot(0,0)) color_image = np.zeros((output_pixels.shape[0], output_pixels.shape[1],3)) color_image[:,:,0] = stretch(orig_pixels) color_image[:,:,1] = stretch(output_pixels) figure.subplot_imshow(1,0, color_image,"Composite image", sharex = figure.subplot(0,0), sharey = figure.subplot(0,0)) output_image = cpi.Image(output_pixels, parent_image = image) workspace.image_set.add(self.output_image_name.value, output_image)
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) if not workspace.frame is None: figure = workspace.create_or_find_figure( title="EnhanceEdges, image cycle #%d" % (workspace.measurements.image_set_number), subplots=(2, 2)) figure.subplot_imshow_grayscale( 0, 0, orig_pixels, "Original: %s" % self.image_name.value) if self.method == M_CANNY: # Canny is binary figure.subplot_imshow_bw(0, 1, output_pixels, self.output_image_name.value, sharex=figure.subplot(0, 0), sharey=figure.subplot(0, 0)) else: figure.subplot_imshow_grayscale(0, 1, output_pixels, self.output_image_name.value, sharex=figure.subplot(0, 0), sharey=figure.subplot(0, 0)) color_image = np.zeros( (output_pixels.shape[0], output_pixels.shape[1], 3)) color_image[:, :, 0] = stretch(orig_pixels) color_image[:, :, 1] = stretch(output_pixels) figure.subplot_imshow(1, 0, color_image, "Composite image", sharex=figure.subplot(0, 0), sharey=figure.subplot(0, 0)) output_image = cpi.Image(output_pixels, parent_image=image) workspace.image_set.add(self.output_image_name.value, output_image)