Java Code Examples for org.opencv.imgproc.Imgproc#arcLength()

The following examples show how to use org.opencv.imgproc.Imgproc#arcLength() . 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: CVProcessor.java    From CVScanner with GNU General Public License v3.0 6 votes vote down vote up
static public Quadrilateral getQuadrilateral(List<MatOfPoint> contours, Size srcSize){
    double ratio = getScaleRatio(srcSize);
    int height = Double.valueOf(srcSize.height / ratio).intValue();
    int width = Double.valueOf(srcSize.width / ratio).intValue();
    Size size = new Size(width,height);

    for ( MatOfPoint c: contours ) {
        MatOfPoint2f c2f = new MatOfPoint2f(c.toArray());
        double peri = Imgproc.arcLength(c2f, true);
        MatOfPoint2f approx = new MatOfPoint2f();
        Imgproc.approxPolyDP(c2f, approx, 0.02 * peri, true);

        Point[] points = approx.toArray();
        Log.d("SCANNER", "approx size: " + points.length);

        // select biggest 4 angles polygon
        if (points.length == 4) {
            Point[] foundPoints = sortPoints(points);

            if (isInside(foundPoints, size) && isLargeEnough(foundPoints, size, 0.25)) {
                return new Quadrilateral( c , foundPoints );
            }
            else{
                //showToast(context, "Try getting closer to the ID");
                Log.d("SCANNER", "Not inside defined area");
            }
        }
    }

    //showToast(context, "Make sure the ID is on a contrasting background");
    return null;
}
 
Example 2
Source File: NativeClass.java    From AndroidDocumentScanner with MIT License 4 votes vote down vote up
public List<MatOfPoint2f> getPoints(Mat src) {

        // Blur the image to filter out the noise.
        Mat blurred = new Mat();
        Imgproc.medianBlur(src, blurred, 9);

        // Set up images to use.
        Mat gray0 = new Mat(blurred.size(), CvType.CV_8U);
        Mat gray = new Mat();

        // For Core.mixChannels.
        List<MatOfPoint> contours = new ArrayList<>();
        List<MatOfPoint2f> rectangles = new ArrayList<>();

        List<Mat> sources = new ArrayList<>();
        sources.add(blurred);
        List<Mat> destinations = new ArrayList<>();
        destinations.add(gray0);

        // To filter rectangles by their areas.
        int srcArea = src.rows() * src.cols();

        // Find squares in every color plane of the image.
        for (int c = 0; c < 3; c++) {
            int[] ch = {c, 0};
            MatOfInt fromTo = new MatOfInt(ch);

            Core.mixChannels(sources, destinations, fromTo);

            // Try several threshold levels.
            for (int l = 0; l < THRESHOLD_LEVEL; l++) {
                if (l == 0) {
                    // HACK: Use Canny instead of zero threshold level.
                    // Canny helps to catch squares with gradient shading.
                    // NOTE: No kernel size parameters on Java API.
                    Imgproc.Canny(gray0, gray, 10, 20);

                    // Dilate Canny output to remove potential holes between edge segments.
                    Imgproc.dilate(gray, gray, Mat.ones(new Size(3, 3), 0));
                } else {
                    int threshold = (l + 1) * 255 / THRESHOLD_LEVEL;
                    Imgproc.threshold(gray0, gray, threshold, 255, Imgproc.THRESH_BINARY);
                }

                // Find contours and store them all as a list.
                Imgproc.findContours(gray, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

                for (MatOfPoint contour : contours) {
                    MatOfPoint2f contourFloat = MathUtils.toMatOfPointFloat(contour);
                    double arcLen = Imgproc.arcLength(contourFloat, true) * 0.02;

                    // Approximate polygonal curves.
                    MatOfPoint2f approx = new MatOfPoint2f();
                    Imgproc.approxPolyDP(contourFloat, approx, arcLen, true);

                    if (isRectangle(approx, srcArea)) {
                        rectangles.add(approx);
                    }
                }
            }
        }

        return rectangles;

    }
 
Example 3
Source File: Proc.java    From android-object-distance with Apache License 2.0 4 votes vote down vote up
public static  double findMarkerWidth(String imgPath){
    Mat frame = Highgui.imread(imgPath);
    Mat gscale = new Mat();
    Mat blur = new Mat();
    Mat edged = new Mat();

    // convert the image to grayscale, blur it, and detect edges
    if(frame.channels()>1)
        Imgproc.cvtColor(frame, gscale, Imgproc.COLOR_BGR2GRAY);
    else
        gscale = frame;

    Imgproc.GaussianBlur(gscale, blur, new Size(5, 5), 0);
    Imgproc.Canny(blur, edged, 35, 125);

    // find the contours in the edged image and keep the largest one;
    // we'll assume that this is our piece of paper in the image
    List<MatOfPoint> contours = new ArrayList<>();
    Mat hierarchy = new Mat(edged.width(), edged.height(), CvType.CV_8UC1);
    Imgproc.findContours(edged.clone(), contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
    int max_idx = 0;

    // if any contour exist...
    if (hierarchy.size().height > 0 && hierarchy.size().width > 0)
    {
        double max_area = 0;
        double area;
        // find the contour with largest area
        for (int idx = 0; idx >= 0; idx = (int) hierarchy.get(0, idx)[0])
        {
            area = Imgproc.contourArea(contours.get(idx));
            if(area > max_area){
                max_area = area;
                max_idx = idx;
            }
            Imgproc.drawContours(frame, contours, idx, new Scalar(0, 0, 255));
        }

        //Riz: Save File
        //Imgproc.drawContours(frame, contours, max_idx, new Scalar(250, 0, 0));
        byte[] bytes = new byte[ frame.rows() * frame.cols() * frame.channels() ];


        File file = new File(CameraActivity.activity.getExternalFilesDir(null), "pic_contour"+ Integer.toString(pic_count) + ".jpg");
        pic_count++;

        Boolean bool = null;
        String filename = file.toString();
        bool = Highgui.imwrite(filename, frame);

        if (bool == true)
            Log.d(LOG_TAG, "SUCCESS writing image to external storage");
        else
            Log.d(LOG_TAG, "Fail writing image to external storage");

        Log.i(LOG_TAG, "Max Area: " + Double.toString(max_area));
    }
    else{
        Log.e(LOG_TAG, "No Contour Found!");
    }

    MatOfPoint2f newPoint = new MatOfPoint2f(contours.get(max_idx).toArray());

    return Imgproc.arcLength(newPoint, true);
}
 
Example 4
Source File: Contour.java    From FTCVision with MIT License 2 votes vote down vote up
/**
 * Get the arc length of the contour
 *
 * @param closed True if the contour should be calculated as closed
 * @return Arc length
 */
public double arcLength(boolean closed) {
    return Imgproc.arcLength(getDoubleData(), closed);
}