Example #1
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_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 run(self, workspace):
        image = workspace.image_set.get_image(self.image_name.value,
                                              must_be_grayscale=True)
        #
        # Match against Matlab's strel('disk') operation.
        #
        radius = (float(self.object_size.value) - 1.0) / 2.0
        mask = image.mask if image.has_mask else None
        pixel_data = image.pixel_data
        if self.method == ENHANCE:
            if self.enhance_method == E_SPECKLES:
                result = white_tophat(pixel_data, radius, mask)
            elif self.enhance_method == E_NEURITES:
                if self.neurite_choice == N_GRADIENT:
                    #
                    # white_tophat = img - opening
                    # black_tophat = closing - img
                    # desired effect = img + white_tophat - black_tophat
                    #                = img + img - opening - closing + img
                    #                = 3*img - opening - closing
                    result = (3 * pixel_data -
                              opening(pixel_data, radius, mask) -
                              closing(pixel_data, radius, mask))
                    result[result > 1] = 1
                    result[result < 0] = 0
                else:
                    sigma = self.smoothing.value
                    smoothed = gaussian_filter(pixel_data, sigma)
                    L = hessian(smoothed,
                                return_hessian=False,
                                return_eigenvectors=False)
                    #
                    # The positive values are darker pixels with lighter
                    # neighbors. The original ImageJ code scales the result
                    # by sigma squared - I have a feeling this might be
                    # a first-order correction for e**(-2*sigma), possibly
                    # because the hessian is taken from one pixel away
                    # and the gradient is less as sigma gets larger.
                    #
                    result = -L[:, :, 0] * (L[:, :, 0] < 0) * sigma * sigma
                if image.has_mask:
                    result[~mask] = pixel_data[~mask]
            elif self.enhance_method == E_DARK_HOLES:
                min_radius = max(1, int(self.hole_size.min / 2))
                max_radius = int((self.hole_size.max + 1) / 2)
                result = enhance_dark_holes(pixel_data, min_radius, max_radius,
                                            mask)
            elif self.enhance_method == E_CIRCLES:
                result = circular_hough(pixel_data, radius + .5, mask=mask)
            elif self.enhance_method == E_TEXTURE:
                result = variance_transform(pixel_data,
                                            self.smoothing.value,
                                            mask=mask)
            elif self.enhance_method == E_DIC:
                result = line_integration(pixel_data, self.angle.value,
                                          self.decay.value,
                                          self.smoothing.value)
            else:
                raise NotImplementedError("Unimplemented enhance method: %s" %
                                          self.enhance_method.value)
        elif self.method == SUPPRESS:
            if image.has_mask:
                result = opening(image.pixel_data, radius, image.mask)
            else:
                result = opening(image.pixel_data, radius)
        else:
            raise ValueError("Unknown filtering method: %s" % self.method)
        result_image = cpi.Image(result, parent_image=image)
        workspace.image_set.add(self.filtered_image_name.value, result_image)

        if self.show_window:
            workspace.display_data.image = image.pixel_data
            workspace.display_data.result = result
 def run(self, workspace):
     image = workspace.image_set.get_image(self.image_name.value,
                                           must_be_grayscale = True)
     #
     # Match against Matlab's strel('disk') operation.
     #
     radius = (float(self.object_size.value)-1.0) / 2.0
     mask = image.mask if image.has_mask else None
     pixel_data = image.pixel_data
     if self.method == ENHANCE:
         if self.enhance_method == E_SPECKLES:
             result = white_tophat(pixel_data, radius, mask)
         elif self.enhance_method == E_NEURITES:
             if self.neurite_choice == N_GRADIENT:
                 #
                 # white_tophat = img - opening
                 # black_tophat = closing - img
                 # desired effect = img + white_tophat - black_tophat
                 #                = img + img - opening - closing + img
                 #                = 3*img - opening - closing
                 result = (3 * pixel_data - 
                           opening(pixel_data, radius, mask) -
                           closing(pixel_data, radius, mask))
                 result[result > 1] = 1
                 result[result < 0] = 0
             else:
                 sigma = self.smoothing.value
                 smoothed = gaussian_filter(pixel_data, sigma)
                 L = hessian(smoothed, return_hessian = False,
                             return_eigenvectors = False)
                 #
                 # The positive values are darker pixels with lighter
                 # neighbors. The original ImageJ code scales the result
                 # by sigma squared - I have a feeling this might be
                 # a first-order correction for e**(-2*sigma), possibly
                 # because the hessian is taken from one pixel away
                 # and the gradient is less as sigma gets larger.
                 #
                 result = -L[:, :, 0] * (L[:, :, 0] < 0) * sigma * sigma
             if image.has_mask:
                 result[~mask] = pixel_data[~mask]
         elif self.enhance_method == E_DARK_HOLES:
             min_radius = max(1,int(self.hole_size.min / 2))
             max_radius = int((self.hole_size.max+1)/2)
             result = enhance_dark_holes(pixel_data, min_radius,
                                         max_radius, mask)
         elif self.enhance_method == E_CIRCLES:
             result = circular_hough(pixel_data, radius + .5, mask=mask)
         elif self.enhance_method == E_TEXTURE:
             result = variance_transform(pixel_data,
                                         self.smoothing.value,
                                         mask = mask)
         elif self.enhance_method == E_DIC:
             result = line_integration(pixel_data, 
                                       self.angle.value,
                                       self.decay.value,
                                       self.smoothing.value)
         else:
             raise NotImplementedError("Unimplemented enhance method: %s"%
                                       self.enhance_method.value)
     elif self.method == SUPPRESS:
         if image.has_mask:
             result = opening(image.pixel_data, radius, image.mask)
         else:
             result = opening(image.pixel_data, radius)
     else:
         raise ValueError("Unknown filtering method: %s"%self.method)
     result_image = cpi.Image(result, parent_image=image)
     workspace.image_set.add(self.filtered_image_name.value, result_image)
     
     if not workspace.frame is None:
         figure = workspace.create_or_find_figure(title="EnhanceOrSuppressFeatures, image cycle #%d"%(
             workspace.measurements.image_set_number),subplots=(2,1))
         figure.subplot_imshow_grayscale(0, 0, image.pixel_data,
                                         "Original: %s" % 
                                         self.image_name.value)
         figure.subplot_imshow_grayscale(1, 0, result,
                                         "Filtered: %s" %
                                         self.filtered_image_name.value,
                                         sharex = figure.subplot(0,0),
                                         sharey = figure.subplot(0,0))
