Module ij

Class GaussianBlur

  • All Implemented Interfaces:
    DialogListener, ExtendedPlugInFilter, PlugInFilter

    public class GaussianBlur
    extends java.lang.Object
    implements ExtendedPlugInFilter, DialogListener
    This plug-in filter uses convolution with a Gaussian function for smoothing. 'Radius' means the radius of decay to exp(-0.5) ~ 61%, i.e. the standard deviation sigma of the Gaussian (this is the same as in Photoshop, but different from the 'Gaussian Blur' in ImageJ versions before 1.38u, where a value 2.5 times as much had to be entered. - Like all convolution operations in ImageJ, it assumes that out-of-image pixels have a value equal to the nearest edge pixel. This gives higher weight to edge pixels than pixels inside the image, and higher weight to corner pixels than non-corner pixels at the edge. Thus, when smoothing with very high blur radius, the output will be dominated by the edge pixels and especially the corner pixels (in the extreme case, with a blur radius of e.g. 1e20, the image will be raplaced by the average of the four corner pixels). - For increased speed, except for small blur radii, the lines (rows or columns of the image) are downscaled before convolution and upscaled to their original length thereafter. Version 03-Jun-2007 M. Schmid with preview, progressBar stack-aware, snapshot via snapshot flag; restricted range for resetOutOfRoi 20-Feb-2010 S. Saalfeld inner multi-threading, modified 29-Aug-2019 M. Schmid
    • Constructor Detail

      • GaussianBlur

        public GaussianBlur()
    • Method Detail

      • setup

        public int setup​(java.lang.String arg,
                         ImagePlus imp)
        Method to return types supported
        Specified by:
        setup in interface PlugInFilter
        Parameters:
        arg - unused
        imp - The ImagePlus, used to get the spatial calibration
        Returns:
        Code describing supported formats etc. (see ij.plugin.filter.PlugInFilter & ExtendedPlugInFilter)
      • showDialog

        public int showDialog​(ImagePlus imp,
                              java.lang.String command,
                              PlugInFilterRunner pfr)
        Ask the user for the parameters
        Specified by:
        showDialog in interface ExtendedPlugInFilter
        Parameters:
        imp - The active image already passed in the setup(arg, imp) call. It will be null, however, if the NO_IMAGE_REQUIRED flag has been set.
        command - The command that has led to the invocation of the plugin-filter. Useful as a title for the dialog.
        pfr - The PlugInFilterRunner calling this plugin-filter. It can be passed to a GenericDialog by addPreviewCheckbox to enable preview by calling the run(ip) method of this plugin-filter. pfr can be also used later for calling back the PlugInFilterRunner, e.g., to obtain the slice number currently processed by run(ip).
        Returns:
        The method should return a combination (bitwise OR) of the flags specified in interfaces PlugInFilter and ExtendedPlugInFilter.
      • dialogItemChanged

        public boolean dialogItemChanged​(GenericDialog gd,
                                         java.awt.AWTEvent e)
        Listener to modifications of the input fields of the dialog
        Specified by:
        dialogItemChanged in interface DialogListener
        Parameters:
        gd - A reference to the GenericDialog.
        e - The event that has been generated by the user action in the dialog. Note that e is null if the dialogItemChanged method is called after the user has pressed the OK button or if the GenericDialog has read its parameters from a macro.
        Returns:
        Should be true if the dialog input is valid. False disables the OK button and preview (if any).
      • setNPasses

        public void setNPasses​(int nPasses)
        Set the number of passes of the blur1Direction method. If called by the PlugInFilterRunner of ImageJ, an ImagePlus is known and conversion of RGB images to float as well as the two filter directions are taken into account. Otherwise, the caller should set nPasses to the number of 1-dimensional filter operations required.
        Specified by:
        setNPasses in interface ExtendedPlugInFilter
      • run

        public void run​(ImageProcessor ip)
        This method is invoked for each slice during execution
        Specified by:
        run in interface PlugInFilter
        Parameters:
        ip - The image subject to filtering. It must have a valid snapshot if the height of the roi is less than the full image height.
      • blur

        @Deprecated
        public boolean blur​(ImageProcessor ip,
                            double radius)
        Deprecated.
        Gaussian Filtering of an ImageProcessor. This method is for compatibility with the previous code (before 1.38r) and uses a low-accuracy kernel, only slightly better than the previous ImageJ code. The 'radius' in this call is different from the one used in ImageJ 1.38r and later. Therefore, use blurGaussian(ip, sigma, sigma, accuracy), where 'sigma' is equivalent to the 'sigma (radius)' of the Menu, and accuracy should be 0.02 unless better accuracy is desired.
      • blurGaussian

        public void blurGaussian​(ImageProcessor ip,
                                 double sigma)
        Gaussian Filtering of an ImageProcessor
        Parameters:
        ip - The ImageProcessor to be filtered.
        sigma - Standard deviation of the Gaussian (pixels)
        See Also:
        ImageProcessor.blurGaussian(double)
      • blurGaussian

        public void blurGaussian​(ImageProcessor ip,
                                 double sigmaX,
                                 double sigmaY,
                                 double accuracy)
        Gaussian Filtering of an ImageProcessor
        Parameters:
        ip - The ImageProcessor to be filtered.
        sigmaX - Standard deviation of the Gaussian in x direction (pixels)
        sigmaY - Standard deviation of the Gaussian in y direction (pixels)
        accuracy - Accuracy of kernel, should not be above 0.02. Better (lower) accuracy needs slightly more computing time.
      • blurFloat

        public void blurFloat​(FloatProcessor ip,
                              double sigmaX,
                              double sigmaY,
                              double accuracy)
        Gaussian Filtering of a FloatProcessor. This method does NOT include resetOutOfRoi(ip), i.e., pixels above and below the roi rectangle will be also subject to filtering in x direction and must be restored afterwards (unless the full image height is processed).
        Parameters:
        ip - The FloatProcessor to be filtered.
        sigmaX - Standard deviation of the Gaussian in x direction (pixels)
        sigmaY - Standard deviation of the Gaussian in y direction (pixels)
        accuracy - Accuracy of kernel, should not be above 0.02. Better (lower) accuracy needs slightly more computing time.
      • blur1Direction

        public void blur1Direction​(FloatProcessor ip,
                                   double sigma,
                                   double accuracy,
                                   boolean xDirection,
                                   int extraLines)
        Blur an image in one direction (x or y) by a Gaussian, using multiple threads on multiprocessor machines
        Parameters:
        ip - The Image with the original data where also the result will be stored
        sigma - Standard deviation of the Gaussian
        accuracy - Accuracy of kernel, should not be > 0.02
        xDirection - True for bluring in x direction, false for y direction
        extraLines - Number of lines (parallel to the blurring direction) below and above the roi bounds that should be processed.
      • makeGaussianKernel

        public float[][] makeGaussianKernel​(double sigma,
                                            double accuracy,
                                            int maxRadius)
        Create a 1-dimensional normalized Gaussian kernel with standard deviation sigma and the running sum over the kernel Note: this is one side of the kernel only, not the full kernel as used by the Convolver class of ImageJ. To avoid a step due to the cutoff at a finite value, the near-edge values are replaced by a 2nd-order polynomial with its minimum=0 at the first out-of-kernel pixel. Thus, the kernel function has a smooth 1st derivative in spite of finite length.
        Parameters:
        sigma - Standard deviation, i.e. radius of decay to 1/sqrt(e), in pixels.
        accuracy - Relative accuracy; for best results below 0.01 when processing 8-bit images. For short or float images, values of 1e-3 to 1e-4 are better (but increase the kernel size and thereby the processing time). Edge smoothing will fail with very poor accuracy (above approx. 0.02)
        maxRadius - Maximum radius of the kernel: Limits kernel size in case of large sigma, should be set to image width or height. For small values of maxRadius, the kernel returned may have a larger radius, however.
        Returns:
        A 2*n array. Array[0][n] is the kernel, decaying towards zero, which would be reached at kernel.length (unless kernel size is limited by maxRadius). Array[1][n] holds the sum over all kernel values > n, including non-calculated values in case the kernel size is limited by maxRadius.
      • resetOutOfRoi

        public static void resetOutOfRoi​(ImageProcessor ip,
                                         int radius)
        Set the processed pixels above and below the roi rectangle back to their previous value (i.e., snapshot buffer). This is necessary since ImageJ only restores out-of-roi pixels inside the enclosing rectangle of the roi (If the roi is non-rectangular and the SUPPORTS_MASKING flag is set).
        Parameters:
        ip - The image to be processed
        radius - The range above and below the roi that should be processed
      • showProgress

        public void showProgress​(boolean showProgressBar)