Class IjUtils

java.lang.Object
imagingbook.common.ij.IjUtils

public abstract class IjUtils extends Object
This class defines static utility methods adding to ImageJs functionality.
  • Field Details

  • Method Details

    • getOpenImages

      public static ImagePlus[] getOpenImages(boolean sortByTitle)
      Returns a (possibly empty) array of ImagePlus objects that are sorted by their titles if the 'sortByTitle' flag is set.
      Parameters:
      sortByTitle - flag, result is sorted if true.
      Returns:
      an array of currently open images.
    • getImageShortTitles

      public static String[] getImageShortTitles(ImagePlus[] images)
      Returns an array of strings containing the short titles of the images supplied.
      Parameters:
      images - array of images.
      Returns:
      array of names.
    • selectOpenImage

      public static ImagePlus selectOpenImage(String title)
      Opens a dialog to let the user select one of the currently open images.
      Parameters:
      title - string to show in the dialog
      Returns:
      a ImagePlus object, use the getProcessor method to obtain the associated ImageProcessor
    • getOpenImages

      public static ImagePlus[] getOpenImages(boolean sortByTitle, ImagePlus exclude)
      Returns a (possibly empty) array of ImagePlus objects that are sorted by their titles if the sortByTitle flag is set. The image "exclude" (typically the current image) is not included in the returned array (pass null to exclude no image).
      Parameters:
      sortByTitle - set true to return images sorted by title
      exclude - reference to an image to be excluded (may be null)
      Returns:
      a (possibly empty) array of ImagePlus objects
    • createImage

      public static ImagePlus createImage(String title, float[][] M)
      Creates an ImageJ ImagePlus image for the matrix M[r][c] (2D array), where r is treated as the row (vertical) coordinate and c is treated as the column (horizontal) coordinate. Use show() to display the resulting image.
      Parameters:
      title - image title
      M - 2D array
      Returns:
      a new ImagePlus image
    • createImage

      public static ImagePlus createImage(String title, double[][] M)
      Creates an ImageJ ImagePlus image for the matrix M[r][c] (2D array), where r is treated as the row (vertical) coordinate and c is treated as the column (horizontal) coordinate. Use show() to display the resulting image.
      Parameters:
      title - the image title
      M - a 2D array holding the image data
      Returns:
      a new ImagePlus instance
    • setRgbConversionWeights

      public static void setRgbConversionWeights(ImageProcessor ip)
      Sets the weighing factors for the color components used in RGB-to-grayscale conversion for the specified image ip. Note that this method can be applied to any ImageProcessor instance but has no effect unless ip is of type ColorProcessor. Applies standard (ITU-709) weights.
      Parameters:
      ip - the affected image
    • setRgbConversionWeights

      public static void setRgbConversionWeights(ImageProcessor ip, double wr, double wg, double wb)
      Sets the weighing factors for the color components used in RGB-to-grayscale conversion for the specified image ip. Note that this method can be applied to any ImageProcessor instance but has no effect unless ip is of type ColorProcessor.
      Parameters:
      ip - the affected image
      wr - red component weight
      wg - green component weight
      wb - blue component weight
    • crop

      public static <T extends ImageProcessor> T crop(T ip, int x, int y, int width, int height)
      Extracts (crops) a rectangular region from the given image and returns it as a new image (of the same sub-type of ImageProcessor). If the specified rectangle extends outside the source image, only the overlapping region is cropped. Thus the returned image may have smaller size than the specified rectangle. An exception is thrown if the specified width or height is less than 1. null is returned if the rectangle does not overlap the image at all.
      Type Parameters:
      T - the generic image type
      Parameters:
      ip - the image to be cropped
      x - the left corner coordinate of the cropping rectangle
      y - the top corner coordinate of the cropping rectangle
      width - the width of the cropping rectangle
      height - the height of the cropping rectangle
      Returns:
      the cropped image
    • toDoubleArray

      public static double[][] toDoubleArray(FloatProcessor fp)
      Returns a copy of the pixel data as a 2D double array with dimensions [x = 0,..,width-1][y = 0,..,height-1].
      Parameters:
      fp - the image
      Returns:
      the resulting array
    • toFloatProcessor

      public static FloatProcessor toFloatProcessor(double[][] A)
      Creates a new FloatProcessor instance of size width x height from the given double[][] with dimensions [x = 0,..,width-1][y = 0,..,height-1].
      Parameters:
      A - a 2D double array
      Returns:
      a new FloatProcessor instance
    • toFloatProcessor

      public static FloatProcessor toFloatProcessor(float[][] A)
      Creates a new FloatProcessor instance of size width x height from the given float[][] with dimensions [x = 0,..,width-1][y = 0,..,height-1].
      Parameters:
      A - a 2D float array
      Returns:
      a new FloatProcessor instance
    • toFloatArray

      public static float[][] toFloatArray(FloatProcessor fp)
      Converts a FloatProcessor to a float[][].
      Parameters:
      fp - a FloatProcessor
      Returns:
      the resulting float[][]
    • toByteProcessor

      Converts the given RGB ColorProcessor to a scalar-valued ByteProcessor, using clearly specified RGB component weights. The processor's individual RGB component weights are used if they have been set (not null), otherwise ITU709 weights (see RgbUtils.ITU709RgbWeights) are applied. This is to avoid problems with standard conversion methods in ImageJ, which depend on a variety of factors (including current user settings). See also ColorProcessor.getRGBWeights(), ColorProcessor.setRGBWeights(double[]), ImageProcessor.convertToByteProcessor().
      Parameters:
      cp - a ColorProcessor
      Returns:
      the resulting ByteProcessor
      See Also:
    • toByteProcessor

      public static ByteProcessor toByteProcessor(ColorProcessor cp, double[] rgbWeights)
      Converts the given RGB ColorProcessor to a scalar-valued ByteProcessor, applying the specified set of RGB component weights. The processor's individual weights (if set) are ignored. This is to avoid problems with standard conversion methods in ImageJ, which depend on a variety of factors (including current user settings). See also ColorProcessor.getRGBWeights(), ColorProcessor.setRGBWeights(double[]), ImageProcessor.convertToByteProcessor().
      Parameters:
      cp - a ColorProcessor
      rgbWeights - a 3-vector of RGB component weights (must sum to 1)
      Returns:
      the resulting ByteProcessor
      See Also:
    • toFloatProcessor

      Converts the given RGB ColorProcessor to a scalar-valued FloatProcessor, using clearly specified RGB component weights. The processor's individual RGB component weights are used if set, otherweise default weights are used (see RgbUtils.getDefaultWeights()). This should avoid problems with standard conversion methods in ImageJ, which depend on a variety of factors (including current user settings). See also ColorProcessor.getRGBWeights(), ColorProcessor.setRGBWeights(double[]), ImageProcessor.convertToFloatProcessor().
      Parameters:
      cp - a ColorProcessor
      Returns:
      the resulting FloatProcessor
      See Also:
    • toFloatProcessor

      public static FloatProcessor toFloatProcessor(ColorProcessor cp, double[] rgbWeights)
      Converts the given RGB ColorProcessor to a scalar-valued FloatProcessor, applying the specified set of RGB component weights. If null is passed for the weights, default weights are used (see RgbUtils.getDefaultWeights()). The processor's individual weights (if set at all) are ignored. This should avoid problems with standard conversion methods in ImageJ, which depend on a variety of factors (including current user settings). See also ColorProcessor.getRGBWeights(), ColorProcessor.setRGBWeights(double[]), ImageProcessor.convertToFloatProcessor().
      Parameters:
      cp - a ColorProcessor
      rgbWeights - a 3-vector of RGB component weights (must sum to 1)
      Returns:
      the resulting FloatProcessor
      See Also:
    • toByteProcessor

      public static ByteProcessor toByteProcessor(byte[][] A)
      Creates and returns a new ByteProcessor from the specified 2D byte array, assumed to be arranged in the form A[x][y], i.e., the first coordinate is horizontal, the second vertical. Thus A.length is the width and A[0].length the height of the resulting image.
      Parameters:
      A - a 2D byte array
      Returns:
      a new ByteProcessor of size A.length x A[0].length
    • toByteArray

      public static byte[][] toByteArray(ByteProcessor bp)
      Creates and returns a new byte[][] from the specified ByteProcessor. The resulting array is arranged in the form A[x][y], i.e., the first coordinate is horizontal, the second vertical. Thus A.length is the width and A[0].length the height of the image.
      Parameters:
      bp - a ByteProcessor
      Returns:
      a 2D byte array
    • toByteProcessor

      public static ByteProcessor toByteProcessor(int[][] A)
      Creates and returns a new ByteProcessor from the specified 2D int array, assumed to be arranged in the form A[x][y], i.e., the first coordinate is horizontal, the second vertical. Thus A.length is the width and A[0].length the height of the resulting image. Pixel values are clamped to [0, 255].
      Parameters:
      A - a 2D int array
      Returns:
      a new ByteProcessor of size A.length x A[0].length
    • toIntArray

      public static int[][] toIntArray(ByteProcessor bp)
      Creates and returns a new int[][] from the specified ByteProcessor. The resulting array is arranged in the form A[x][y], i.e., the first coordinate is horizontal, the second vertical. Thus A.length is the width and A[0].length the height of the image.
      Parameters:
      bp - a ByteProcessor
      Returns:
      a 2D int array
    • openImage

      public static ImagePlus openImage(URI uri)
      Opens the image from the specified URI and returns it as a ImagePlus instance.
      Parameters:
      uri - the URI leading to the image (including extension)
      Returns:
      a new ImagePlus instance or null if unable to open
    • openImage

      public static ImagePlus openImage(String filename)
      Opens the image from the specified filename and returns it as a ImagePlus instance.
      Parameters:
      filename - the path and filename to be opened
      Returns:
      a new ImagePlus instance or null if unable to open
    • sameType

      public static boolean sameType(ImageProcessor ip1, ImageProcessor ip2)
      Checks if two images are of the same type.
      Parameters:
      ip1 - the first image
      ip2 - the second image
      Returns:
      true if both images have the same type
    • sameSize

      public static boolean sameSize(ImageProcessor ip1, ImageProcessor ip2)
      Checks if two images have the same size.
      Parameters:
      ip1 - the first image
      ip2 - the second image
      Returns:
      true if both images have the same size
    • isBinary

      public static boolean isBinary(ImageProcessor ip)
      Checks if the given image is possibly a binary image. This requires that the image contains at most two different pixel values, one of (the 'background' value) which must be zero. Also returns true if the image is filled with zeros or a single nonzero value. All pixels are checked. This should work for all image types. More efficient implementations are certainly possible.
      Parameters:
      ip - the image (ImageProcessor) to be checked
      Returns:
      true if the image is possibly binary
    • isFlat

      public static boolean isFlat(ImageProcessor ip)
      Checks if the given image is "flat", i.e., all pixels have the same value. This should work for all image types.
      Parameters:
      ip - the image (ImageProcessor) to be checked
      Returns:
      true if the image is flat
    • collectNonzeroPoints

      Collects all image coordinates with non-zero pixel values into an array of 2D points (Pnt2d).
      Parameters:
      ip - an image (of any type)
      Returns:
      an array of 2D points
    • match

      public static boolean match(ImageProcessor ip1, ImageProcessor ip2)
      Checks if two images have the same type, size and content (using DefaultMatchTolerance for float images).
      Parameters:
      ip1 - the first image
      ip2 - the second image
      Returns:
      true if both images have the same type and content
    • match

      public static boolean match(ImageProcessor ip1, ImageProcessor ip2, double tolerance)
      Checks if two images have the same type, size and values (using the specified tolerance for float images).
      Parameters:
      ip1 - the first image
      ip2 - the second image
      tolerance - the matching tolerance
      Returns:
      true if both images have the same type, size and content
    • convertToBitMap

      public static BitMap convertToBitMap(ByteProcessor bp)
      Converts the specified ByteProcessor to a BitMap of the same size, with all zero values set to 0 and non-zero values set to 1.
      Parameters:
      bp - a ByteProcessor
      Returns:
      the corresponding BitMap
      See Also:
    • convertToByteProcessor

      Converts the specified BitMap to a ByteProcessor of the same size, with all zero values set to 0 and non-zero values set to 1. The resulting image should be multiplied by 255 to achieve full contrast, e.g.:

       ByteProcessor bp1 = ... // some ByteProcessor
       BitMap bm = IjUtils.convertToBitMap(bp);
       ByteProcessor bp2 = IjUtils.convertToByteProcessor(bm);
       bp2.multiply(255);
       ...
       
      Parameters:
      bitmap - a BitMap
      Returns:
      the corresponding ByteProcessor
      See Also:
    • drawPoints

      public static void drawPoints(ImageProcessor ip, Pnt2d[] points, int value)
      Draws the given set of points onto the specified image (by setting the corresponding pixels).
      Parameters:
      ip - the image to draw to
      points - the 2D points
      value - the pixel value to use
    • run

      public static boolean run(PlugInFilter pluginfilter)
      Runs the given PlugInFilter instance with empty argument string.
      Parameters:
      pluginfilter - an instance of PlugInFilter
      Returns:
      true if no exception was thrown
    • run

      public static boolean run(PlugInFilter pluginfilter, String arg)
      Runs the given PlugInFilter instance.
      Parameters:
      pluginfilter - an instance of PlugInFilter
      arg - argument passed to PlugInFilter.setup(String, ImagePlus)
      Returns:
      true if no exception was thrown
    • run

      public static boolean run(PlugIn plugin)
      Runs the given PlugIn instance with empty argument string.
      Parameters:
      plugin - an instance of PlugIn
      Returns:
      true if no exception was thrown
    • run

      public static boolean run(PlugIn plugin, String arg)
      Runs the given PlugIn instance.
      Parameters:
      plugin - an instance of PlugIn
      arg - argument passed to PlugIn.run(String)
      Returns:
      true if no exception was thrown
    • runPlugInFilter

      public static boolean runPlugInFilter(Class<? extends PlugInFilter> clazz)
      Run a PlugInFilter from the associated class with empty argument string. If the plugin's constructor is available, use method run(PlugInFilter) instead.
      Parameters:
      clazz - class of the pluginfilter
      Returns:
      true if no exception was thrown
    • runPlugInFilter

      public static boolean runPlugInFilter(Class<? extends PlugInFilter> clazz, String arg)
      Run a PlugInFilter from the associated class. If the plugin's constructor is available, use method run(PlugInFilter, String) instead.
      Parameters:
      clazz - class of the plugin
      arg - argument string
      Returns:
      true if no exception was thrown
    • runPlugIn

      public static boolean runPlugIn(Class<? extends PlugIn> clazz)
      Run a PlugIn from the associated class with empty argument string. If the plugin's constructor is available, use method run(PlugIn) instead.
      Parameters:
      clazz - class of the plugin
      Returns:
      true if no exception was thrown
    • runPlugIn

      public static boolean runPlugIn(Class<? extends PlugIn> clazz, String arg)
      Run a PlugIn from the associated class. If the plugin's constructor is available, use method run(PlugIn, String) instead.
      Parameters:
      clazz - class of the plugin
      arg - argument string
      Returns:
      true if no exception was thrown
    • convolveX

      public static void convolveX(ImageProcessor ip, float[] h)
      Applies a one-dimensional convolution kernel to the given image, which is modified. The 1D kernel is applied in horizontal direction only.# The supplied filter kernel is not normalized.
      Parameters:
      ip - the image to be filtered (modified)
      h - the filter kernel
      See Also:
    • convolveY

      public static void convolveY(ImageProcessor ip, float[] h)
      Applies a one-dimensional convolution kernel to the given image, which is modified. The 1D kernel is applied in vertical direction only. The supplied filter kernel must be odd-sized. It is not normalized.
      Parameters:
      ip - the image to be filtered (modified)
      h - the filter kernel
      See Also:
    • convolveXY

      public static void convolveXY(ImageProcessor ip, float[] h)
      Applies a one-dimensional convolution kernel to the given image, which is modified. The same 1D kernel is applied twice, once in horizontal and once in vertical direction. The supplied filter kernel must be odd-sized. It is not normalized.
      Parameters:
      ip - the image to be filtered (modified)
      h - the filter kernel
      See Also:
    • convolve

      public static void convolve(ImageProcessor ip, float[][] H)
      Applies a two-dimensional convolution kernel to the given image, which is modified. The supplied kernel float[x][y] must be rectangular and odd-sized. It is not normalized.
      Parameters:
      ip - the image to be filtered (modified)
      H - the filter kernel
    • save

      public static String save(ImageProcessor ip, String filepath)
      Saves the given ImageProcessor using the specified path. The image file type is inferred from the file extension. TIFF is used if no file extension is given. This method simply invokes IJ.save(ImagePlus, String), creating a temporary and titleless ImagePlus instance. Existing files with the same path are overwritten.
      Parameters:
      ip - a ImageProcessor
      filepath - the path where to save the image, e.g. "C:/tmp/MyImage.png"
      Returns:
      the absolute file path
    • noCurrentImage

      public static boolean noCurrentImage()
      Returns true if no image is currently open in ImageJ.
      Returns:
      true if no image is open
    • checkImageFlagsCurrent

      public static boolean checkImageFlagsCurrent(int flags)

      Returns true if the current (active) image is compatible with the specified flags (as specified by PlugInFilter, typically used to compose the return value of PlugInFilter.setup(String, ImagePlus)). This method emulates the compatibility check performed by ImageJ's built-in PlugInFilterRunner before a PlugInFilter is executed. It may be used, e.g., in the (normally empty) constructor of a class implementing PlugInFilter.

      Example, checking if the current image is either 8-bit or 32-bit gray:

       if (checkImageFlagsCurrent(PlugInFilter.DOES_8G + PlugInFilter.DOES_32)) {
              // some action ...
       }
       
      Parameters:
      flags - int-encoded binary flags
      Returns:
      true if the current image is compatible
      See Also:
    • checkImageFlags

      public static boolean checkImageFlags(ImagePlus im, int flags)
    • countColors

      public static int countColors(ColorProcessor cp)
      Determines how many different colors are contained in the specified 24 bit full-color RGB image.
      Parameters:
      cp - a RGB image
      Returns:
      the number of distinct colors
    • isIjPlugin

      public static boolean isIjPlugin(Class<?> clazz)
      Checks if the specified class implements one of ImageJ's plugin interfaces.
      Parameters:
      clazz - any class
      Returns:
      true iff class implements one of ImageJ's plugin interfaces