Example #4
0
 def run(self, workspace):
     image = workspace.image_set.get_image(self.image_name.value,
                                           must_be_grayscale = True)
     #
     # Match against Matlab's strel('disk') operation.
     #
     radius = (float(self.object_size.value)-1.0) / 2.0
     mask = image.mask if image.has_mask else None
     pixel_data = image.pixel_data
     if self.method == ENHANCE:
         if self.enhance_method == E_SPECKLES:
             result = white_tophat(pixel_data, radius, mask)
         elif self.enhance_method == E_NEURITES:
             #
             # white_tophat = img - opening
             # black_tophat = closing - img
             # desired effect = img + white_tophat - black_tophat
             #                = img + img - opening - closing + img
             #                = 3*img - opening - closing
             result = (3 * pixel_data - 
                       opening(pixel_data, radius, mask) -
                       closing(pixel_data, radius, mask))
             result[result > 1] = 1
             result[result < 0] = 0
             if image.has_mask:
                 result[~mask] = pixel_data[~mask]
         elif self.enhance_method == E_DARK_HOLES:
             min_radius = max(1,int(self.hole_size.min / 2))
             max_radius = int((self.hole_size.max+1)/2)
             result = enhance_dark_holes(pixel_data, min_radius,
                                         max_radius, mask)
         elif self.enhance_method == E_CIRCLES:
             result = circular_hough(pixel_data, radius + .5, mask=mask)
         elif self.enhance_method == E_TEXTURE:
             result = variance_transform(pixel_data,
                                         self.smoothing.value,
                                         mask = mask)
         elif self.enhance_method == E_DIC:
             result = line_integration(pixel_data, 
                                       self.angle.value,
                                       self.decay.value,
                                       self.smoothing.value)
         else:
             raise NotImplementedError("Unimplemented enhance method: %s"%
                                       self.enhance_method.value)
     elif self.method == SUPPRESS:
         if image.has_mask:
             result = opening(image.pixel_data, radius, image.mask)
         else:
             result = opening(image.pixel_data, radius)
     else:
         raise ValueError("Unknown filtering method: %s"%self.method)
     result_image = cpi.Image(result, parent_image=image)
     workspace.image_set.add(self.filtered_image_name.value, result_image)
     
     if not workspace.frame is None:
         figure = workspace.create_or_find_figure(title="EnhanceOrSuppressFeatures, image cycle #%d"%(
             workspace.measurements.image_set_number),subplots=(2,1))
         figure.subplot_imshow_grayscale(0, 0, image.pixel_data,
                                         "Original: %s" % 
                                         self.image_name.value)
         figure.subplot_imshow_grayscale(1, 0, result,
                                         "Filtered: %s" %
                                         self.filtered_image_name.value,
                                         sharex = figure.subplot(0,0),
                                         sharey = figure.subplot(0,0))
 def run(self, workspace):
     image = workspace.image_set.get_image(self.image_name.value,
                                           must_be_grayscale = True)
     #
     # Match against Matlab's strel('disk') operation.
     #
     radius = (float(self.object_size.value)-1.0) / 2.0
     mask = image.mask if image.has_mask else None
     pixel_data = image.pixel_data
     if self.method == ENHANCE:
         if self.enhance_method == E_SPECKLES:
             if self.speckle_accuracy == S_SLOW:
                 result = white_tophat(pixel_data, radius, mask)
             else:
                 #
                 # white_tophat = img - opening
                 #              = img - dilate(erode)
                 #              = img - median_filter(median_filter(0%) 100%)
                 result = pixel_data - median_filter(
                     median_filter(pixel_data, mask, radius, percent = 0), 
                     mask, radius, percent = 100)
                 if mask is not None:
                     result[~mask] = pixel_data[~mask]
         elif self.enhance_method == E_NEURITES:
             if self.neurite_choice == N_GRADIENT:
                 #
                 # white_tophat = img - opening
                 # black_tophat = closing - img
                 # desired effect = img + white_tophat - black_tophat
                 #                = img + img - opening - closing + img
                 #                = 3*img - opening - closing
                 result = (3 * pixel_data - 
                           opening(pixel_data, radius, mask) -
                           closing(pixel_data, radius, mask))
                 result[result > 1] = 1
                 result[result < 0] = 0
             else:
                 sigma = self.smoothing.value
                 smoothed = gaussian_filter(pixel_data, sigma)
                 L = hessian(smoothed, return_hessian = False,
                             return_eigenvectors = False)
                 #
                 # The positive values are darker pixels with lighter
                 # neighbors. The original ImageJ code scales the result
                 # by sigma squared - I have a feeling this might be
                 # a first-order correction for e**(-2*sigma), possibly
                 # because the hessian is taken from one pixel away
                 # and the gradient is less as sigma gets larger.
                 #
                 result = -L[:, :, 0] * (L[:, :, 0] < 0) * sigma * sigma
             if image.has_mask:
                 result[~mask] = pixel_data[~mask]
         elif self.enhance_method == E_DARK_HOLES:
             min_radius = max(1,int(self.hole_size.min / 2))
             max_radius = int((self.hole_size.max+1)/2)
             result = enhance_dark_holes(pixel_data, min_radius,
                                         max_radius, mask)
         elif self.enhance_method == E_CIRCLES:
             result = circular_hough(pixel_data, radius + .5, mask=mask)
         elif self.enhance_method == E_TEXTURE:
             result = variance_transform(pixel_data,
                                         self.smoothing.value,
                                         mask = mask)
         elif self.enhance_method == E_DIC:
             result = line_integration(pixel_data, 
                                       self.angle.value,
                                       self.decay.value,
                                       self.smoothing.value)
         else:
             raise NotImplementedError("Unimplemented enhance method: %s"%
                                       self.enhance_method.value)
     elif self.method == SUPPRESS:
         if image.has_mask:
             result = opening(image.pixel_data, radius, image.mask)
         else:
             result = opening(image.pixel_data, radius)
     else:
         raise ValueError("Unknown filtering method: %s"%self.method)
     result_image = cpi.Image(result, parent_image=image)
     workspace.image_set.add(self.filtered_image_name.value, result_image)
     
     if self.show_window:
         workspace.display_data.image = image.pixel_data
         workspace.display_data.result = 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_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
