Package qupath.opencv.dnn
Class DnnTools
java.lang.Object
qupath.opencv.dnn.DnnTools
Tools for working with OpenCV's DNN module.
- Since:
- 0.3.0
- Author:
- Pete Bankhead
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Helper class to summarize a DNN layer. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Mat
blobFromImage
(Mat mat) Create an OpenCV blob from a single mat.static Mat
blobFromImages
(Collection<Mat> mats, double scaleFactor, Size size, Scalar mean, boolean swapRB, boolean crop) Create an OpenCV blob from a batch of Mats with optional scaling, resizing and cropping.static Mat
blobFromImages
(Mat... mats) Create an OpenCV blob from one or more mats.static Mat
blobFromImages
(Mat mat, double scaleFactor, Size size, Scalar mean, boolean swapRB, boolean crop) Create an OpenCV blob from a Mat with optional scaling, resizing and cropping.static OpenCVDnn.Builder
Initiative building and configuring anOpenCVDnn
.static boolean
classify
(ClassificationModel model, PathObject pathObject, Mat input, IntFunction<PathClass> classifier, String predictionMeasurement) Apply a classification model to an existing image patch to classify an object.static boolean
classify
(ClassificationModel model, PathObject pathObject, ImageServer<BufferedImage> server, double downsample, int width, int height, IntFunction<PathClass> classifier, String predictionMeasurement) Apply a classification model to an image patch to classify an object.static boolean
classify
(ClassificationModel model, PathObject pathObject, ImageServer<BufferedImage> server, double downsample, IntFunction<PathClass> classifier, String predictionMeasurement) Apply a classification model to an image patch to classify an object.static List
<PathObject> detect
(DetectionModel model, Mat mat, RegionRequest request, IntFunction<PathClass> classifier, Function<ROI, PathObject> creator) Apply a detection model to generate rectangles surrounding distinct structures.static List
<PathObject> detect
(DetectionModel model, ImageServer<BufferedImage> server, RegionRequest request, IntFunction<PathClass> classifier, Function<ROI, PathObject> creator) Apply a detection model to generate rectangles surrounding distinct structures.getOutputLayerNames
(Net net) Get the names of all unconnected output layers.getOutputLayers
(Net net, DnnShape... inputShape) Get the names of all unconnected output layers.imagesFromBlob
(Mat blob) Extract images from an OpenCV blob.static boolean
Query whether CUDA is reported as available by OpenCV.static List
<DnnTools.DNNLayer> parseLayers
(Net net, int width, int height, int channels, int batchSize) Parse the layers for a Net, which allows inspection of names and sizes.parseShape
(MatShapeVector vector) ExtractMat
dimensions from aMatShapeVector
.parseStrings
(StringVector vector) Extract Strings from aStringVector
.static Mat
readPatch
(ImageServer<BufferedImage> server, ROI roi, double downsample, int width, int height) Read an image patch, optionally with a fixed size and using zero-padding if required.static Mat
readPatch
(ImageServer<BufferedImage> server, ROI roi, double downsample, int width, int height, int borderPadding) Read an image patch, optionally with a fixed size.static <T extends DnnModel>
voidregisterDnnModel
(Class<T> subtype, String name) Deprecated.static Mat
segment
(SegmentationModel model, Mat input, Mat output) Apply a segmentation model to an image region.static Mat
segment
(SegmentationModel model, ImageServer<BufferedImage> server, RegionRequest request) Apply a segmentation model to an image region.static void
setUseCuda
(boolean requestUseCuda) Request that CUDA is used.static String
Create a (multiline) summary String for aNet
, given the specified image input dimensions.static boolean
useCuda()
Returns true if CUDA is available and requested.
-
Constructor Details
-
DnnTools
public DnnTools()
-
-
Method Details
-
registerDnnModel
Deprecated.since v0.4.0; useDnnModels.registerDnnModel(Class, String)
instead.Register a newDnnModel
class for JSON serialization/deserialization.- Type Parameters:
T
-- Parameters:
subtype
-name
-
-
builder
Initiative building and configuring anOpenCVDnn
.Note that
DnnModels.buildModel(DnnModelParams)
should generally be used instead to create an arbitraryDnnModel
, since it can potentially use different libraries or frameworks.- Parameters:
modelPath
-- Returns:
- See Also:
-
isCudaAvailable
public static boolean isCudaAvailable()Query whether CUDA is reported as available by OpenCV. If it is, it will be used by default untilsetUseCuda(boolean)
is used to turn if off.- Returns:
-
setUseCuda
public static void setUseCuda(boolean requestUseCuda) Request that CUDA is used. This will be ignored ifisCudaAvailable()
returns false, therefore the main purpose of this method is to disable the use of CUDA if it would otherwise be employed.- Parameters:
requestUseCuda
-
-
useCuda
public static boolean useCuda()Returns true if CUDA is available and requested. Classes that could potentially use CUDA should query this request before attempting to use it.- Returns:
- true if CUDA should be used, false otherwise
-
getOutputLayerNames
Get the names of all unconnected output layers.- Parameters:
net
-- Returns:
-
getOutputLayers
Get the names of all unconnected output layers.- Parameters:
net
-inputShape
-- Returns:
-
parseLayers
public static List<DnnTools.DNNLayer> parseLayers(Net net, int width, int height, int channels, int batchSize) Parse the layers for a Net, which allows inspection of names and sizes.- Parameters:
net
- the Net to parsewidth
- input widthheight
- input heightchannels
- input channelsbatchSize
- input batch size- Returns:
-
parseStrings
Extract Strings from aStringVector
.- Parameters:
vector
-- Returns:
-
parseShape
ExtractMat
dimensions from aMatShapeVector
.- Parameters:
vector
-- Returns:
-
summarize
Create a (multiline) summary String for aNet
, given the specified image input dimensions.- Parameters:
net
- the Net to summarizewidth
- input widthheight
- input heightnChannels
- input channel count- Returns:
- Throws:
IOException
- if an error occurs when loading the model
-
readPatch
public static Mat readPatch(ImageServer<BufferedImage> server, ROI roi, double downsample, int width, int height) throws IOException Read an image patch, optionally with a fixed size and using zero-padding if required. If the patch width and height are specified, these relate to the output (downsampled) image and are centered on the ROI centroid. Otherwise the ROI bounds are used.- Parameters:
server
- the image serverroi
- the ROI for which the patch should be extracteddownsample
- the downsample valuewidth
- the patch width, or -1 if the ROI bounds should be usedheight
- the patch height, or -1 if the ROI bounds should be used- Returns:
- Throws:
IOException
- See Also:
-
readPatch
public static Mat readPatch(ImageServer<BufferedImage> server, ROI roi, double downsample, int width, int height, int borderPadding) throws IOException Read an image patch, optionally with a fixed size. If the patch width and height are specified, these relate to the output (downsampled) image and are centered on the ROI centroid. Otherwise the ROI bounds are used.- Parameters:
server
- the image serverroi
- the ROI for which the patch should be extracteddownsample
- the downsample valuewidth
- the patch width, or -1 if the ROI bounds should be usedheight
- the patch height, or -1 if the ROI bounds should be usedborderPadding
- the border padding for out-of-bounds requests, for use with OpenCV's copyMakeBorder- Returns:
- Throws:
IOException
- See Also:
-
classify
public static boolean classify(ClassificationModel model, PathObject pathObject, ImageServer<BufferedImage> server, double downsample, IntFunction<PathClass> classifier, String predictionMeasurement) throws IOException Apply a classification model to an image patch to classify an object.- Parameters:
model
- the model for predictionpathObject
- the object to classifyserver
- the image supplying the patchdownsample
- the requested downsample at which classification should be appliedclassifier
- function to convert the classification label into aPathClass
predictionMeasurement
- optional measurement name for storing the prediction value (often treated as a probability) in the measurement list of the object.- Returns:
- true if the classification of the object has changed, false otherwise
- Throws:
IOException
- if the patch cannot be read
-
classify
public static boolean classify(ClassificationModel model, PathObject pathObject, ImageServer<BufferedImage> server, double downsample, int width, int height, IntFunction<PathClass> classifier, String predictionMeasurement) throws IOException, IllegalArgumentException Apply a classification model to an image patch to classify an object. If a patch width and height both > 0, the patch is cropped around the ROI centroid and padded if necessary. If a patch width and height both < 0, the bounding box of hte ROI is used directly and the model is assumed able to resize if needed. If the patch width and height are anything else, anIllegalArgumentException
is thrown.- Parameters:
model
- the model for predictionpathObject
- the object to classifyserver
- the image supplying the patchdownsample
- the requested downsample at which classification should be appliedwidth
- the fixed input sizeheight
- the fixed input sizeclassifier
- function to convert the classification label into aPathClass
predictionMeasurement
- optional measurement name for storing the prediction value (often treated as a probability) in the measurement list of the object.- Returns:
- true if the classification of the object has changed, false otherwise
- Throws:
IOException
- if the patch cannot be readIllegalArgumentException
- if the patch width or height are invalid
-
classify
public static boolean classify(ClassificationModel model, PathObject pathObject, Mat input, IntFunction<PathClass> classifier, String predictionMeasurement) Apply a classification model to an existing image patch to classify an object.- Parameters:
model
- the model for predictionpathObject
- the object to classifyinput
- image patchclassifier
- function to convert the classification label into aPathClass
predictionMeasurement
- optional measurement name for storing the prediction value (often treated as a probability) in the measurement list of the object.- Returns:
- true if the classification of the object has changed, false otherwise
-
segment
public static Mat segment(SegmentationModel model, ImageServer<BufferedImage> server, RegionRequest request) throws IOException Apply a segmentation model to an image region.- Parameters:
model
- the segmentation modelserver
- the imagerequest
- the region- Returns:
- a
Mat
containing the segmentation results - Throws:
IOException
- if the input image could not be read
-
segment
Apply a segmentation model to an image region.- Parameters:
model
- the segmentation modelinput
- the input imageoutput
- the output image- Returns:
- a
Mat
containing the segmentation results (the same as output, if provided)
-
detect
public static List<PathObject> detect(DetectionModel model, ImageServer<BufferedImage> server, RegionRequest request, IntFunction<PathClass> classifier, Function<ROI, PathObject> creator) throws IOExceptionApply a detection model to generate rectangles surrounding distinct structures.- Parameters:
model
- the detection modelserver
- the imagerequest
- the region within which detection should be appliedclassifier
- function to convert the classification label into aPathClass
creator
- function to create an object (e.g. detection, annotation) from a ROI- Returns:
- a list of created objects
- Throws:
IOException
- if the image could not be read
-
detect
public static List<PathObject> detect(DetectionModel model, Mat mat, RegionRequest request, IntFunction<PathClass> classifier, Function<ROI, PathObject> creator) Apply a detection model to generate rectangles surrounding distinct structures.- Parameters:
model
- the detection modelmat
- the imagerequest
- the region corresponding to the Mat; if provided, this is used to scale and translate detected regionsclassifier
- function to convert the classification label into aPathClass
creator
- function to create an object (e.g. detection, annotation) from a ROI- Returns:
- a list of created objects
-
blobFromImages
Create an OpenCV blob from one or more mats.- Parameters:
mats
-- Returns:
-
blobFromImage
Create an OpenCV blob from a single mat.- Parameters:
mat
-- Returns:
-
blobFromImages
public static Mat blobFromImages(Mat mat, double scaleFactor, Size size, Scalar mean, boolean swapRB, boolean crop) Create an OpenCV blob from a Mat with optional scaling, resizing and cropping.- Parameters:
mat
- input imagescaleFactor
- scale factorsize
- input width and heightmean
- mean values for subtractionswapRB
- swap red and blue of the mean valuescrop
- center crop after resizing if needed- Returns:
- a blob with axis order NCHW
-
blobFromImages
public static Mat blobFromImages(Collection<Mat> mats, double scaleFactor, Size size, Scalar mean, boolean swapRB, boolean crop) Create an OpenCV blob from a batch of Mats with optional scaling, resizing and cropping.- Parameters:
mats
- input imagesscaleFactor
- scale factorsize
- input width and heightmean
- mean values for subtractionswapRB
- swap red and blue of the mean valuescrop
- center crop after resizing if needed- Returns:
- a blob with axis order NCHW
-
imagesFromBlob
Extract images from an OpenCV blob.- Parameters:
blob
-- Returns:
- a list of of images, with length depending upon batch size
-
DnnModels.registerDnnModel(Class, String)
instead.