Example usage for org.opencv.core Core add

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

Introduction

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

Prototype

public static void add(Mat src1, Scalar src2, Mat dst, Mat mask) 

Source Link

Usage

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 www  .ja v  a  2  s.c  om*/
    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);
    }
}