Search in sources :

Example 11 with MatVector

use of org.bytedeco.opencv.opencv_core.MatVector in project qupath by qupath.

the class OpenCVTools method mergeChannels.

/**
 * Merge channels from a multichannel {@link Mat}.
 * May be more convenient than OpenCV's built-in approach.
 *
 * @param channels separate channels
 * @param dest optional destination (may be null)
 * @return merged {@link Mat}, which will be the same as dest if provided
 */
@SuppressWarnings("unchecked")
public static Mat mergeChannels(Collection<? extends Mat> channels, Mat dest) {
    if (dest == null)
        dest = new Mat();
    boolean firstMat = true;
    boolean allSingleChannel = true;
    int rows = -1;
    int cols = -1;
    int depth = 0;
    int nChannels = 0;
    for (var m : channels) {
        if (firstMat) {
            rows = m.rows();
            cols = m.cols();
            depth = m.depth();
            if (rows < 0 || cols < 0)
                throw new IllegalArgumentException("Rows and columns must be >= 0");
            firstMat = false;
        }
        int nc = m.channels();
        allSingleChannel = allSingleChannel && nc == 1;
        nChannels += nc;
        if (depth != m.depth() || rows != m.rows() || cols != m.cols())
            throw new IllegalArgumentException("mergeChannels() requires all Mats to have the same dimensions and depth!");
    }
    // We can only use OpenCV's merge if all Mats are single-channel
    boolean mixChannels = !allSingleChannel;
    try (var scope = new PointerScope()) {
        if (mixChannels) {
            dest.create(rows, cols, opencv_core.CV_MAKETYPE(depth, nChannels));
            var vecSource = new MatVector(channels.toArray(Mat[]::new));
            var vecDest = new MatVector(dest);
            int[] inds = new int[nChannels * 2];
            for (int i = 0; i < nChannels; i++) {
                inds[i * 2] = i;
                inds[i * 2 + 1] = i;
            }
            opencv_core.mixChannels(vecSource, vecDest, new IntPointer(inds));
        } else {
            opencv_core.merge(new MatVector(channels.toArray(Mat[]::new)), dest);
        }
    // scope.deallocate();
    }
    return dest;
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) IntPointer(org.bytedeco.javacpp.IntPointer) MatVector(org.bytedeco.opencv.opencv_core.MatVector) PointerScope(org.bytedeco.javacpp.PointerScope) Point(org.bytedeco.opencv.opencv_core.Point)

Example 12 with MatVector

use of org.bytedeco.opencv.opencv_core.MatVector in project qupath by qupath.

the class ImageOps method doPrediction.

private static <T> Mat doPrediction(DnnModel<T> model, Mat mat, String inputName, String... outputNames) {
    var matResult = new Mat();
    try (@SuppressWarnings("unchecked") var scope = new PointerScope()) {
        var output = model.convertAndPredict(Map.of(inputName, mat));
        if (!output.isEmpty()) {
            if (outputNames.length == 0 || (outputNames.length == 1 && output.containsKey(outputNames[0])))
                matResult.put(output.values().iterator().next());
            else {
                var tempArray = new Mat[outputNames.length];
                for (int i = 0; i < outputNames.length; i++) {
                    var name = outputNames[i];
                    if (output.containsKey(name)) {
                        tempArray[i] = output.get(name);
                    } else
                        throw new RuntimeException(String.format("Unable to find output '%s' in %s", name, model));
                }
                opencv_core.merge(new MatVector(tempArray), matResult);
            }
        }
        scope.deallocate();
    }
    return matResult;
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) MatVector(org.bytedeco.opencv.opencv_core.MatVector) PointerScope(org.bytedeco.javacpp.PointerScope)

Aggregations

Mat (org.bytedeco.opencv.opencv_core.Mat)12 MatVector (org.bytedeco.opencv.opencv_core.MatVector)12 Point (org.bytedeco.opencv.opencv_core.Point)8 ArrayList (java.util.ArrayList)4 PointerScope (org.bytedeco.javacpp.PointerScope)3 DoubleIndexer (org.bytedeco.javacpp.indexer.DoubleIndexer)3 FloatIndexer (org.bytedeco.javacpp.indexer.FloatIndexer)3 IntIndexer (org.bytedeco.javacpp.indexer.IntIndexer)3 BufferedImage (java.awt.image.BufferedImage)2 ByteIndexer (org.bytedeco.javacpp.indexer.ByteIndexer)2 Indexer (org.bytedeco.javacpp.indexer.Indexer)2 ShortIndexer (org.bytedeco.javacpp.indexer.ShortIndexer)2 UByteIndexer (org.bytedeco.javacpp.indexer.UByteIndexer)2 UShortIndexer (org.bytedeco.javacpp.indexer.UShortIndexer)2 CompositeImage (ij.CompositeImage)1 ImagePlus (ij.ImagePlus)1 ImageStack (ij.ImageStack)1 ImageProcessor (ij.process.ImageProcessor)1 Graphics2D (java.awt.Graphics2D)1 Area (java.awt.geom.Area)1