Java Code Examples for org.opencv.imgproc.Imgproc#Sobel

The following examples show how to use org.opencv.imgproc.Imgproc#Sobel . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From MOAAP with MIT License 6 votes vote down vote up
void Sobel() {
    Mat grayMat = new Mat();
    Mat sobel = new Mat(); //Mat to store the final result

    //Matrices to store gradient and absolute gradient respectively
    Mat grad_x = new Mat();
    Mat abs_grad_x = new Mat();

    Mat grad_y = new Mat();
    Mat abs_grad_y = new Mat();

    //Converting the image to grayscale
    Imgproc.cvtColor(originalMat, grayMat, Imgproc.COLOR_BGR2GRAY);

    //Calculating gradient in horizontal direction
    Imgproc.Sobel(grayMat, grad_x, CvType.CV_16S, 1, 0, 3, 1, 0);

    //Calculating gradient in vertical direction
    Imgproc.Sobel(grayMat, grad_y, CvType.CV_16S, 0, 1, 3, 1, 0);

    //Calculating absolute value of gradients in both the direction
    Core.convertScaleAbs(grad_x, abs_grad_x);
    Core.convertScaleAbs(grad_y, abs_grad_y);

    //Calculating the resultant gradient
    Core.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 1, sobel);

    //Converting Mat back to Bitmap
    Utils.matToBitmap(sobel, currentBitmap);
    imageView.setImageBitmap(currentBitmap);
}
 
Example 2
Source File: MainActivity.java    From open-quartz with Apache License 2.0 6 votes vote down vote up
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    mRgba = inputFrame.rgba();
    mGray = inputFrame.gray();

    switch (MainActivity.viewMode) {
        case MainActivity.VIEW_MODE_RGBA:
            return mRgba;

        case MainActivity.VIEW_MODE_HIST:
            return mRgba;

        case MainActivity.VIEW_MODE_CANNY:
            Imgproc.Canny(mGray, mIntermediateMat, 80, 100);
            Imgproc.cvtColor(mIntermediateMat, mGray, Imgproc.COLOR_GRAY2BGRA, 4);
            return mGray;

        case MainActivity.VIEW_MODE_SOBEL:
            Imgproc.Sobel(mGray, mGray, CvType.CV_8U, 1, 1);
            //			Core.convertScaleAbs(mIntermediateMat, mIntermediateMat, 10, 0);
            Imgproc.cvtColor(mGray, mGray, Imgproc.COLOR_GRAY2BGRA, 4);
            return mGray;

        case MainActivity.VIEW_MODE_PIXELIZE:
            Imgproc.resize(mGray, mIntermediateMat, mSize0, 0.1, 0.1,
                Imgproc.INTER_NEAREST);
            Imgproc.resize(mIntermediateMat, mRgba, mRgba.size(), 0.0, 0.0,
                Imgproc.INTER_NEAREST);
            return mRgba;

        case MainActivity.VIEW_MODE_GRAY:
            return mGray;

        case MainActivity.VIEW_MODE_FEATURES:
            FindFeatures(mGray.getNativeObjAddr(), mRgba.getNativeObjAddr());
            return mRgba;

        default:
            return mRgba;
    }
}
 
