Search in sources :

Example 6 with Scalar

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

the class OpenCVTools method fillSmallHoles.

/**
 * Fill holes in a binary image (1-channel, 8-bit unsigned) with an area <= maxArea.
 *
 * @param matBinary
 * @param maxArea
 */
public static void fillSmallHoles(Mat matBinary, double maxArea) {
    Mat matHoles = new Mat();
    invertBinary(matBinary, matHoles);
    MatVector contours = new MatVector();
    Mat hierarchy = new Mat();
    opencv_imgproc.findContours(matHoles, contours, hierarchy, opencv_imgproc.RETR_CCOMP, opencv_imgproc.CHAIN_APPROX_SIMPLE);
    Scalar color = Scalar.WHITE;
    int ind = 0;
    Point offset = new Point(0, 0);
    Indexer indexerHierarchy = hierarchy.createIndexer();
    for (int c = 0; c < contours.size(); c++) {
        Mat contour = contours.get(c);
        // TODO: Check hierarchy indexing after switch to JavaCPP!!
        if (indexerHierarchy.getDouble(0, ind, 3) >= 0 || opencv_imgproc.contourArea(contour) > maxArea) {
            ind++;
            continue;
        }
        opencv_imgproc.drawContours(matBinary, contours, c, color, -1, opencv_imgproc.LINE_8, null, Integer.MAX_VALUE, offset);
        ind++;
    }
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) ByteIndexer(org.bytedeco.javacpp.indexer.ByteIndexer) IntIndexer(org.bytedeco.javacpp.indexer.IntIndexer) ShortIndexer(org.bytedeco.javacpp.indexer.ShortIndexer) DoubleIndexer(org.bytedeco.javacpp.indexer.DoubleIndexer) UByteIndexer(org.bytedeco.javacpp.indexer.UByteIndexer) UShortIndexer(org.bytedeco.javacpp.indexer.UShortIndexer) FloatIndexer(org.bytedeco.javacpp.indexer.FloatIndexer) Indexer(org.bytedeco.javacpp.indexer.Indexer) MatVector(org.bytedeco.opencv.opencv_core.MatVector) Point(org.bytedeco.opencv.opencv_core.Point) Point(org.bytedeco.opencv.opencv_core.Point) Scalar(org.bytedeco.opencv.opencv_core.Scalar)

Aggregations

Scalar (org.bytedeco.opencv.opencv_core.Scalar)6 Mat (org.bytedeco.opencv.opencv_core.Mat)5 Size (org.bytedeco.opencv.opencv_core.Size)3 Point (org.bytedeco.opencv.opencv_core.Point)2 GsonBuilder (com.google.gson.GsonBuilder)1 PointerScope (org.bytedeco.javacpp.PointerScope)1 ByteIndexer (org.bytedeco.javacpp.indexer.ByteIndexer)1 DoubleIndexer (org.bytedeco.javacpp.indexer.DoubleIndexer)1 FloatIndexer (org.bytedeco.javacpp.indexer.FloatIndexer)1 Indexer (org.bytedeco.javacpp.indexer.Indexer)1 IntIndexer (org.bytedeco.javacpp.indexer.IntIndexer)1 ShortIndexer (org.bytedeco.javacpp.indexer.ShortIndexer)1 UByteIndexer (org.bytedeco.javacpp.indexer.UByteIndexer)1 UShortIndexer (org.bytedeco.javacpp.indexer.UShortIndexer)1 AbstractScalar (org.bytedeco.opencv.opencv_core.AbstractScalar)1 MatVector (org.bytedeco.opencv.opencv_core.MatVector)1 Point2f (org.bytedeco.opencv.opencv_core.Point2f)1 Test (org.junit.jupiter.api.Test)1