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
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic classHelper class to summarize a DNN layer.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic MatblobFromImage(Mat mat) Create an OpenCV blob from a single mat.static MatblobFromImages(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 MatblobFromImages(Mat... mats) Create an OpenCV blob from one or more mats.static MatblobFromImages(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.BuilderInitiative building and configuring anOpenCVDnn.static booleanclassify(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 booleanclassify(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 booleanclassify(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 booleanQuery 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) ExtractMatdimensions from aMatShapeVector.parseStrings(StringVector vector) Extract Strings from aStringVector.static MatreadPatch(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 MatreadPatch(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 Matsegment(SegmentationModel model, Mat input, Mat output) Apply a segmentation model to an image region.static Matsegment(SegmentationModel model, ImageServer<BufferedImage> server, RegionRequest request) Apply a segmentation model to an image region.static voidsetUseCuda(boolean requestUseCuda) Request that CUDA is used.static StringCreate a (multiline) summary String for aNet, given the specified image input dimensions.static booleanuseCuda()Returns true if CUDA is available and requested.
- 
Constructor Details- 
DnnToolspublic DnnTools()
 
- 
- 
Method Details- 
registerDnnModelDeprecated.since v0.4.0; useDnnModels.registerDnnModel(Class, String)instead.Register a newDnnModelclass for JSON serialization/deserialization.- Type Parameters:
- T-
- Parameters:
- subtype-
- name-
 
- 
builderInitiative 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.
- 
isCudaAvailablepublic 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:
 
- 
setUseCudapublic 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-
 
- 
useCudapublic 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
 
- 
getOutputLayerNamesGet the names of all unconnected output layers.- Parameters:
- net-
- Returns:
 
- 
getOutputLayersGet the names of all unconnected output layers.- Parameters:
- net-
- inputShape-
- Returns:
 
- 
parseLayerspublic 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 parse
- width- input width
- height- input height
- channels- input channels
- batchSize- input batch size
- Returns:
 
- 
parseStringsExtract Strings from aStringVector.- Parameters:
- vector-
- Returns:
 
- 
parseShapeExtractMatdimensions from aMatShapeVector.- Parameters:
- vector-
- Returns:
 
- 
summarizeCreate a (multiline) summary String for aNet, given the specified image input dimensions.- Parameters:
- net- the Net to summarize
- width- input width
- height- input height
- nChannels- input channel count
- Returns:
- Throws:
- IOException- if an error occurs when loading the model
 
- 
readPatchpublic 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 server
- roi- the ROI for which the patch should be extracted
- downsample- the downsample value
- width- the patch width, or -1 if the ROI bounds should be used
- height- the patch height, or -1 if the ROI bounds should be used
- Returns:
- Throws:
- IOException
- See Also:
 
- 
readPatchpublic 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 server
- roi- the ROI for which the patch should be extracted
- downsample- the downsample value
- width- the patch width, or -1 if the ROI bounds should be used
- height- the patch height, or -1 if the ROI bounds should be used
- borderPadding- the border padding for out-of-bounds requests, for use with OpenCV's copyMakeBorder
- Returns:
- Throws:
- IOException
- See Also:
 
- 
classifypublic 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 prediction
- pathObject- the object to classify
- server- the image supplying the patch
- downsample- the requested downsample at which classification should be applied
- classifier- function to convert the classification label into a- PathClass
- 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
 
- 
classifypublic 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, anIllegalArgumentExceptionis thrown.- Parameters:
- model- the model for prediction
- pathObject- the object to classify
- server- the image supplying the patch
- downsample- the requested downsample at which classification should be applied
- width- the fixed input size
- height- the fixed input size
- classifier- function to convert the classification label into a- PathClass
- 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
- IllegalArgumentException- if the patch width or height are invalid
 
- 
classifypublic 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 prediction
- pathObject- the object to classify
- input- image patch
- classifier- function to convert the classification label into a- PathClass
- 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
 
- 
segmentpublic static Mat segment(SegmentationModel model, ImageServer<BufferedImage> server, RegionRequest request) throws IOException Apply a segmentation model to an image region.- Parameters:
- model- the segmentation model
- server- the image
- request- the region
- Returns:
- a Matcontaining the segmentation results
- Throws:
- IOException- if the input image could not be read
 
- 
segmentApply a segmentation model to an image region.- Parameters:
- model- the segmentation model
- input- the input image
- output- the output image
- Returns:
- a Matcontaining the segmentation results (the same as output, if provided)
 
- 
detectpublic 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 model
- server- the image
- request- the region within which detection should be applied
- classifier- function to convert the classification label into a- PathClass
- 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
 
- 
detectpublic 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 model
- mat- the image
- request- the region corresponding to the Mat; if provided, this is used to scale and translate detected regions
- classifier- function to convert the classification label into a- PathClass
- creator- function to create an object (e.g. detection, annotation) from a ROI
- Returns:
- a list of created objects
 
- 
blobFromImagesCreate an OpenCV blob from one or more mats.- Parameters:
- mats-
- Returns:
 
- 
blobFromImageCreate an OpenCV blob from a single mat.- Parameters:
- mat-
- Returns:
 
- 
blobFromImagespublic 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 image
- scaleFactor- scale factor
- size- input width and height
- mean- mean values for subtraction
- swapRB- swap red and blue of the mean values
- crop- center crop after resizing if needed
- Returns:
- a blob with axis order NCHW
 
- 
blobFromImagespublic 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 images
- scaleFactor- scale factor
- size- input width and height
- mean- mean values for subtraction
- swapRB- swap red and blue of the mean values
- crop- center crop after resizing if needed
- Returns:
- a blob with axis order NCHW
 
- 
imagesFromBlobExtract images from an OpenCV blob.- Parameters:
- blob-
- Returns:
- a list of of images, with length depending upon batch size
 
 
- 
DnnModels.registerDnnModel(Class, String)instead.