Search in sources :

Example 61 with Mat

use of org.bytedeco.opencv.opencv_core.Mat in project karate by karatelabs.

the class OpenCvUtils method rescale.

public static Mat rescale(Mat mat, double scale) {
    Mat resized = new Mat();
    resize(mat, resized, new Size(), scale, scale, CV_INTER_AREA);
    return resized;
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) Size(org.bytedeco.opencv.opencv_core.Size)

Example 62 with Mat

use of org.bytedeco.opencv.opencv_core.Mat in project karate by karatelabs.

the class Tesseract method process.

public void process(Region region, boolean negative) {
    BufferedImage bi = region.captureGreyScale();
    if (region.robot.highlight) {
        region.highlight(region.robot.highlightDuration);
    }
    Mat mat = OpenCvUtils.toMat(bi);
    process(mat, negative);
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) BufferedImage(java.awt.image.BufferedImage)

Example 63 with Mat

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

the class OpenCVTools method getGaussianDerivKernel.

/**
 * Get filter coefficients for a 1D Gaussian (derivative) kernel.
 *
 * @param sigma Gaussian sigma
 * @param order order of the derivative: 0, ('standard' Gaussian filter), 1 (first derivative) or 2 (second derivative)
 * @param doColumn if true, return coefficients as a column vector rather than a row vector (default)
 * @return
 */
public static Mat getGaussianDerivKernel(double sigma, int order, boolean doColumn) {
    int n = (int) (sigma * 4);
    int len = n * 2 + 1;
    double[] kernel = getGaussianDeriv(sigma, order, len);
    Mat mat;
    if (doColumn)
        mat = new Mat(1, kernel.length, opencv_core.CV_64F);
    else
        mat = new Mat(kernel.length, 1, opencv_core.CV_64F);
    DoubleIndexer indexer = mat.createIndexer();
    indexer.put(0L, kernel);
    indexer.release();
    return mat;
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) DoubleIndexer(org.bytedeco.javacpp.indexer.DoubleIndexer) Point(org.bytedeco.opencv.opencv_core.Point)

Example 64 with Mat

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

the class OpenCVTools method splitChannels.

/**
 * Split channels from a {@link Mat}.
 * May be more convenient than OpenCV's built-in approach.
 *
 * @param mat
 * @return a list of {@link Mat}, containing each split channel in order
 */
public static List<Mat> splitChannels(Mat mat) {
    var list = new ArrayList<Mat>();
    var channels = mat.channels();
    for (int c = 0; c < channels; c++) {
        var temp = new Mat();
        opencv_core.extractChannel(mat, temp, c);
        list.add(temp);
    }
    return list;
// This code appeared to give the occasional crazy result (perhaps followed by a segfault?)
// var matvec = new MatVector();
// opencv_core.split(mat, matvec);
// return Arrays.asList(matvec.get());
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) ArrayList(java.util.ArrayList) Point(org.bytedeco.opencv.opencv_core.Point)

Example 65 with Mat

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

the class OpenCVTools method labelsToPoints.

