Search in sources :

Example 1 with Scalar

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

the class Similarity method main.

public static void main(String[] args) {
    Mat img1 = opencv_imgcodecs.imread("face.jpg");
    Mat img2 = img1.clone();
    opencv_imgproc.GaussianBlur(img2, img2, new Size(15, 15), 10);
    double psnr = getPSNR(img1, img2);
    Scalar mssim = getMSSIM(img1, img2);
    System.out.println("PSNR: " + psnr);
    System.out.printf("SSIM: %f, %f, %f\n", mssim.get(0), mssim.get(1), mssim.get(2));
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) Size(org.bytedeco.opencv.opencv_core.Size) Scalar(org.bytedeco.opencv.opencv_core.Scalar)

Example 2 with Scalar

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

the class Similarity method getPSNR.

private static double getPSNR(Mat I1, Mat I2) {
    Mat s1 = new Mat();
    // |I1 - I2|
    opencv_core.absdiff(I1, I2, s1);
    // cannot make a square on 8 bits
    s1.convertTo(s1, opencv_core.CV_32F);
    // |I1 - I2|^2
    s1 = s1.mul(s1).asMat();
    // sum elements per channel
    Scalar s = opencv_core.sumElems(s1);
    // sum channels
    double sse = s.get(0) + s.get(1) + s.get(2);
    if (sse <= 1e-10) {
        // for small values return zero
        return 0;
    } else {
        double mse = sse / (double) (I1.channels() * I1.total());
        double psnr = 10.0 * Math.log10((255 * 255) / mse);
        return psnr;
    }
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) Scalar(org.bytedeco.opencv.opencv_core.Scalar)

Example 3 with Scalar

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

the class OpenCvUtils method drawOnImage.

public static Mat drawOnImage(Mat image, Point2fVector points) {
    Mat dest = image.clone();
    int radius = 5;
    Scalar red = new Scalar(0, 0, 255, 0);
    for (int i = 0; i < points.size(); i++) {
        Point2f p = points.get(i);
        circle(dest, new Point(Math.round(p.x()), Math.round(p.y())), radius, red);
    }
    return dest;
}
Also used : Mat(org.bytedeco.opencv.opencv_core.Mat) Point2f(org.bytedeco.opencv.opencv_core.Point2f) Point(org.bytedeco.opencv.opencv_core.Point) Point(org.bytedeco.opencv.opencv_core.Point) AbstractScalar(org.bytedeco.opencv.opencv_core.AbstractScalar) Scalar(org.bytedeco.opencv.opencv_core.Scalar)

Example 4 with Scalar

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

the class TypeAdaptersCVTest method testSizeAndScalars.

@SuppressWarnings("unchecked")
@Test
public void testSizeAndScalars() {
    var gson = new GsonBuilder().registerTypeAdapterFactory(OpenCVTypeAdapters.getOpenCVTypeAdaptorFactory()).create();
    try (PointerScope scope = new PointerScope()) {
        // Test scalar
        var scalarArray = new double[] { 1.1, 2.2, 3.3, 4.4 };
        for (int n = 0; n <= 4; n++) {
            var arr = Arrays.copyOf(scalarArray, n);
            Scalar scalar;
            if (n == 0)
                scalar = new Scalar();
            else if (n == 1)
                scalar = new Scalar(arr[0]);
            else if (n == 2)
                scalar = new Scalar(arr[0], arr[1]);
            else if (n == 3)
                scalar = new Scalar(arr[0], arr[1], arr[2], 0.0);
            else
                scalar = new Scalar(arr[0], arr[1], arr[2], arr[3]);
            assertArrayEquals(arr, scalarToArray(scalar, n), 1e-3);
            var scalarJson = gson.toJson(scalar);
            assertArrayEquals(arr, scalarToArray(gson.fromJson(scalarJson, Scalar.class), n), 1e-3);
        }
        // Test size
        for (int w : new int[] { 102, 234, -1 }) {
            var size = new Size(w, w * 2);
            var sizeJson = gson.toJson(size);
            var size2 = gson.fromJson(sizeJson, Size.class);
            assertEquals(size.width(), size2.width());
            assertEquals(size.height(), size2.height());
        }
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Size(org.bytedeco.opencv.opencv_core.Size) PointerScope(org.bytedeco.javacpp.PointerScope) Scalar(org.bytedeco.opencv.opencv_core.Scalar) Test(org.junit.jupiter.api.Test)

Example 5 with Scalar

use of org.bytedeco.opencv.opencv_core.Scalar 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)

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