Search in sources :

Example 56 with Mat

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

the class Similarity method getMSSIM.

private static Scalar getMSSIM(Mat i1, Mat i2) {
    double C1 = 6.5025, C2 = 58.5225;
    /**
     *************************** INITS *********************************
     */
    int d = opencv_core.CV_32F;
    Mat I1 = new Mat();
    Mat I2 = new Mat();
    // cannot calculate on one byte large values
    i1.convertTo(I1, d);
    i2.convertTo(I2, d);
    // I2^2
    Mat I2_2 = I2.mul(I2).asMat();
    // I1^2
    Mat I1_2 = I1.mul(I1).asMat();
    // I1 * I2
    Mat I1_I2 = I1.mul(I2).asMat();
    /**
     ************************* END INITS *********************************
     */
    // PRELIMINARY COMPUTING
    Mat mu1 = new Mat();
    Mat mu2 = new Mat();
    opencv_imgproc.GaussianBlur(I1, mu1, new Size(11, 11), 1.5);
    opencv_imgproc.GaussianBlur(I2, mu2, new Size(11, 11), 1.5);
    Mat mu1_2 = mu1.mul(mu1).asMat();
    Mat mu2_2 = mu2.mul(mu2).asMat();
    Mat mu1_mu2 = mu1.mul(mu2).asMat();
    Mat sigma1_2 = new Mat();
    Mat sigma2_2 = new Mat();
    Mat sigma12 = new Mat();
    opencv_imgproc.GaussianBlur(I1_2, sigma1_2, new Size(11, 11), 1.5);
    sigma1_2 = opencv_core.subtract(sigma1_2, mu1_2).asMat();
    opencv_imgproc.GaussianBlur(I2_2, sigma2_2, new Size(11, 11), 1.5);
    sigma2_2 = opencv_core.subtract(sigma2_2, mu2_2).asMat();
    opencv_imgproc.GaussianBlur(I1_I2, sigma12, new Size(11, 11), 1.5);
    sigma12 = opencv_core.subtract(sigma12, mu1_mu2).asMat();
    Mat t1, t2, t3;
    t1 = opencv_core.add(opencv_core.multiply(2, mu1_mu2), Scalar.all(C1)).asMat();
    t2 = opencv_core.add(opencv_core.multiply(2, sigma12), Scalar.all(C2)).asMat();
    // t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
    t3 = t1.mul(t2).asMat();
    t1 = opencv_core.add(opencv_core.add(mu1_2, mu2_2), Scalar.all(C1)).asMat();
    t2 = opencv_core.add(opencv_core.add(sigma1_2, sigma2_2), Scalar.all(C2)).asMat();
    // t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2))
    t1 = t1.mul(t2).asMat();
    Mat ssim_map = new Mat();
    // ssim_map =  t3./t1;
    opencv_core.divide(t3, t1, ssim_map);
    // mssim = average of ssim map
    Scalar mssim = opencv_core.mean(ssim_map);
    return mssim;
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) Size(org.bytedeco.opencv.opencv_core.Size) Scalar(org.bytedeco.opencv.opencv_core.Scalar)

Example 57 with Mat

use of org.bytedeco.opencv.opencv_core.Mat in project javacpp-presets by bytedeco.

the class RealSense2Show method toColorMap.

private static byte[] toColorMap(byte[] depth, int w, int h, double max, double min) {
    byte[] result = new byte[w * h * 3];
    Mat depthmat = new Mat(h, w, opencv_core.CV_16UC1);
    depthmat.data().put(depth, 0, depth.length);
    Mat rmat = toColorMap(depthmat, max, min);
    rmat.data().get(result, 0, result.length);
    depthmat.release();
    depthmat.close();
    rmat.release();
    rmat.close();
    return result;
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat)

Example 58 with Mat

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

the class OpenCvUtils method collect.

private static int collect(int strictness, List<Region> found, boolean findAll, RobotBase robot, Mat source, Mat target, double scale) {
    int targetWidth = target.cols();
    int targetHeight = target.rows();
    int targetMinVal = targetWidth * targetHeight * TARGET_MINVAL_FACTOR * strictness;
    Mat result = new Mat();
    int[] minData = templateAndMin(strictness, scale, source, target, result);
    int minValue = minData[2];
    if (minValue > targetMinVal) {
        logger.debug("no match at scale {}, minVal: {} / {} at {}:{}", scale, minValue, targetMinVal, minData[0], minData[1]);
        if (robot != null && robot.debug) {
            Rect rect = new Rect(minData[0], minData[1], targetWidth, targetHeight);
            Mat temp = drawOnImage(source, rect, Scalar.RED);
            show(temp, scale + " " + minData[0] + ":" + minData[1] + " " + minValue + " / " + targetMinVal);
        }
        return minData[2];
    }
    logger.debug("found match at scale {}, minVal: {} / {} at {}:{}", scale, minValue, targetMinVal, minData[0], minData[1]);
    if (findAll) {
        List<int[]> points = getPointsBelowThreshold(result, targetMinVal);
        for (int[] p : points) {
            Region region = toRegion(robot, p, scale, targetWidth, targetHeight);
            found.add(region);
        }
    } else {
        Region region = toRegion(robot, minData, scale, targetWidth, targetHeight);
        found.add(region);
    }
    return minValue;
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) Rect(org.bytedeco.opencv.opencv_core.Rect) Point(org.bytedeco.opencv.opencv_core.Point)

Example 59 with Mat

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

the class OpenCvUtils method negative.

public static Mat negative(Mat src) {
    Mat dest = new Mat();
    bitwise_not(src, dest);
    return dest;
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat)

Example 60 with Mat

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

the class OpenCvUtils method loadAndShowOrExit.

public static Mat loadAndShowOrExit(File file, int flags) {
    Mat image = read(file, flags);
    show(image, file.getName());
    return image;
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat)

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