Example #7
0
 def run(self, workspace):
     image = workspace.image_set.get_image(self.image_name.value,
                                           must_be_grayscale = True)
     #
     # Match against Matlab's strel('disk') operation.
     #
     radius = (float(self.object_size.value)-1.0) / 2.0
     mask = image.mask if image.has_mask else None
     pixel_data = image.pixel_data
     if self.method == ENHANCE:
         if self.enhance_method == E_SPECKLES:
             result = white_tophat(pixel_data, radius, mask)
         elif self.enhance_method == E_NEURITES:
             #
             # white_tophat = img - opening
             # black_tophat = closing - img
             # desired effect = img + white_tophat - black_tophat
             #                = img + img - opening - closing + img
             #                = 3*img - opening - closing
             result = (3 * pixel_data - 
                       opening(pixel_data, radius, mask) -
                       closing(pixel_data, radius, mask))
             result[result > 1] = 1
             result[result < 0] = 0
             if image.has_mask:
                 result[~mask] = pixel_data[~mask]
         elif self.enhance_method == E_DARK_HOLES:
             min_radius = max(1,int(self.hole_size.min / 2))
             max_radius = int((self.hole_size.max+1)/2)
             result = enhance_dark_holes(pixel_data, min_radius,
                                         max_radius, mask)
         elif self.enhance_method == E_CIRCLES:
             result = circular_hough(pixel_data, radius + .5, mask=mask)
         elif self.enhance_method == E_TEXTURE:
             result = variance_transform(pixel_data,
                                         self.smoothing.value,
                                         mask = mask)
         elif self.enhance_method == E_DIC:
             result = line_integration(pixel_data, 
                                       self.angle.value,
                                       self.decay.value,
                                       self.smoothing.value)
         else:
             raise NotImplementedError("Unimplemented enhance method: %s"%
                                       self.enhance_method.value)
     elif self.method == SUPPRESS:
         if image.has_mask:
             result = opening(image.pixel_data, radius, image.mask)
         else:
             result = opening(image.pixel_data, radius)
     else:
         raise ValueError("Unknown filtering method: %s"%self.method)
     result_image = cpi.Image(result, parent_image=image)
     workspace.image_set.add(self.filtered_image_name.value, result_image)
     
     if not workspace.frame is None:
         figure = workspace.create_or_find_figure(title="EnhanceOrSuppressFeatures, image cycle #%d"%(
             workspace.measurements.image_set_number),subplots=(2,1))
         figure.subplot_imshow_grayscale(0, 0, image.pixel_data,
                                         "Original: %s" % 
                                         self.image_name.value)
         figure.subplot_imshow_grayscale(1, 0, result,
                                         "Filtered: %s" %
                                         self.filtered_image_name.value,
                                         sharex = figure.subplot(0,0),
                                         sharey = figure.subplot(0,0))