Example 3
Source File: CVProcessor.java    From CVScanner with GNU General Public License v3.0 5 votes vote down vote up
public static Rect detectBorder(Mat original){
    Mat src = original.clone();
    Log.d(TAG, "1 original: " + src.toString());

    Imgproc.GaussianBlur(src, src, new Size(3, 3), 0);
    Log.d(TAG, "2.1 --> Gaussian blur done\n blur: " + src.toString());

    Imgproc.cvtColor(src, src, Imgproc.COLOR_RGBA2GRAY);
    Log.d(TAG, "2.2 --> Grayscaling done\n gray: " + src.toString());

    Mat sobelX = new Mat();
    Mat sobelY = new Mat();

    Imgproc.Sobel(src, sobelX, CvType.CV_32FC1, 2, 0, 5, 1, 0);
    Log.d(TAG, "3.1 --> Sobel done.\n X: " + sobelX.toString());
    Imgproc.Sobel(src, sobelY, CvType.CV_32FC1, 0, 2, 5, 1, 0);
    Log.d(TAG, "3.2 --> Sobel done.\n Y: " + sobelY.toString());

    Mat sum_img = new Mat();
    Core.addWeighted(sobelX, 0.5, sobelY, 0.5, 0.5, sum_img);
    //Core.add(sobelX, sobelY, sum_img);
    Log.d(TAG, "4 --> Addition done. sum: " + sum_img.toString());

    sobelX.release();
    sobelY.release();

    Mat gray = new Mat();
    Core.normalize(sum_img, gray, 0, 255, Core.NORM_MINMAX, CvType.CV_8UC1);
    Log.d(TAG, "5 --> Normalization done. gray: " + gray.toString());
    sum_img.release();

    Mat row_proj = new Mat();
    Mat col_proj = new Mat();
    Core.reduce(gray, row_proj, 1, Core.REDUCE_AVG, CvType.CV_8UC1);
    Log.d(TAG, "6.1 --> Reduce done. row: " + row_proj.toString());

    Core.reduce(gray, col_proj, 0, Core.REDUCE_AVG, CvType.CV_8UC1);
    Log.d(TAG, "6.2 --> Reduce done. col: " + col_proj.toString());
    gray.release();

    Imgproc.Sobel(row_proj, row_proj, CvType.CV_8UC1, 0, 2);
    Log.d(TAG, "7.1 --> Sobel done. row: " + row_proj.toString());

    Imgproc.Sobel(col_proj, col_proj, CvType.CV_8UC1, 2, 0);
    Log.d(TAG, "7.2 --> Sobel done. col: " + col_proj.toString());

    Rect result = new Rect();

    int half_pos = (int) (row_proj.total()/2);
    Mat row_sub = new Mat(row_proj, new Range(0, half_pos), new Range(0, 1));
    Log.d(TAG, "8.1 --> Copy sub matrix done. row: " + row_sub.toString());
    result.y = (int) Core.minMaxLoc(row_sub).maxLoc.y;
    Log.d(TAG, "8.2 --> Minmax done. Y: " + result.y);
    row_sub.release();
    Mat row_sub2 = new Mat(row_proj, new Range(half_pos, (int) row_proj.total()), new Range(0, 1));
    Log.d(TAG, "8.3 --> Copy sub matrix done. row: " + row_sub2.toString());
    result.height = (int) (Core.minMaxLoc(row_sub2).maxLoc.y + half_pos - result.y);
    Log.d(TAG, "8.4 --> Minmax done. Height: " + result.height);
    row_sub2.release();

    half_pos = (int) (col_proj.total()/2);
    Mat col_sub = new Mat(col_proj, new Range(0, 1), new Range(0, half_pos));
    Log.d(TAG, "9.1 --> Copy sub matrix done. col: " + col_sub.toString());
    result.x = (int) Core.minMaxLoc(col_sub).maxLoc.x;
    Log.d(TAG, "9.2 --> Minmax done. X: " + result.x);
    col_sub.release();
    Mat col_sub2 = new Mat(col_proj, new Range(0, 1), new Range(half_pos, (int) col_proj.total()));
    Log.d(TAG, "9.3 --> Copy sub matrix done. col: " + col_sub2.toString());
    result.width = (int) (Core.minMaxLoc(col_sub2).maxLoc.x + half_pos - result.x);
    Log.d(TAG, "9.4 --> Minmax done. Width: " + result.width);
    col_sub2.release();

    row_proj.release();
    col_proj.release();
    src.release();

    return result;
}
 
Example 4
Source File: CVProcessor.java    From CVScanner with GNU General Public License v3.0 5 votes vote down vote up
public static List<MatOfPoint> findContoursForMRZ(Mat src){
    Mat img = src.clone();
    src.release();
    double ratio = getScaleRatio(img.size());
    int width = (int) (img.size().width / ratio);
    int height = (int) (img.size().height / ratio);
    Size newSize = new Size(width, height);
    Mat resizedImg = new Mat(newSize, CvType.CV_8UC4);
    Imgproc.resize(img, resizedImg, newSize);

    Mat gray = new Mat();
    Imgproc.cvtColor(resizedImg, gray, Imgproc.COLOR_BGR2GRAY);
    Imgproc.medianBlur(gray, gray, 3);
    //Imgproc.blur(gray, gray, new Size(3, 3));

    Mat morph = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(13, 5));
    Mat dilatedImg = new Mat();
    Imgproc.morphologyEx(gray, dilatedImg, Imgproc.MORPH_BLACKHAT, morph);
    gray.release();

    Mat gradX = new Mat();
    Imgproc.Sobel(dilatedImg, gradX, CvType.CV_32F, 1, 0);
    dilatedImg.release();
    Core.convertScaleAbs(gradX, gradX, 1, 0);
    Core.MinMaxLocResult minMax = Core.minMaxLoc(gradX);
    Core.convertScaleAbs(gradX, gradX, (255/(minMax.maxVal - minMax.minVal)),
            - ((minMax.minVal * 255) / (minMax.maxVal - minMax.minVal)));
    Imgproc.morphologyEx(gradX, gradX, Imgproc.MORPH_CLOSE, morph);

    Mat thresh = new Mat();
    Imgproc.threshold(gradX, thresh, 0, 255, Imgproc.THRESH_OTSU);
    gradX.release();
    morph.release();

    morph = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(21, 21));
    Imgproc.morphologyEx(thresh, thresh, Imgproc.MORPH_CLOSE, morph);
    Imgproc.erode(thresh, thresh, new Mat(), new Point(-1, -1), 4);
    morph.release();

    int col = (int) resizedImg.size().width;
    int p = (int) (resizedImg.size().width * 0.05);
    int row = (int) resizedImg.size().height;
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < p; j++){
            thresh.put(i, j, 0);
            thresh.put(i, col-j, 0);
        }
    }

    List<MatOfPoint> contours = new ArrayList<>();
    Mat hierarchy = new Mat();
    Imgproc.findContours(thresh, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
    hierarchy.release();

    Log.d(TAG, "contours found: " + contours.size());

    Collections.sort(contours, new Comparator<MatOfPoint>() {
        @Override
        public int compare(MatOfPoint o1, MatOfPoint o2) {
            return Double.valueOf(Imgproc.contourArea(o2)).compareTo(Imgproc.contourArea(o1));
        }
    });

    return contours;
}