Example usage for org.opencv.imgproc Imgproc Scharr

List of usage examples for org.opencv.imgproc Imgproc Scharr

Introduction

In this page you can find the example usage for org.opencv.imgproc Imgproc Scharr.

Prototype

public static void Scharr(Mat src, Mat dst, int ddepth, int dx, int dy) 

Source Link

Usage

From source file:gab.opencv.OpenCV.java

License:Open Source License

public void findScharrEdges(int direction) {
    if (direction == HORIZONTAL) {
        Imgproc.Scharr(getCurrentMat(), getCurrentMat(), -1, 1, 0);
    }/*from  w w w  .  ja v a  2s.co m*/

    if (direction == VERTICAL) {
        Imgproc.Scharr(getCurrentMat(), getCurrentMat(), -1, 0, 1);
    }

    if (direction == BOTH) {
        Mat hMat = imitate(getCurrentMat());
        Mat vMat = imitate(getCurrentMat());
        Imgproc.Scharr(getCurrentMat(), hMat, -1, 1, 0);
        Imgproc.Scharr(getCurrentMat(), vMat, -1, 0, 1);
        Core.add(vMat, hMat, getCurrentMat());
    }
}

From source file:gab.opencv.OpenCVProcessingUtils.java

License:Open Source License

public void findScharrEdges(int direction) {
    if (direction == HORIZONTAL) {
        Imgproc.Scharr(getCurrentMat(), getCurrentMat(), -1, 1, 0);
    }// w w  w  .  ja v a 2s. c om

    if (direction == VERTICAL) {
        Imgproc.Scharr(getCurrentMat(), getCurrentMat(), -1, 0, 1);
    }
}

From source file:karthik.Barcode.MatrixBarcode.java

License:Open Source License

private void calcGradientDirectionAndMagnitude() {
    // calculates magnitudes and directions of gradients in the image
    // results are stored in appropriate matrices in img_details object

    Imgproc.Scharr(img_details.src_grayscale, img_details.scharr_x, CvType.CV_32F, 1, 0);
    Imgproc.Scharr(img_details.src_grayscale, img_details.scharr_y, CvType.CV_32F, 0, 1);

    // calc angle using Core.phase function - quicker than using atan2 manually
    Core.phase(img_details.scharr_x, img_details.scharr_y, img_details.gradient_direction, true);

    // convert angles from 180-360 to 0-180 range and set angles from 170-180 to 0
    Core.inRange(img_details.gradient_direction, scalarDict.get(180), scalarDict.get(360), img_details.mask);
    Core.add(img_details.gradient_direction, scalarDict.get(-180), img_details.gradient_direction,
            img_details.mask);/*from   w ww  .j a  va  2  s  .co m*/
    Core.inRange(img_details.gradient_direction, scalarDict.get(170), scalarDict.get(180), img_details.mask);
    img_details.gradient_direction.setTo(ZERO_SCALAR, img_details.mask);

    // convert type after modifying angle so that angles above 360 don't get truncated
    img_details.gradient_direction.convertTo(img_details.gradient_direction, CvType.CV_8U);
    if (DEBUG_IMAGES)
        write_Mat("angles.csv", img_details.gradient_direction);

    // calculate magnitude of gradient, normalize and threshold
    Core.magnitude(img_details.scharr_x, img_details.scharr_y, img_details.gradient_magnitude);
    Core.normalize(img_details.gradient_magnitude, img_details.gradient_magnitude, 0, 255, Core.NORM_MINMAX,
            CvType.CV_8U);
    Imgproc.threshold(img_details.gradient_magnitude, img_details.gradient_magnitude, 50, 255,
            Imgproc.THRESH_BINARY | Imgproc.THRESH_OTSU);

    // set angle to DUMMY_ANGLE = 255 at all points where gradient magnitude is 0 i.e. where there are no edges
    // these angles will be ignored in the histogram calculation since that counts only up to 180
    Core.inRange(img_details.gradient_magnitude, ZERO_SCALAR, ZERO_SCALAR, img_details.mask);
    img_details.gradient_direction.setTo(scalarDict.get(DUMMY_ANGLE), img_details.mask);
    // add 1 to gradient directions so that gradients of 0 can be located
    Core.add(img_details.gradient_direction, new Scalar(1), img_details.gradient_direction);

    // calculate integral image for edge density
    img_details.edgeDensity = calcEdgeDensityIntegralImage();

    // calculate histograms for each tile
    calcHistograms();

    if (DEBUG_IMAGES) {
        write_Mat("magnitudes.csv", img_details.gradient_magnitude);
        write_Mat("angles_modified.csv", img_details.gradient_direction);
    }
}