Example usage for org.opencv.core Core convertScaleAbs

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

Introduction

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

Prototype

public static void convertScaleAbs(Mat src, Mat dst) 

Source Link

Usage

From source file:javaapplication1.Ocv.java

void doSobel(String input, String output) {
    // load the image and read it into a matrix
    File f2 = new File(input);
    Mat image = Highgui.imread(this.input);

    // blur the image
    Imgproc.GaussianBlur(image, image, new Size(3, 3), 0, 0, Imgproc.BORDER_DEFAULT);

    // convert the image to gray
    Mat gray = new Mat();
    Imgproc.cvtColor(image, gray, Imgproc.COLOR_BGR2GRAY);

    // parameters for our conversion
    int scale = 1;
    int delta = 0;
    int ddepth = CvType.CV_16S;

    // figure out the X gradient, get its absolute value
    Mat gx = new Mat();
    Imgproc.Sobel(gray, gx, ddepth, 1, 0, 3, scale, delta, Imgproc.BORDER_DEFAULT);
    Mat abs_gx = new Mat();
    Core.convertScaleAbs(gx, abs_gx);

    // do the same for Y
    Mat gy = new Mat();
    Imgproc.Sobel(gray, gy, ddepth, 0, 1, 3, scale, delta, Imgproc.BORDER_DEFAULT);
    Mat abs_gy = new Mat();
    Core.convertScaleAbs(gy, abs_gy);// w w w . j a  v  a  2s .com

    // do a simple combine, and save it
    Mat grad = new Mat();
    Core.addWeighted(abs_gx, 0.5, abs_gy, 0.5, 0, grad);
    Highgui.imwrite(this.output, grad);
}

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

License:Open Source License

public FluentCv findEdgesRobertsCross(String... tag) {
    // Java interpretation of
    // https://www.scss.tcd.ie/publications/book-supplements/A-Practical-Introduction-to-Computer-Vision-with-OpenCV/Code/Edges.cpp
    // Note: Java API does not have abs. This appears to be doing the
    // same thing effectively, but I am not sure it's 100% the same
    // as Cri's version.
    Mat kernel = Mat.eye(new Size(2, 2), CvType.CV_32FC1);
    kernel.put(0, 0, 0, 1, -1, 0);/*from  w w w .j  a va  2s.  com*/
    Mat roberts1 = new Mat();
    Imgproc.filter2D(mat, roberts1, CvType.CV_32FC1, kernel);
    Core.convertScaleAbs(roberts1, roberts1);

    kernel.put(0, 0, 1, 0, 0, -1);
    Mat roberts2 = new Mat();
    Imgproc.filter2D(mat, roberts2, CvType.CV_32FC1, kernel);
    Core.convertScaleAbs(roberts2, roberts2);

    Mat roberts = new Mat();
    Core.add(roberts1, roberts2, roberts);

    return store(roberts, tag);

    // // Java interpretation of Cri S's C version.
    // // This is very slow, my fault, not his. Probably due to all the
    // // array accesses.
    // int ptr1[] = { 0, 0, 0, 0 };
    // int indexx[] = { 0, 1, 1, 0 };
    // int indexy[] = { 0, 0, 1, 1 };
    // for (int y = 0; y < mat.rows() - 1; y++) {
    // for (int x = 0; x < mat.cols() - 1; x++) {
    // int temp = 0, temp1 = 0;
    // for (int i = 0; i < 4; i++) {
    // ptr1[i] = (int) mat.get(y + indexy[i], x + indexx[i])[0]; // // ptr1[i] = *(ptr + (y +
    // indexy[i]) * gray->widthStep + x + indexx[i]);
    // }
    // temp = Math.abs(ptr1[0] - ptr1[2]);
    // temp1 = Math.abs(ptr1[1] - ptr1[3]);
    // temp = (temp > temp1 ? temp : temp1);
    // temp = (int) Math.sqrt((float) (temp * temp) + (float) (temp1 * temp1));
    // mat.put(y, x, temp); // *(ptr + y * gray->widthStep + x) = temp;
    // }
    // }
    // return store(mat, tag);
}

From source file:pfe.Segmentation.java

@Override
public void applySobelFactor(JLabel jLabelSobelFactor) {
    // init/* w w  w .  j a  va2s. c o m*/
    Mat frame = Imgcodecs.imread(smoothed);
    Mat grayImage = new Mat();
    Mat detectedEdges = new Mat();

    int ddepth = CvType.CV_16S;
    Mat grad_x = new Mat();
    Mat grad_y = new Mat();
    Mat abs_grad_x = new Mat();
    Mat abs_grad_y = new Mat();

    // reduce noise with a 3x3 kernel
    Imgproc.GaussianBlur(frame, frame, new Size(3, 3), 0, 0, Core.BORDER_DEFAULT);

    // convert to grayscale
    Imgproc.cvtColor(frame, grayImage, Imgproc.COLOR_BGR2GRAY);

    // Gradient X
    Imgproc.Sobel(grayImage, grad_x, ddepth, 1, 0);
    Core.convertScaleAbs(grad_x, abs_grad_x);

    // Gradient Y
    Imgproc.Sobel(grayImage, grad_y, ddepth, 0, 1);
    Core.convertScaleAbs(grad_y, abs_grad_y);

    // Total Gradient (approximate)
    Core.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, detectedEdges);
    // Core.addWeighted(grad_x, 0.5, grad_y, 0.5, 0, detectedEdges);

    Imgcodecs.imwrite(sobelImage, detectedEdges);
    jLabelSobelFactor.setIcon(new ImageIcon(sobelImage));

}