java.lang.Object
imagingbook.common.ij.IjUtils
This class defines static utility methods adding to ImageJs functionality.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic booleancheckImageFlags(ImagePlus im, int flags) static booleancheckImageFlagsCurrent(int flags) Returns true if the current (active) image is compatible with the specified flags (as specified byPlugInFilter, typically used to compose the return value ofPlugInFilter.setup(String, ImagePlus)).static Pnt2d[]Collects all image coordinates with non-zero pixel values into an array of 2D points (Pnt2d).static BitMapConverts the specifiedByteProcessorto aBitMapof the same size, with all zero values set to 0 and non-zero values set to 1.static ByteProcessorconvertToByteProcessor(BitMap bitmap) Converts the specifiedBitMapto aByteProcessorof the same size, with all zero values set to 0 and non-zero values set to 1.static voidconvolve(ImageProcessor ip, float[][] H) Applies a two-dimensional convolution kernel to the given image, which is modified.static voidconvolveX(ImageProcessor ip, float[] h) Applies a one-dimensional convolution kernel to the given image, which is modified.static voidconvolveXY(ImageProcessor ip, float[] h) Applies a one-dimensional convolution kernel to the given image, which is modified.static voidconvolveY(ImageProcessor ip, float[] h) Applies a one-dimensional convolution kernel to the given image, which is modified.static intDetermines how many different colors are contained in the specified 24 bit full-color RGB image.static ImagePluscreateImage(String title, double[][] M) Creates an ImageJImagePlusimage for the matrixM[r][c](2D array), whereris treated as the row (vertical) coordinate andcis treated as the column (horizontal) coordinate.static ImagePluscreateImage(String title, float[][] M) Creates an ImageJImagePlusimage for the matrixM[r][c](2D array), whereris treated as the row (vertical) coordinate andcis treated as the column (horizontal) coordinate.static <T extends ImageProcessor>
Tcrop(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 ofImageProcessor).static voiddrawPoints(ImageProcessor ip, Pnt2d[] points, int value) Draws the given set of points onto the specified image (by setting the corresponding pixels).static String[]getImageShortTitles(ImagePlus[] images) Returns an array of strings containing the short titles of the images supplied.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.static ImagePlus[]getOpenImages(boolean sortByTitle, ImagePlus exclude) Returns a (possibly empty) array ofImagePlusobjects that are sorted by their titles if the sortByTitle flag is set.static booleanChecks if the given image is possibly a binary image.static booleanisFlat(ImageProcessor ip) Checks if the given image is "flat", i.e., all pixels have the same value.static booleanisIjPlugin(Class<?> clazz) Checks if the specified class implements one of ImageJ's plugin interfaces.static booleanmatch(ImageProcessor ip1, ImageProcessor ip2) Checks if two images have the same type, size and content (usingDefaultMatchTolerancefor float images).static booleanmatch(ImageProcessor ip1, ImageProcessor ip2, double tolerance) Checks if two images have the same type, size and values (using the specified tolerance for float images).static booleanReturns true if no image is currently open in ImageJ.static ImagePlusOpens the image from the specified filename and returns it as aImagePlusinstance.static ImagePlusstatic booleanrun(PlugInFilter pluginfilter) Runs the givenPlugInFilterinstance with empty argument string.static booleanrun(PlugInFilter pluginfilter, String arg) Runs the givenPlugInFilterinstance.static booleanRuns the givenPlugIninstance with empty argument string.static booleanRuns the givenPlugIninstance.static booleanRun aPlugInfrom the associated class with empty argument string.static booleanRun aPlugInfrom the associated class.static booleanrunPlugInFilter(Class<? extends PlugInFilter> clazz) Run aPlugInFilterfrom the associated class with empty argument string.static booleanrunPlugInFilter(Class<? extends PlugInFilter> clazz, String arg) Run aPlugInFilterfrom the associated class.static booleansameSize(ImageProcessor ip1, ImageProcessor ip2) Checks if two images have the same size.static booleansameType(ImageProcessor ip1, ImageProcessor ip2) Checks if two images are of the same type.static Stringsave(ImageProcessor ip, String filepath) Saves the givenImageProcessorusing the specified path.static ImagePlusselectOpenImage(String title) Opens a dialog to let the user select one of the currently open images.static voidSets the weighing factors for the color components used in RGB-to-grayscale conversion for the specified imageip.static voidsetRgbConversionWeights(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 imageip.static byte[][]Creates and returns a newbyte[][]from the specifiedByteProcessor.static ByteProcessortoByteProcessor(byte[][] A) Creates and returns a newByteProcessorfrom the specified 2Dbytearray, assumed to be arranged in the formA[x][y], i.e., the first coordinate is horizontal, the second vertical.static ByteProcessortoByteProcessor(int[][] A) Creates and returns a newByteProcessorfrom the specified 2Dintarray, assumed to be arranged in the formA[x][y], i.e., the first coordinate is horizontal, the second vertical.static ByteProcessorConverts the given RGBColorProcessorto a scalar-valuedByteProcessor, using clearly specified RGB component weights.static ByteProcessortoByteProcessor(ColorProcessor cp, double[] rgbWeights) Converts the given RGBColorProcessorto a scalar-valuedByteProcessor, applying the specified set of RGB component weights.static double[][]Returns a copy of the pixel data as a 2D double array with dimensions [x = 0,..,width-1][y = 0,..,height-1].static float[][]Converts aFloatProcessorto afloat[][].static FloatProcessortoFloatProcessor(double[][] A) Creates a newFloatProcessorinstance of size width x height from the givendouble[][]with dimensions [x = 0,..,width-1][y = 0,..,height-1].static FloatProcessortoFloatProcessor(float[][] A) Creates a newFloatProcessorinstance of size width x height from the givenfloat[][]with dimensions [x = 0,..,width-1][y = 0,..,height-1].static FloatProcessorConverts the given RGBColorProcessorto a scalar-valuedFloatProcessor, using clearly specified RGB component weights.static FloatProcessortoFloatProcessor(ColorProcessor cp, double[] rgbWeights) Converts the given RGBColorProcessorto a scalar-valuedFloatProcessor, applying the specified set of RGB component weights.static int[][]Creates and returns a newint[][]from the specifiedByteProcessor.
-
Field Details
-
DefaultMatchTolerance
- See Also:
-
-
Method Details
-
getOpenImages
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
Returns an array of strings containing the short titles of the images supplied.- Parameters:
images- array of images.- Returns:
- array of names.
-
selectOpenImage
Opens a dialog to let the user select one of the currently open images.- Parameters:
title- string to show in the dialog- Returns:
- a
ImagePlusobject, use the getProcessor method to obtain the associatedImageProcessor
-
getOpenImages
Returns a (possibly empty) array ofImagePlusobjects 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- settrueto return images sorted by titleexclude- reference to an image to be excluded (may benull)- Returns:
- a (possibly empty) array of
ImagePlusobjects
-
createImage
Creates an ImageJImagePlusimage for the matrixM[r][c](2D array), whereris treated as the row (vertical) coordinate andcis treated as the column (horizontal) coordinate. Useshow()to display the resulting image.- Parameters:
title- image titleM- 2D array- Returns:
- a new
ImagePlusimage
-
createImage
Creates an ImageJImagePlusimage for the matrixM[r][c](2D array), whereris treated as the row (vertical) coordinate andcis treated as the column (horizontal) coordinate. Useshow()to display the resulting image.- Parameters:
title- the image titleM- a 2D array holding the image data- Returns:
- a new
ImagePlusinstance
-
setRgbConversionWeights
Sets the weighing factors for the color components used in RGB-to-grayscale conversion for the specified imageip. Note that this method can be applied to anyImageProcessorinstance but has no effect unlessipis of typeColorProcessor. Applies standard (ITU-709) weights.- Parameters:
ip- the affected image
-
setRgbConversionWeights
Sets the weighing factors for the color components used in RGB-to-grayscale conversion for the specified imageip. Note that this method can be applied to anyImageProcessorinstance but has no effect unlessipis of typeColorProcessor.- Parameters:
ip- the affected imagewr- red component weightwg- green component weightwb- blue component weight
-
crop
Extracts (crops) a rectangular region from the given image and returns it as a new image (of the same sub-type ofImageProcessor). 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.nullis returned if the rectangle does not overlap the image at all.- Type Parameters:
T- the generic image type- Parameters:
ip- the image to be croppedx- the left corner coordinate of the cropping rectangley- the top corner coordinate of the cropping rectanglewidth- the width of the cropping rectangleheight- the height of the cropping rectangle- Returns:
- the cropped image
-
toDoubleArray
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
Creates a newFloatProcessorinstance of size width x height from the givendouble[][]with dimensions [x = 0,..,width-1][y = 0,..,height-1].- Parameters:
A- a 2Ddoublearray- Returns:
- a new
FloatProcessorinstance
-
toFloatProcessor
Creates a newFloatProcessorinstance of size width x height from the givenfloat[][]with dimensions [x = 0,..,width-1][y = 0,..,height-1].- Parameters:
A- a 2Dfloatarray- Returns:
- a new
FloatProcessorinstance
-
toFloatArray
Converts aFloatProcessorto afloat[][].- Parameters:
fp- aFloatProcessor- Returns:
- the resulting
float[][]
-
toByteProcessor
Converts the given RGBColorProcessorto a scalar-valuedByteProcessor, 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 (seeRgbUtils.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 alsoColorProcessor.getRGBWeights(),ColorProcessor.setRGBWeights(double[]),ImageProcessor.convertToByteProcessor().- Parameters:
cp- aColorProcessor- Returns:
- the resulting
ByteProcessor - See Also:
-
toByteProcessor
Converts the given RGBColorProcessorto a scalar-valuedByteProcessor, 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 alsoColorProcessor.getRGBWeights(),ColorProcessor.setRGBWeights(double[]),ImageProcessor.convertToByteProcessor().- Parameters:
cp- aColorProcessorrgbWeights- a 3-vector of RGB component weights (must sum to 1)- Returns:
- the resulting
ByteProcessor - See Also:
-
toFloatProcessor
Converts the given RGBColorProcessorto a scalar-valuedFloatProcessor, using clearly specified RGB component weights. The processor's individual RGB component weights are used if set, otherweise default weights are used (seeRgbUtils.getDefaultWeights()). This should avoid problems with standard conversion methods in ImageJ, which depend on a variety of factors (including current user settings). See alsoColorProcessor.getRGBWeights(),ColorProcessor.setRGBWeights(double[]),ImageProcessor.convertToFloatProcessor().- Parameters:
cp- aColorProcessor- Returns:
- the resulting
FloatProcessor - See Also:
-
toFloatProcessor
Converts the given RGBColorProcessorto a scalar-valuedFloatProcessor, applying the specified set of RGB component weights. Ifnullis passed for the weights, default weights are used (seeRgbUtils.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 alsoColorProcessor.getRGBWeights(),ColorProcessor.setRGBWeights(double[]),ImageProcessor.convertToFloatProcessor().- Parameters:
cp- aColorProcessorrgbWeights- a 3-vector of RGB component weights (must sum to 1)- Returns:
- the resulting
FloatProcessor - See Also:
-
toByteProcessor
Creates and returns a newByteProcessorfrom the specified 2Dbytearray, assumed to be arranged in the formA[x][y], i.e., the first coordinate is horizontal, the second vertical. ThusA.lengthis the width andA[0].lengththe height of the resulting image.- Parameters:
A- a 2Dbytearray- Returns:
- a new
ByteProcessorof sizeA.lengthxA[0].length
-
toByteArray
Creates and returns a newbyte[][]from the specifiedByteProcessor. The resulting array is arranged in the formA[x][y], i.e., the first coordinate is horizontal, the second vertical. ThusA.lengthis the width andA[0].lengththe height of the image.- Parameters:
bp- aByteProcessor- Returns:
- a 2D
bytearray
-
toByteProcessor
Creates and returns a newByteProcessorfrom the specified 2Dintarray, assumed to be arranged in the formA[x][y], i.e., the first coordinate is horizontal, the second vertical. ThusA.lengthis the width andA[0].lengththe height of the resulting image. Pixel values are clamped to [0, 255].- Parameters:
A- a 2Dintarray- Returns:
- a new
ByteProcessorof sizeA.lengthxA[0].length
-
toIntArray
Creates and returns a newint[][]from the specifiedByteProcessor. The resulting array is arranged in the formA[x][y], i.e., the first coordinate is horizontal, the second vertical. ThusA.lengthis the width andA[0].lengththe height of the image.- Parameters:
bp- aByteProcessor- Returns:
- a 2D
intarray
-
openImage
- Parameters:
uri- the URI leading to the image (including extension)- Returns:
- a new
ImagePlusinstance ornullif unable to open
-
openImage
Opens the image from the specified filename and returns it as aImagePlusinstance.- Parameters:
filename- the path and filename to be opened- Returns:
- a new
ImagePlusinstance ornullif unable to open
-
sameType
Checks if two images are of the same type.- Parameters:
ip1- the first imageip2- the second image- Returns:
- true if both images have the same type
-
sameSize
Checks if two images have the same size.- Parameters:
ip1- the first imageip2- the second image- Returns:
- true if both images have the same size
-
isBinary
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
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
Checks if two images have the same type, size and content (usingDefaultMatchTolerancefor float images).- Parameters:
ip1- the first imageip2- the second image- Returns:
- true if both images have the same type and content
-
match
Checks if two images have the same type, size and values (using the specified tolerance for float images).- Parameters:
ip1- the first imageip2- the second imagetolerance- the matching tolerance- Returns:
- true if both images have the same type, size and content
-
convertToBitMap
Converts the specifiedByteProcessorto aBitMapof the same size, with all zero values set to 0 and non-zero values set to 1.- Parameters:
bp- aByteProcessor- Returns:
- the corresponding
BitMap - See Also:
-
convertToByteProcessor
Converts the specified
BitMapto aByteProcessorof 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- aBitMap- Returns:
- the corresponding
ByteProcessor - See Also:
-
drawPoints
Draws the given set of points onto the specified image (by setting the corresponding pixels).- Parameters:
ip- the image to draw topoints- the 2D pointsvalue- the pixel value to use
-
run
Runs the givenPlugInFilterinstance with empty argument string.- Parameters:
pluginfilter- an instance ofPlugInFilter- Returns:
- true if no exception was thrown
-
run
Runs the givenPlugInFilterinstance.- Parameters:
pluginfilter- an instance ofPlugInFilterarg- argument passed toPlugInFilter.setup(String, ImagePlus)- Returns:
- true if no exception was thrown
-
run
Runs the givenPlugIninstance with empty argument string.- Parameters:
plugin- an instance ofPlugIn- Returns:
- true if no exception was thrown
-
run
Runs the givenPlugIninstance.- Parameters:
plugin- an instance ofPlugInarg- argument passed toPlugIn.run(String)- Returns:
- true if no exception was thrown
-
runPlugInFilter
Run aPlugInFilterfrom the associated class with empty argument string. If the plugin's constructor is available, use methodrun(PlugInFilter)instead.- Parameters:
clazz- class of the pluginfilter- Returns:
- true if no exception was thrown
-
runPlugInFilter
Run aPlugInFilterfrom the associated class. If the plugin's constructor is available, use methodrun(PlugInFilter, String)instead.- Parameters:
clazz- class of the pluginarg- argument string- Returns:
- true if no exception was thrown
-
runPlugIn
Run aPlugInfrom the associated class with empty argument string. If the plugin's constructor is available, use methodrun(PlugIn)instead.- Parameters:
clazz- class of the plugin- Returns:
- true if no exception was thrown
-
runPlugIn
Run aPlugInfrom the associated class. If the plugin's constructor is available, use methodrun(PlugIn, String)instead.- Parameters:
clazz- class of the pluginarg- argument string- Returns:
- true if no exception was thrown
-
convolveX
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
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
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
Applies a two-dimensional convolution kernel to the given image, which is modified. The supplied kernelfloat[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
Saves the givenImageProcessorusing 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 invokesIJ.save(ImagePlus, String), creating a temporary and titlelessImagePlusinstance. Existing files with the same path are overwritten.- Parameters:
ip- aImageProcessorfilepath- the path where to save the image, e.g."C:/tmp/MyImage.png"- Returns:
- the absolute file path
-
noCurrentImage
Returns true if no image is currently open in ImageJ.- Returns:
- true if no image is open
-
checkImageFlagsCurrent
Returns true if the current (active) image is compatible with the specified flags (as specified by
PlugInFilter, typically used to compose the return value ofPlugInFilter.setup(String, ImagePlus)). This method emulates the compatibility check performed by ImageJ's built-inPlugInFilterRunnerbefore aPlugInFilteris executed. It may be used, e.g., in the (normally empty) constructor of a class implementingPlugInFilter.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
-
countColors
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
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
-