Search in sources :

Example 41 with Mat

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

the class TestImageOps method testNormalize.

@Test
public void testNormalize() {
    try (var scope = new PointerScope()) {
        opencv_core.setRNGSeed(100);
        int type = opencv_core.CV_32FC1;
        int rows = 25;
        int cols = 30;
        var matList = Arrays.asList(new Mat(rows, cols, type, Scalar.all(1)), new Mat(rows, cols, type, Scalar.all(5)), new Mat(rows, cols, type, Scalar.all(2)), new Mat(rows, cols, type, Scalar.all(10)));
        var mat = OpenCVTools.mergeChannels(matList, null);
        // Add noise
        OpenCVTools.addNoise(mat, 10, 5);
        double eps = 1e-3;
        // Check mean/variance normalization
        var matZeroUnitGlobal = ImageOps.Normalize.zeroMeanUnitVariance(false).apply(mat.clone());
        var values = OpenCVTools.extractDoubles(matZeroUnitGlobal);
        var stats = new DescriptiveStatistics(values);
        assertEquals(stats.getMean(), 0.0, eps);
        assertEquals(stats.getStandardDeviation(), 1.0, eps);
        // When normalizing globally, don't expert zero mean and unit variance per channel
        for (var matTemp : OpenCVTools.splitChannels(matZeroUnitGlobal)) {
            values = OpenCVTools.extractDoubles(matTemp);
            stats = new DescriptiveStatistics(values);
            assertNotEquals(stats.getMean(), 0.0, eps);
            assertNotEquals(stats.getStandardDeviation(), 1.0, eps);
        }
        // When normalizing per-channel, expect to find zero mean & unit variance across the image as well
        var matZeroUnitChannels = ImageOps.Normalize.zeroMeanUnitVariance(true).apply(mat.clone());
        values = OpenCVTools.extractDoubles(matZeroUnitChannels);
        stats = new DescriptiveStatistics(values);
        assertEquals(stats.getMean(), 0.0, eps);
        assertEquals(stats.getStandardDeviation(), 1.0, eps);
        for (var matTemp : OpenCVTools.splitChannels(matZeroUnitChannels)) {
            values = OpenCVTools.extractDoubles(matTemp);
            stats = new DescriptiveStatistics(values);
            assertEquals(stats.getMean(), 0.0, eps);
            assertEquals(stats.getStandardDeviation(), 1.0, eps);
        }
    }
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) PointerScope(org.bytedeco.javacpp.PointerScope) Test(org.junit.jupiter.api.Test)

Example 42 with Mat

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

the class TestImageOps method testChannels.

@Test
public void testChannels() {
    try (var scope = new PointerScope()) {
        int type = opencv_core.CV_32FC1;
        int rows = 25;
        int cols = 30;
        var matList = Arrays.asList(new Mat(rows, cols, type, Scalar.all(1)), new Mat(rows, cols, type, Scalar.all(5)), new Mat(rows, cols, type, Scalar.all(2)), new Mat(rows, cols, type, Scalar.all(4)), new Mat(rows, cols, type, Scalar.all(3)));
        var mat = OpenCVTools.mergeChannels(matList, null);
        var matMean = ImageOps.Channels.mean().apply(mat.clone());
        assertTrue(matsEqual(matMean, new Mat(rows, cols, type, Scalar.all(3)), 1e-6));
        var matSum = ImageOps.Channels.sum().apply(mat.clone());
        assertTrue(matsEqual(matSum, new Mat(rows, cols, type, Scalar.all(15)), 1e-6));
        var matMax = ImageOps.Channels.maximum().apply(mat.clone());
        assertTrue(matsEqual(matMax, new Mat(rows, cols, type, Scalar.all(5)), 1e-6));
        var matMin = ImageOps.Channels.minimum().apply(mat.clone());
        assertTrue(matsEqual(matMin, new Mat(rows, cols, type, Scalar.all(1)), 1e-6));
        var matChannels = ImageOps.Channels.extract(1).apply(mat.clone());
        assertEquals(matChannels.channels(), 1);
        assertTrue(matsEqual(matChannels, matList.get(1), 1e-6));
        var matChannels2 = ImageOps.Channels.extract(2, 4).apply(mat.clone());
        assertEquals(matChannels2.channels(), 2);
        assertThrows(IllegalArgumentException.class, () -> ImageOps.Channels.extract().apply(mat.clone()));
    }
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) PointerScope(org.bytedeco.javacpp.PointerScope) Test(org.junit.jupiter.api.Test)

