Package qupath.lib.awt.common
Class BufferedImageTools
java.lang.Object
qupath.lib.awt.common.BufferedImageTools
Static methods for working with BufferedImages and QuPath objects.
- Author:
- Pete Bankhead
-
Method Summary
Modifier and TypeMethodDescriptionstatic long
computeAboveThresholdCounts
(WritableRaster raster, int band, double threshold, WritableRaster rasterMask) Count the number of above-threshold pixels in a specified band of a raster, with optional mask.static long
computeAboveThresholdCounts
(WritableRaster raster, int band, double threshold, WritableRaster rasterMask, Rectangle bounds) Count the number of above-threshold pixels in a specified band of a raster, with optional mask and/or bounding rectangle.static long[]
computeArgMaxHistogram
(WritableRaster raster, long[] counts, WritableRaster rasterMask) Create a histogram that identifies the channels (bands) of an image with the maximum values according to the argmax criterion.static long[]
computeArgMaxHistogram
(WritableRaster raster, long[] counts, WritableRaster rasterMask, Rectangle bounds) Create a histogram that identifies the channels (bands) of an image with the maximum values according to the argmax criterion, with an optional bounding rectangle.static long[]
computeUnsignedIntHistogram
(WritableRaster raster, long[] counts, WritableRaster rasterMask) Compute the full histogram for a raster containing 8-bit or 16-bit unsigned integer values.static long[]
computeUnsignedIntHistogram
(WritableRaster raster, long[] counts, WritableRaster rasterMask, Rectangle bounds) Compute the full histogram for a raster containing 8-bit or 16-bit unsigned integer values, optionally restricted to a bounding rectangle.static BufferedImage
createROIMask
(int width, int height, ROI roi, double xOrigin, double yOrigin, double downsample) Create a grayscale BufferedImage representing a mask for a specified ROI.static BufferedImage
createROIMask
(int width, int height, ROI roi, RegionRequest request) Create a grayscale BufferedImage representing a mask for a specified ROI.static BufferedImage
createROIMask
(Shape shape, double downsample) Create a Shape mask using the minimal bounding box for the Shape.static BufferedImage
createROIMask
(ROI roi, double downsample) Create a ROI mask using the minimal bounding box for the ROI.static BufferedImage
createShapeMask
(int width, int height, Shape shape, double xOrigin, double yOrigin, double downsample) Create a grayscale BufferedImage representing a mask for a specified ROI.static BufferedImage
crop
(BufferedImage img, int x, int y, int width, int height) Create a crop of the image using the specified bounding box.static BufferedImage
duplicate
(BufferedImage img) Duplicate a BufferedImage.static BufferedImage
ensureBufferedImage
(Image image) Convert anImage
to aBufferedImage
if necessary, or return the original image unchanged if it is already aBufferedImage
.static BufferedImage
ensureBufferedImageType
(BufferedImage img, int requestedType) Convert a BufferedImage to have a specified type.static boolean
is8bitColorType
(int type) Returns true if a BufferedImage type represents an 8-bit color image.static BufferedImage
resize
(BufferedImage img, int finalWidth, int finalHeight, boolean smoothInterpolate) Resize the image to have the requested width/height, using area averaging and bilinear interpolation.static void
setValues
(DataBuffer buffer, double val) Set all values in a DataBuffer.static void
swapRGBOrder
(BufferedImage img, String order) Swap the order of pixels in an RGB image.
-
Method Details
-
createROIMask
Create a grayscale BufferedImage representing a mask for a specified ROI.Pixels inside the ROI will be 255, pixels outside will be 0.
- Parameters:
width
- width of the requested mask imageheight
- height of the requested mask imageroi
- ROI for maskrequest
- region that the mask should correspond to, including the origin (x & y) and downsample factor to use.- Returns:
-
createROIMask
Create a ROI mask using the minimal bounding box for the ROI.- Parameters:
roi
-downsample
-- Returns:
-
createROIMask
public static BufferedImage createROIMask(int width, int height, ROI roi, double xOrigin, double yOrigin, double downsample) Create a grayscale BufferedImage representing a mask for a specified ROI.Pixels inside the ROI will be 255, pixels outside will be 0.
- Parameters:
width
- width of the requested mask imageheight
- height of the requested mask imageroi
- ROI for maskxOrigin
- pixel x coordinate of the top left of the region to include in the mask.yOrigin
- pixel y coordinate of the top left of the region to include in the mask.downsample
- downsample factor to use when generating the mask, i.e. the amount to scale.- Returns:
-
createROIMask
Create a Shape mask using the minimal bounding box for the Shape.- Parameters:
shape
-downsample
-- Returns:
-
createShapeMask
public static BufferedImage createShapeMask(int width, int height, Shape shape, double xOrigin, double yOrigin, double downsample) Create a grayscale BufferedImage representing a mask for a specified ROI.Pixels inside the ROI will be 255, pixels outside will be 0.
- Parameters:
width
- width of the requested mask imageheight
- height of the requested mask imageshape
- Shape for maskxOrigin
- pixel x coordinate of the top left of the region to include in the mask.yOrigin
- pixel y coordinate of the top left of the region to include in the mask.downsample
- downsample factor to use when generating the mask, i.e. the amount to scale.- Returns:
-
ensureBufferedImageType
Convert a BufferedImage to have a specified type.This makes use of
Graphics2D.drawImage
, which imposes limits on supported types (i.e. RGB or 8-bit grayscale), and is therefore not suitable for arbitrary type conversion.A possible use is to ensure that a BGR/RGB/ARGB image is converted to the same representation, for example to allow packed int arrays to be treated in the same way.
Images that already have the same type are returned unchanged.
- Parameters:
img
- the input imagerequestedType
- the type to which the image should be converted- Returns:
- the (possibly-new) output image
- See Also:
-
ensureBufferedImage
Convert anImage
to aBufferedImage
if necessary, or return the original image unchanged if it is already aBufferedImage
.- Parameters:
image
- the image to (possible convert)- Returns:
- a
BufferedImage
-
duplicate
Duplicate a BufferedImage. This retains the same color model, but copies the raster. Properties are copied if non-null.- Parameters:
img
-- Returns:
-
swapRGBOrder
Swap the order of pixels in an RGB image. Specify the order in which each channel should appear in the output image. The color model is unchanged; the purpose of this method is to 'fix' problems that may occur when an RGB image has channels wrongly interpreted (normally BGR rather than RGB).- Parameters:
img
- input imageorder
- a String that is one of "RGB", "RBG", "GRB", "GBR", "BRG", "BGR".- Throws:
IllegalArgumentException
- if the image type is not TYPE_INT_ARGB or TYPE_INT_RGB.
-
setValues
Set all values in a DataBuffer.- Parameters:
buffer
- the buffer containing the banks whose values should be set.val
- the requested value. This will be cast to an int or float if necessary.
-
is8bitColorType
public static boolean is8bitColorType(int type) Returns true if a BufferedImage type represents an 8-bit color image. The precise representation (BGR, RGB, byte, int, with/without alpha) is not important.- Parameters:
type
-- Returns:
- See Also:
-
crop
Create a crop of the image using the specified bounding box.- Parameters:
img
-x
-y
-width
-height
-- Returns:
- cropped image, or a duplicate of the original image if no cropping is required
-
resize
public static BufferedImage resize(BufferedImage img, int finalWidth, int finalHeight, boolean smoothInterpolate) Resize the image to have the requested width/height, using area averaging and bilinear interpolation.- Parameters:
img
- input image to be resizedfinalWidth
- target output widthfinalHeight
- target output heightsmoothInterpolate
- if true, the resize method is permitted to use a smooth interpolation method. If false, nearest-neighbor interpolation is used.- Returns:
- resized image
-
computeUnsignedIntHistogram
public static long[] computeUnsignedIntHistogram(WritableRaster raster, long[] counts, WritableRaster rasterMask) Compute the full histogram for a raster containing 8-bit or 16-bit unsigned integer values.- Parameters:
raster
- the raster containing the data for the histogram; if not TYPE_BYTE or TYPE_USHORT anIllegalArgumentException
will be throwncounts
- histogram counts; if null, a new array will be created. Its must be sufficient for the data type, i.e. 256 or 65536. No size checking is performed, therefore if non-null it must be sufficiently large for the data type.rasterMask
- optional single-channel mask; if not null, corresponding pixels with 0 values in the mask will be skipped- Returns:
- the updated histogram counts
- See Also:
-
computeUnsignedIntHistogram
public static long[] computeUnsignedIntHistogram(WritableRaster raster, long[] counts, WritableRaster rasterMask, Rectangle bounds) Compute the full histogram for a raster containing 8-bit or 16-bit unsigned integer values, optionally restricted to a bounding rectangle.- Parameters:
raster
- the raster containing the data for the histogram; if not TYPE_BYTE or TYPE_USHORT anIllegalArgumentException
will be throwncounts
- histogram counts; if null, a new array will be created. Its must be sufficient for the data type, i.e. 256 or 65536. No size checking is performed, therefore if non-null it must be sufficiently large for the data type.rasterMask
- optional single-channel mask; if not null, corresponding pixels with 0 values in the mask will be skipped.bounds
- optional rectangle defining the bounds of the raster to search. If not null, this is used with the (also optional) mask to restrict the pixels that are used. This can give substantial performance improvements for small masked regions embedded in larger images.- Returns:
- the updated histogram counts
- See Also:
-
computeArgMaxHistogram
public static long[] computeArgMaxHistogram(WritableRaster raster, long[] counts, WritableRaster rasterMask) Create a histogram that identifies the channels (bands) of an image with the maximum values according to the argmax criterion.- Parameters:
raster
- the multi-band raster containing values to checkcounts
- existing histogram if it should be updated, or null if a new histogram should be created. The length should match the number of bands in the raster.rasterMask
- optional single-channel mask; if not null, corresponding pixels with 0 values in the mask will be skipped- Returns:
- See Also:
-
computeArgMaxHistogram
public static long[] computeArgMaxHistogram(WritableRaster raster, long[] counts, WritableRaster rasterMask, Rectangle bounds) Create a histogram that identifies the channels (bands) of an image with the maximum values according to the argmax criterion, with an optional bounding rectangle.- Parameters:
raster
- the multi-band raster containing values to checkcounts
- existing histogram if it should be updated, or null if a new histogram should be created. The length should match the number of bands in the raster.rasterMask
- optional single-channel mask; if not null, corresponding pixels with 0 values in the mask will be skippedbounds
- optional rectangle defining the bounds of the raster to search. If not null, this is used with the (also optional) mask to restrict the pixels that are used. This can give substantial performance improvements for small masked regions embedded in larger images.- Returns:
- See Also:
-
computeAboveThresholdCounts
public static long computeAboveThresholdCounts(WritableRaster raster, int band, double threshold, WritableRaster rasterMask) Count the number of above-threshold pixels in a specified band of a raster, with optional mask.- Parameters:
raster
- the multi-band raster containing values to checkband
- the band (channel) to considerthreshold
- threshold value; pixels with values > threshold this will be countedrasterMask
- optional single-channel mask; if not null, corresponding pixels with 0 values in the mask will be skipped- Returns:
- See Also:
-
computeAboveThresholdCounts
public static long computeAboveThresholdCounts(WritableRaster raster, int band, double threshold, WritableRaster rasterMask, Rectangle bounds) Count the number of above-threshold pixels in a specified band of a raster, with optional mask and/or bounding rectangle.- Parameters:
raster
- the multi-band raster containing values to checkband
- the band (channel) to considerthreshold
- threshold value; pixels with values > threshold this will be countedrasterMask
- optional single-channel mask; if not null, corresponding pixels with 0 values in the mask will be skippedbounds
- optional rectangle defining the bounds of the raster to search. If not null, this is used with the (also optional) mask to restrict the pixels that are used. This can give substantial performance improvements for small masked regions embedded in larger images.- Returns:
- See Also:
-