private static List<IndexedPixel> labelsToPoints(Mat mat) {
    if (mat.channels() != 1)
        throw new IllegalArgumentException("labelsToPoints requires a single-channel mat, but input has " + mat.channels() + " channels");
    int w = mat.cols();
    int h = mat.rows();
    var pixels = new ArrayList<IndexedPixel>();
    try (IntIndexer idx = mat.createIndexer()) {
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                int lab = idx.get(y, x);
                if (lab != 0) {
                    pixels.add(new IndexedPixel(x, y, lab));
                }
            }
        }
    }
    // Group by label
    var groups = pixels.stream().collect(Collectors.groupingBy(p -> p.value));
    return groups.values().stream().map(l -> closestToCentroid(l)).collect(Collectors.toList());
}
Also used : Arrays(java.util.Arrays) ImageServer(qupath.lib.images.servers.ImageServer) ByteProcessor(ij.process.ByteProcessor) BiFunction(java.util.function.BiFunction) ImageProcessor(ij.process.ImageProcessor) LoggerFactory(org.slf4j.LoggerFactory) ByteIndexer(org.bytedeco.javacpp.indexer.ByteIndexer) IntIndexer(org.bytedeco.javacpp.indexer.IntIndexer) Map(java.util.Map) ShortIndexer(org.bytedeco.javacpp.indexer.ShortIndexer) IndexColorModel(java.awt.image.IndexColorModel) DoubleIndexer(org.bytedeco.javacpp.indexer.DoubleIndexer) ColorTools(qupath.lib.common.ColorTools) BufferedImage(java.awt.image.BufferedImage) Collection(java.util.Collection) IntPointer(org.bytedeco.javacpp.IntPointer) Collectors(java.util.stream.Collectors) PathObject(qupath.lib.objects.PathObject) ImagePlus(ij.ImagePlus) List(java.util.List) UByteIndexer(org.bytedeco.javacpp.indexer.UByteIndexer) ShortProcessor(ij.process.ShortProcessor) ContourTracing(qupath.lib.analysis.images.ContourTracing) DataBuffer(java.awt.image.DataBuffer) CompositeImage(ij.CompositeImage) Point(org.bytedeco.opencv.opencv_core.Point) org.bytedeco.opencv.global.opencv_core(org.bytedeco.opencv.global.opencv_core) UShortIndexer(org.bytedeco.javacpp.indexer.UShortIndexer) HashMap(java.util.HashMap) MatVector(org.bytedeco.opencv.opencv_core.MatVector) Function(java.util.function.Function) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) ArrayList(java.util.ArrayList) Rect(org.bytedeco.opencv.opencv_core.Rect) BandedSampleModel(java.awt.image.BandedSampleModel) Mat(org.bytedeco.opencv.opencv_core.Mat) Scalar(org.bytedeco.opencv.opencv_core.Scalar) ColorModelFactory(qupath.lib.color.ColorModelFactory) Logger(org.slf4j.Logger) PointerScope(org.bytedeco.javacpp.PointerScope) GeneralTools(qupath.lib.common.GeneralTools) RegionRequest(qupath.lib.regions.RegionRequest) SimpleImage(qupath.lib.analysis.images.SimpleImage) PixelType(qupath.lib.images.servers.PixelType) IOException(java.io.IOException) org.bytedeco.opencv.global.opencv_imgproc(org.bytedeco.opencv.global.opencv_imgproc) Consumer(java.util.function.Consumer) Index(org.bytedeco.javacpp.indexer.Index) ROI(qupath.lib.roi.interfaces.ROI) FloatProcessor(ij.process.FloatProcessor) Percentile(org.apache.commons.math3.stat.descriptive.rank.Percentile) Size(org.bytedeco.opencv.opencv_core.Size) ColorModel(java.awt.image.ColorModel) FloatIndexer(org.bytedeco.javacpp.indexer.FloatIndexer) Pointer(org.bytedeco.javacpp.Pointer) Indexer(org.bytedeco.javacpp.indexer.Indexer) ImageStack(ij.ImageStack) WritableRaster(java.awt.image.WritableRaster) DoublePredicate(java.util.function.DoublePredicate) Comparator(java.util.Comparator) SimpleImages(qupath.lib.analysis.images.SimpleImages) Collections(java.util.Collections) ArrayList(java.util.ArrayList) Point(org.bytedeco.opencv.opencv_core.Point) IntIndexer(org.bytedeco.javacpp.indexer.IntIndexer)

Aggregations

Mat (org.bytedeco.opencv.opencv_core.Mat)101 Point (org.bytedeco.opencv.opencv_core.Point)23 MatVector (org.bytedeco.opencv.opencv_core.MatVector)20 ArrayList (java.util.ArrayList)16 PointerScope (org.bytedeco.javacpp.PointerScope)16 Size (org.bytedeco.opencv.opencv_core.Size)15 BufferedImage (java.awt.image.BufferedImage)13 FloatIndexer (org.bytedeco.javacpp.indexer.FloatIndexer)13 Scalar (org.bytedeco.opencv.opencv_core.Scalar)13 Test (org.junit.jupiter.api.Test)12 IntIndexer (org.bytedeco.javacpp.indexer.IntIndexer)10 DoubleIndexer (org.bytedeco.javacpp.indexer.DoubleIndexer)9 UByteIndexer (org.bytedeco.javacpp.indexer.UByteIndexer)8 org.bytedeco.opencv.global.opencv_core (org.bytedeco.opencv.global.opencv_core)7 IOException (java.io.IOException)6 Arrays (java.util.Arrays)6 Collections (java.util.Collections)6 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 UShortIndexer (org.bytedeco.javacpp.indexer.UShortIndexer)6