Example 43 with Mat

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

the class TestImageOps method matsEqual.

/**
 * Compare if two Mats are equal in terms of dimensions and values.
 * @param m1
 * @param m2
 * @param tol
 * @return
 */
private static boolean matsEqual(Mat m1, Mat m2, double tol) {
    if (m1.rows() != m2.rows() || m1.cols() != m2.cols() || m1.channels() != m2.channels())
        return false;
    Mat matDiff = new Mat();
    opencv_core.absdiff(m1, m2, matDiff);
    for (var channel : OpenCVTools.splitChannels(matDiff)) {
        if (opencv_core.countNonZero(opencv_core.greaterThan(channel, tol).asMat()) != 0)
            return false;
    }
    return true;
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat)

Example 44 with Mat

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

the class TestMultiscaleFeatures method getAllValues.

private static double[] getAllValues(Mat mat) {
    var mat2 = new Mat();
    mat.convertTo(mat2, opencv_core.CV_64F);
    DoubleBuffer buffer = mat2.createBuffer();
    var values = new double[buffer.limit()];
    buffer.get(values);
    mat2.close();
    return values;
}
Also used : DoubleBuffer(java.nio.DoubleBuffer) Mat(org.bytedeco.opencv.opencv_core.Mat)

Example 45 with Mat

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

the class TestOpenCVTools method testScalar.

@Test
public void testScalar() {
    double[] values = new double[] { -100, 123.4, 0, -0, 12023.423 };
    try (var scope = new PointerScope()) {
        // Test floats
        for (int type : new int[] { opencv_core.CV_32F, opencv_core.CV_64F }) {
            for (double value : values) {
                Mat mat = OpenCVTools.scalarMat(value, type);
                assertEquals(1, mat.rows());
                assertEquals(1, mat.cols());
                assertEquals(1, mat.channels());
                assertEquals(value, mat.createIndexer().getDouble(0L), 1e-3);
            }
        }
        // Test floats with channels
        for (int c : new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }) {
            for (double value : values) {
                Mat mat = OpenCVTools.scalarMatWithType(value, opencv_core.CV_32FC(c));
                assertEquals(1, mat.rows());
                assertEquals(1, mat.cols());
                assertEquals(c, mat.channels());
                double[] doubles = new double[c];
                Arrays.fill(doubles, value);
                assertArrayEquals(doubles, OpenCVTools.extractDoubles(mat), 1e-3);
            }
        }
        // Test unsigned integers
        for (int type : new int[] { opencv_core.CV_8U }) {
            for (double value : values) {
                Mat mat = OpenCVTools.scalarMat(value, type);
                assertEquals(1, mat.rows());
                assertEquals(1, mat.cols());
                assertEquals(1, mat.channels());
                double val = Math.min(255, Math.max(0, Math.round(value)));
                assertEquals(val, mat.createIndexer().getDouble(0L));
            }
        }
        // Test signed integers
        for (int type : new int[] { opencv_core.CV_32S }) {
            for (double value : values) {
                Mat mat = OpenCVTools.scalarMat(value, type);
                assertEquals(1, mat.rows());
                assertEquals(1, mat.cols());
                assertEquals(1, mat.channels());
                double val = Math.round(value);
                assertEquals(val, mat.createIndexer().getDouble(0L));
            }
        }
    }
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) PointerScope(org.bytedeco.javacpp.PointerScope) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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