Example usage for org.opencv.core Core sumElems

List of usage examples for org.opencv.core Core sumElems

Introduction

In this page you can find the example usage for org.opencv.core Core sumElems.

Prototype

public static Scalar sumElems(Mat src) 

Source Link

Usage

From source file:at.ac.tuwien.caa.docscan.camera.CameraPreview.java

License:Open Source License

public boolean isFrameSame(Mat frame1, Mat frame2) {

    Mat tmp1 = new Mat(frame1.rows(), frame1.cols(), CvType.CV_8UC1);
    Imgproc.cvtColor(frame1, tmp1, Imgproc.COLOR_RGB2GRAY);

    Mat tmp2 = new Mat(frame2.rows(), frame2.cols(), CvType.CV_8UC1);
    Imgproc.cvtColor(frame2, tmp2, Imgproc.COLOR_RGB2GRAY);

    Mat subtractResult = new Mat(frame2.rows(), frame2.cols(), CvType.CV_8UC1);
    Core.absdiff(frame1, frame2, subtractResult);
    Imgproc.threshold(subtractResult, subtractResult, 50, 1, Imgproc.THRESH_BINARY);
    Scalar sumDiff = Core.sumElems(subtractResult);
    double diffRatio = sumDiff.val[0] / (frame1.cols() * frame2.rows());

    return diffRatio < .05;

}

From source file:at.uniklu.itec.videosummary.Summarize.java

License:GNU General Public License

private double getImageSharpness(File frame) {
    Mat img = Highgui.imread(frame.getAbsolutePath(), 0);
    Mat dx, dy;/*  ww w .  j av a 2 s  .c om*/
    dx = new Mat();
    dy = new Mat();
    Imgproc.Sobel(img, dx, CvType.CV_32F, 1, 0);
    Imgproc.Sobel(img, dy, CvType.CV_32F, 0, 1);
    Core.magnitude(dx, dy, dx);
    Scalar sum = Core.sumElems(dx);
    img.release();
    dx.release();
    dy.release();
    System.gc();
    System.gc();
    System.gc();
    //System.out.println("Sum of gradients= "+sum);
    return (sum.val[0]);
}

From source file:es.ugr.osgiliart.features.opencv.Histogram.java

License:Open Source License

@Override
public double[] extract(Mat image) {
    Mat hsvImage = new Mat(image.width(), image.height(), image.type());
    Mat histHue = new Mat();
    Mat histSaturation = new Mat();

    Imgproc.cvtColor(image, hsvImage, Imgproc.COLOR_BGR2HSV);
    List<Mat> channels = new ArrayList<Mat>();
    Core.split(hsvImage, channels);//from  ww  w . j ava  2 s. c  o m

    //Histogram for hue
    Imgproc.calcHist(Arrays.asList(new Mat[] { channels.get(0) }), new MatOfInt(0), new Mat(), histHue,
            new MatOfInt(BINS), new MatOfFloat(MIN_VALUE, MAX_VALUE));

    //Histogram for saturation
    Imgproc.calcHist(Arrays.asList(new Mat[] { channels.get(1) }), new MatOfInt(0), new Mat(), histSaturation,
            new MatOfInt(BINS), new MatOfFloat(MIN_VALUE, MAX_VALUE));

    double sum = Core.sumElems(histHue).val[0];
    double[] values = new double[histHue.height() + histSaturation.height()];
    int k = 0;
    for (int i = 0; i < histHue.height(); ++i) {
        values[k++] = histHue.get(i, 0)[0] / sum;
    }
    sum = Core.sumElems(histSaturation).val[0];
    for (int i = 0; i < histSaturation.height(); ++i) {
        values[k++] = histSaturation.get(i, 0)[0] / sum;
    }
    return values;
}

From source file:in.fabinpaul.sixthsense.ColorBlobDetectionFragment.java

