Package qupath.lib.awt.common
Class BufferedImageTools
java.lang.Object
qupath.lib.awt.common.BufferedImageTools
Static methods for working with BufferedImages and QuPath objects.
- 
Method SummaryModifier and TypeMethodDescriptionstatic longcomputeAboveThresholdCounts(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 longcomputeAboveThresholdCounts(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 BufferedImageconvertImageType(BufferedImage img, PixelType targetPixelType, List<? extends ImageChannel> channels) Convert a raster to have a specified pixel type.static WritableRasterconvertRasterType(Raster inputRaster, PixelType targetPixelType) Convert a raster to have a specified pixel type.static BufferedImagecreateImage(int width, int height, PixelType pixelType, int nChannels) Create a new image with the specified width, height, pixel type, and number of channels.static BufferedImagecreateImage(int width, int height, PixelType pixelType, List<? extends ImageChannel> channels) Create a new image with the specified width, height, pixel type, and channels.static BufferedImagecreateROIMask(int width, int height, ROI roi, double xOrigin, double yOrigin, double downsample) Create a grayscale BufferedImage representing a mask for a specified ROI.static BufferedImagecreateROIMask(int width, int height, ROI roi, RegionRequest request) Create a grayscale BufferedImage representing a mask for a specified ROI.static BufferedImagecreateROIMask(Shape shape, double downsample) Create a Shape mask using the minimal bounding box for the Shape.static BufferedImagecreateROIMask(ROI roi, double downsample) Create a ROI mask using the minimal bounding box for the ROI.static BufferedImagecreateShapeMask(int width, int height, Shape shape, double xOrigin, double yOrigin, double downsample) Create a grayscale BufferedImage representing a mask for a specified ROI.static BufferedImagecrop(BufferedImage img, int x, int y, int width, int height) Create a crop of the image using the specified bounding box.static BufferedImageduplicate(BufferedImage img) Duplicate a BufferedImage.static BufferedImageensureBufferedImage(Image image) Convert anImageto aBufferedImageif necessary, or return the original image unchanged if it is already aBufferedImage.static BufferedImageensureBufferedImageType(BufferedImage img, int requestedType) Convert a BufferedImage to have a specified type.static intgetDataBufferType(PixelType pixelType) Get the appropriate DataBuffer type for a given PixelType.static booleanis8bitColorType(int type) Returns true if a BufferedImage type represents an 8-bit color image.static BufferedImageresize(BufferedImage img, int finalWidth, int finalHeight, boolean smoothInterpolate) Resize the image to have the requested width/height, using area averaging and bilinear interpolation.static voidsetValues(DataBuffer buffer, double val) Set all values in a DataBuffer.static BufferedImageRemove the alpha channel for an 8-bit color BufferedImage if it contains only 255 for all pixels.static voidswapRGBOrder(BufferedImage img, String order) Swap the order of pixels in an RGB image.
- 
Method Details- 
createROIMaskCreate 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 image
- height- height of the requested mask image
- roi- ROI for mask
- request- region that the mask should correspond to, including the origin (x & y) and downsample factor to use.
- Returns:
 
- 
createROIMaskCreate a ROI mask using the minimal bounding box for the ROI.- Parameters:
- roi-
- downsample-
- Returns:
 
- 
createROIMaskpublic 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 image
- height- height of the requested mask image
- roi- ROI for mask
- xOrigin- 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:
 
- 
createROIMaskCreate a Shape mask using the minimal bounding box for the Shape.- Parameters:
- shape-
- downsample-
- Returns:
 
- 
createShapeMaskpublic 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 image
- height- height of the requested mask image
- shape- Shape for mask
- xOrigin- 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:
 
- 
ensureBufferedImageTypeConvert 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 image
- requestedType- the type to which the image should be converted
- Returns:
- the (possibly-new) output image
- See Also:
 
- 
ensureBufferedImageConvert anImageto aBufferedImageif necessary, or return the original image unchanged if it is already aBufferedImage.- Parameters:
- image- the image to (possible convert)
- Returns:
- a BufferedImage
 
- 
stripEmptyAlphaRemove the alpha channel for an 8-bit color BufferedImage if it contains only 255 for all pixels. Does nothing ifis8bitColorType(img.getType())returns false.The purpose of this is to get rid of an alpha channel if it has no effect, which may improve efficiency or reduce file sizes if the image is saved. - Parameters:
- img-
- Returns:
- the input image unchanged, or a converted image of type TYPE_INT_RGB.
 
- 
duplicateDuplicate a BufferedImage. This retains the same color model, but copies the raster. Properties are copied if non-null.- Parameters:
- img-
- Returns:
 
- 
swapRGBOrderSwap 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 image
- order- 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.
 
- 
setValuesSet 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.
 
- 
is8bitColorTypepublic 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:
 
- 
cropCreate 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
 
- 
resizepublic 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 resized
- finalWidth- target output width
- finalHeight- target output height
- smoothInterpolate- if true, the resize method is permitted to use a smooth interpolation method. If false, nearest-neighbor interpolation is used.
- Returns:
- resized image
- Implementation Note
- the method of smooth interpolation is not currently specified or adjustable. Since v0.4.0, it uses ImageJ's bilinear interpolation with area averaging - however this may change.
 
- 
computeUnsignedIntHistogrampublic 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 an- IllegalArgumentExceptionwill be thrown
- counts- 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:
 
- 
computeUnsignedIntHistogrampublic 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 an- IllegalArgumentExceptionwill be thrown
- counts- 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:
 
- 
computeArgMaxHistogrampublic 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 check
- counts- 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:
 
- 
computeArgMaxHistogrampublic 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 check
- counts- 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
- 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:
- See Also:
 
- 
computeAboveThresholdCountspublic 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 check
- band- the band (channel) to consider
- threshold- threshold value; pixels with values > threshold this will be counted
- rasterMask- optional single-channel mask; if not null, corresponding pixels with 0 values in the mask will be skipped
- Returns:
- See Also:
 
- 
computeAboveThresholdCountspublic 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 check
- band- the band (channel) to consider
- threshold- threshold value; pixels with values > threshold this will be counted
- 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:
- See Also:
- Implementation Note
- use of the bounding rectangle is not yet implemented
 
- 
convertImageTypepublic static BufferedImage convertImageType(BufferedImage img, PixelType targetPixelType, List<? extends ImageChannel> channels) Convert a raster to have a specified pixel type. Note that this is not intended for RGB images, and the output will not be a specific RGB type.- Parameters:
- img- the input image
- targetPixelType- the required pixel type of the output
- channels- the channels used to create the color model; if null, the default channels for the pixel type will be used
- Returns:
- a new image containing pixels converted to the required type
- Throws:
- UnsupportedOperationException- if the pixel type is not supported (e.g. INT8 or UINT32).
 
- 
convertRasterTypepublic static WritableRaster convertRasterType(Raster inputRaster, PixelType targetPixelType) throws UnsupportedOperationException Convert a raster to have a specified pixel type. If the output is an integer type, rounding will be applied and values will be clipped to the permitted range.- Parameters:
- inputRaster- raster containing the input pixels
- targetPixelType- pixel type to which the raster should be converted
- Returns:
- a new raster containing pixels converted to the required type
- Throws:
- UnsupportedOperationException- if the pixel type is not supported (e.g. INT8 or UINT32).
 
- 
getDataBufferTypeGet the appropriate DataBuffer type for a given PixelType.- Parameters:
- pixelType- the pixel type for which the DataBuffer type is required
- Returns:
- the integer corresponding to the DataBuffer type.
- Throws:
- UnsupportedOperationException- if the pixel type is not supported (e.g. INT8 or UINT32).
- See Also:
 
- 
createImageCreate a new image with the specified width, height, pixel type, and number of channels. Note that this is not intended for RGB images, which already have straightforward BufferedImage constructors.- Parameters:
- width- the width of the image
- height- the height of the image
- pixelType- the pixel type of the image
- nChannels- the number of channels to use in the image; the default channel colors will be used
- Returns:
- a new image with the specified properties
 
- 
createImagepublic static BufferedImage createImage(int width, int height, PixelType pixelType, List<? extends ImageChannel> channels) Create a new image with the specified width, height, pixel type, and channels. Note that this is not intended for RGB images, which already have straightforward BufferedImage constructors.- Parameters:
- width- the width of the image
- height- the height of the image
- pixelType- the pixel type of the image
- channels- the channels to use in the image
- Returns:
- a new image with the specified properties
 
 
-