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 boolean
checkImageFlags
(ImagePlus im, int flags) static boolean
checkImageFlagsCurrent
(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 BitMap
Converts the specifiedByteProcessor
to aBitMap
of the same size, with all zero values set to 0 and non-zero values set to 1.static ByteProcessor
convertToByteProcessor
(BitMap bitmap) Converts the specifiedBitMap
to aByteProcessor
of the same size, with all zero values set to 0 and non-zero values set to 1.static void
convolve
(ImageProcessor ip, float[][] H) Applies a two-dimensional convolution kernel to the given image, which is modified.static void
convolveX
(ImageProcessor ip, float[] h) Applies a one-dimensional convolution kernel to the given image, which is modified.static void
convolveXY
(ImageProcessor ip, float[] h) Applies a one-dimensional convolution kernel to the given image, which is modified.static void
convolveY
(ImageProcessor ip, float[] h) Applies a one-dimensional convolution kernel to the given image, which is modified.static int
Determines how many different colors are contained in the specified 24 bit full-color RGB image.static ImagePlus
createImage
(String title, double[][] M) Creates an ImageJImagePlus
image for the matrixM[r][c]
(2D array), wherer
is treated as the row (vertical) coordinate andc
is treated as the column (horizontal) coordinate.static ImagePlus
createImage
(String title, float[][] M) Creates an ImageJImagePlus
image for the matrixM[r][c]
(2D array), wherer
is treated as the row (vertical) coordinate andc
is 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 void
drawPoints
(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 ofImagePlus
objects that are sorted by their titles if the sortByTitle flag is set.static boolean
Checks if the given image is possibly a binary image.static boolean
isFlat
(ImageProcessor ip) Checks if the given image is "flat", i.e., all pixels have the same value.static boolean
isIjPlugin
(Class<?> clazz) Checks if the specified class implements one of ImageJ's plugin interfaces.static boolean
match
(ImageProcessor ip1, ImageProcessor ip2) Checks if two images have the same type, size and content (usingDefaultMatchTolerance
for float images).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).static boolean
Returns true if no image is currently open in ImageJ.static ImagePlus
Opens the image from the specified filename and returns it as aImagePlus
instance.static ImagePlus
static boolean
run
(PlugInFilter pluginfilter) Runs the givenPlugInFilter
instance with empty argument string.static boolean
run
(PlugInFilter pluginfilter, String arg) Runs the givenPlugInFilter
instance.static boolean
Runs the givenPlugIn
instance with empty argument string.static boolean
Runs the givenPlugIn
instance.static boolean
Run aPlugIn
from the associated class with empty argument string.static boolean
Run aPlugIn
from the associated class.static boolean
runPlugInFilter
(Class<? extends PlugInFilter> clazz) Run aPlugInFilter
from the associated class with empty argument string.static boolean
runPlugInFilter
(Class<? extends PlugInFilter> clazz, String arg) Run aPlugInFilter
from the associated class.static boolean
sameSize
(ImageProcessor ip1, ImageProcessor ip2) Checks if two images have the same size.static boolean
sameType
(ImageProcessor ip1, ImageProcessor ip2) Checks if two images are of the same type.static String
save
(ImageProcessor ip, String filepath) Saves the givenImageProcessor
using the specified path.static ImagePlus
selectOpenImage
(String title) Opens a dialog to let the user select one of the currently open images.static void
Sets the weighing factors for the color components used in RGB-to-grayscale conversion for the specified imageip
.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 imageip
.static byte[][]
Creates and returns a newbyte[][]
from the specifiedByteProcessor
.static ByteProcessor
toByteProcessor
(byte[][] A) Creates and returns a newByteProcessor
from the specified 2Dbyte
array, assumed to be arranged in the formA[x][y]
, i.e., the first coordinate is horizontal, the second vertical.static ByteProcessor
toByteProcessor
(int[][] A) Creates and returns a newByteProcessor
from the specified 2Dint
array, assumed to be arranged in the formA[x][y]
, i.e., the first coordinate is horizontal, the second vertical.static ByteProcessor
Converts the given RGBColorProcessor
to a scalar-valuedByteProcessor
, using clearly specified RGB component weights.static ByteProcessor
toByteProcessor
(ColorProcessor cp, double[] rgbWeights) Converts the given RGBColorProcessor
to 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 aFloatProcessor
to afloat[][]
.static FloatProcessor
toFloatProcessor
(double[][] A) Creates a newFloatProcessor
instance of size width x height from the givendouble[][]
with dimensions [x = 0,..,width-1][y = 0,..,height-1].static FloatProcessor
toFloatProcessor
(float[][] A) Creates a newFloatProcessor
instance of size width x height from the givenfloat[][]
with dimensions [x = 0,..,width-1][y = 0,..,height-1].static FloatProcessor
Converts the given RGBColorProcessor
to a scalar-valuedFloatProcessor
, using clearly specified RGB component weights.static FloatProcessor
toFloatProcessor
(ColorProcessor cp, double[] rgbWeights) Converts the given RGBColorProcessor
to 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
ImagePlus
object, use the getProcessor method to obtain the associatedImageProcessor
-
getOpenImages
Returns a (possibly empty) array ofImagePlus
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
- settrue
to return images sorted by titleexclude
- reference to an image to be excluded (may benull
)- Returns:
- a (possibly empty) array of
ImagePlus
objects
-
createImage
Creates an ImageJImagePlus
image for the matrixM[r][c]
(2D array), wherer
is treated as the row (vertical) coordinate andc
is treated as the column (horizontal) coordinate. Useshow()
to display the resulting image.- Parameters:
title
- image titleM
- 2D array- Returns:
- a new
ImagePlus
image
-
createImage
Creates an ImageJImagePlus
image for the matrixM[r][c]
(2D array), wherer
is treated as the row (vertical) coordinate andc
is 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
ImagePlus
instance
-
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 anyImageProcessor
instance but has no effect unlessip
is 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 anyImageProcessor
instance but has no effect unlessip
is 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.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 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 newFloatProcessor
instance of size width x height from the givendouble[][]
with dimensions [x = 0,..,width-1][y = 0,..,height-1].- Parameters:
A
- a 2Ddouble
array- Returns:
- a new
FloatProcessor
instance
-
toFloatProcessor
Creates a newFloatProcessor
instance of size width x height from the givenfloat[][]
with dimensions [x = 0,..,width-1][y = 0,..,height-1].- Parameters:
A
- a 2Dfloat
array- Returns:
- a new
FloatProcessor
instance
-
toFloatArray
Converts aFloatProcessor
to afloat[][]
.- Parameters:
fp
- aFloatProcessor
- Returns:
- the resulting
float[][]
-
toByteProcessor
Converts the given RGBColorProcessor
to 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 RGBColorProcessor
to 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
- aColorProcessor
rgbWeights
- a 3-vector of RGB component weights (must sum to 1)- Returns:
- the resulting
ByteProcessor
- See Also:
-
toFloatProcessor
Converts the given RGBColorProcessor
to 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 RGBColorProcessor
to a scalar-valuedFloatProcessor
, applying the specified set of RGB component weights. Ifnull
is 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
- aColorProcessor
rgbWeights
- a 3-vector of RGB component weights (must sum to 1)- Returns:
- the resulting
FloatProcessor
- See Also:
-
toByteProcessor
Creates and returns a newByteProcessor
from the specified 2Dbyte
array, assumed to be arranged in the formA[x][y]
, i.e., the first coordinate is horizontal, the second vertical. ThusA.length
is the width andA[0].length
the height of the resulting image.- Parameters:
A
- a 2Dbyte
array- Returns:
- a new
ByteProcessor
of sizeA.length
xA[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.length
is the width andA[0].length
the height of the image.- Parameters:
bp
- aByteProcessor
- Returns:
- a 2D
byte
array
-
toByteProcessor
Creates and returns a newByteProcessor
from the specified 2Dint
array, assumed to be arranged in the formA[x][y]
, i.e., the first coordinate is horizontal, the second vertical. ThusA.length
is the width andA[0].length
the height of the resulting image. Pixel values are clamped to [0, 255].- Parameters:
A
- a 2Dint
array- Returns:
- a new
ByteProcessor
of sizeA.length
xA[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.length
is the width andA[0].length
the height of the image.- Parameters:
bp
- aByteProcessor
- Returns:
- a 2D
int
array
-
openImage
- Parameters:
uri
- the URI leading to the image (including extension)- Returns:
- a new
ImagePlus
instance ornull
if unable to open
-
openImage
Opens the image from the specified filename and returns it as aImagePlus
instance.- Parameters:
filename
- the path and filename to be opened- Returns:
- a new
ImagePlus
instance ornull
if 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 (usingDefaultMatchTolerance
for 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 specifiedByteProcessor
to aBitMap
of 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
BitMap
to aByteProcessor
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
- 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 givenPlugInFilter
instance with empty argument string.- Parameters:
pluginfilter
- an instance ofPlugInFilter
- Returns:
- true if no exception was thrown
-
run
Runs the givenPlugInFilter
instance.- Parameters:
pluginfilter
- an instance ofPlugInFilter
arg
- argument passed toPlugInFilter.setup(String, ImagePlus)
- Returns:
- true if no exception was thrown
-
run
Runs the givenPlugIn
instance with empty argument string.- Parameters:
plugin
- an instance ofPlugIn
- Returns:
- true if no exception was thrown
-
run
Runs the givenPlugIn
instance.- Parameters:
plugin
- an instance ofPlugIn
arg
- argument passed toPlugIn.run(String)
- Returns:
- true if no exception was thrown
-
runPlugInFilter
Run aPlugInFilter
from 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 aPlugInFilter
from 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 aPlugIn
from 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 aPlugIn
from 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 givenImageProcessor
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 invokesIJ.save(ImagePlus, String)
, creating a temporary and titlelessImagePlus
instance. Existing files with the same path are overwritten.- Parameters:
ip
- aImageProcessor
filepath
- 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-inPlugInFilterRunner
before aPlugInFilter
is 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
-