License:Apache License

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        count++;/* w  ww.  j  a v  a2 s  .  com*/
        if (count > 3)
            count = 0;

        if (count == 3) {
            colorMarkerSet = true;
            comm.saveButtonVisibility();
        }

        int cols = mRgba.cols();
        int rows = mRgba.rows();

        int xOffset = (mOpenCvCameraView.getWidth() - cols) / 2;
        int yOffset = (mOpenCvCameraView.getHeight() - rows) / 2;
        Log.i(TAG, "x coordinates" + event.getX() + "y coordinates" + event.getY());
        Log.i(TAG, "View width" + mOpenCvCameraView.getWidth() + "View Height" + mOpenCvCameraView.getHeight());

        int x = (int) event.getX() - xOffset;
        int y = (int) event.getY() - yOffset;

        if ((x < 0) || (y < 0) || (x > cols) || (y > rows))
            return false;

        Rect touchedRect = new Rect();

        touchedRect.x = (x > 4) ? x - 4 : 0;
        touchedRect.y = (y > 4) ? y - 4 : 0;

        touchedRect.width = (x + 4 < cols) ? x - 1 - touchedRect.x : cols - touchedRect.x;
        touchedRect.height = (y + 4 < rows) ? y - 1 - touchedRect.y : rows - touchedRect.y;

        Log.i(TAG, "Width" + touchedRect.width + " Height" + touchedRect.height);
        Log.i(TAG, "Column" + cols + " Rows" + rows);
        Log.i(TAG, "Touch image coordinates: (" + x + ", " + y + ")");

        Mat touchedRegionRgba = mRgba.submat(touchedRect);

        Mat touchedRegionHsv = new Mat();
        Imgproc.cvtColor(touchedRegionRgba, touchedRegionHsv, Imgproc.COLOR_RGB2HSV_FULL);

        // Calculate average color of touched region
        mBlobColorHsv[count] = Core.sumElems(touchedRegionHsv);
        int pointCount = touchedRect.width * touchedRect.height;
        for (int i = 0; i < mBlobColorHsv[count].val.length; i++)
            mBlobColorHsv[count].val[i] /= pointCount;

        mBlobColorRgba[count] = converScalarHsv2Rgba(mBlobColorHsv[count]);

        Log.i(TAG, "Before" + mBlobColorHsv[count].val[0] + " " + mBlobColorHsv[count].val[1] + " "
                + mBlobColorHsv[count].val[2]);
        Log.i(TAG, "After" + mBlobColorRgba[count].val[0] + " " + mBlobColorRgba[count].val[1] + " "
                + mBlobColorRgba[count].val[2]);

        Log.i(TAG, "Touched rgba color: (" + mBlobColorRgba[count].val[0] + ", " + mBlobColorRgba[count].val[1]
                + ", " + mBlobColorRgba[count].val[2] + ", " + mBlobColorRgba[count].val[3] + ")");

        // mDetector[count].setHsvColor(mBlobColorHsv[count]);
        setHSV(count);

        mIsColorSelected[count] = true;

        touchedRegionRgba.release();
        touchedRegionHsv.release();
    }
    return true; // don't need subsequent touch events
}

From source file:org.openpnp.vision.FluentCv.java

License:Open Source License

public static double calculatePsnr(Mat I1, Mat I2) {
    Mat s1 = new Mat();
    Core.absdiff(I1, I2, s1); // |I1 - I2|
    s1.convertTo(s1, CvType.CV_32F); // cannot make a square on 8 bits
    s1 = s1.mul(s1); // |I1 - I2|^2

    Scalar s = Core.sumElems(s1); // sum elements per channel

    double sse = s.val[0] + s.val[1] + s.val[2]; // sum channels

    if (sse <= 1e-10) // for small values return zero
        return 0;
    else {/*  w  w w.j  av  a 2s .  c o  m*/
        double mse = sse / (double) (I1.channels() * I1.total());
        double psnr = 10.0 * Math.log10((255 * 255) / mse);
        return psnr;
    }
}

From source file:tv.danmaku.ijk.media.example.activities.VideoActivity.java

License:Apache License

public boolean onTouch(View v, MotionEvent event) {
    int cols = mRgba.cols();
    int rows = mRgba.rows();

    int xOffset = (mOpenCvCameraView.getWidth() - cols) / 2;
    int yOffset = (mOpenCvCameraView.getHeight() - rows) / 2;

    int x = (int) event.getX() - xOffset;
    int y = (int) event.getY() - yOffset;

    Log.i(TAG, "Touch image coordinates: (" + x + ", " + y + ")");

    if ((x < 0) || (y < 0) || (x > cols) || (y > rows))
        return false;

    Rect touchedRect = new Rect();

    touchedRect.x = (x > 5) ? x - 5 : 0;
    touchedRect.y = (y > 5) ? y - 5 : 0;

    touchedRect.width = (x + 5 < cols) ? x + 5 - touchedRect.x : cols - touchedRect.x;
    touchedRect.height = (y + 5 < rows) ? y + 5 - touchedRect.y : rows - touchedRect.y;

    Mat touchedRegionRgba = mRgba.submat(touchedRect);

    Mat touchedRegionHsv = new Mat();
    Imgproc.cvtColor(touchedRegionRgba, touchedRegionHsv, Imgproc.COLOR_RGB2HSV_FULL);

    // Calculate average color of touched region
    mBlobColorHsv = Core.sumElems(touchedRegionHsv);
    int pointCount = touchedRect.width * touchedRect.height;
    for (int i = 0; i < mBlobColorHsv.val.length; i++)
        mBlobColorHsv.val[i] /= pointCount;

    mBlobColorRgba = converScalarHsv2Rgba(mBlobColorHsv);

    Log.i(TAG, "Touched rgba color: (" + mBlobColorRgba.val[0] + ", " + mBlobColorRgba.val[1] + ", "
            + mBlobColorRgba.val[2] + ", " + mBlobColorRgba.val[3] + ")");

    mDetector.setHsvColor(mBlobColorHsv);

    Imgproc.resize(mDetector.getSpectrum(), mSpectrum, SPECTRUM_SIZE);

    mIsColorSelected = true;//  w w  w.  j  a v  a 2s. c  o  m

    touchedRegionRgba.release();
    touchedRegionHsv.release();

    return false; // don't need subsequent touch events
}