如何使用Android从Opencv相机预览中提取轮廓?

时间:2019-02-25 09:38:51

标签: java android opencv image-processing opencv4android

我正在使用Opencv开发一个Android应用程序,其主要思想是比较从画廊图像中检测到的对象与使用Opencv预览相机检测到的帧。 现在,我能够从输入图像中检测最大轮廓和不同对象,但是我需要像实时一样使用onCameraFrame方法进行这项工作。

这是最大的轮廓检测方法:

  private Mat findLargestRectangle_2(Mat original_image) {
    imgSource = new Mat(original_image.size(), CvType.CV_8UC1);
    //convert the image to black and white
    Imgproc.cvtColor(original_image, imgSource, Imgproc.COLOR_BGR2GRAY);

    final Point anchor = new Point(-1, -1);
    final int iterations = 2;

    Imgproc.erode(imgSource, imgSource, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3,3)), anchor, iterations);
    Imgproc.dilate(imgSource, imgSource, Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3,3)), anchor, iterations);

    //convert the image to black and white does (8 bit)
    Imgproc.Canny(imgSource, imgSource, 100, 200, 3, false);

    //find the contours
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_NONE);

    double maxArea = -1;

    if (contours.size() > 0) {
        MatOfPoint temp_contour = contours.get(0); //the largest is at the index 0 for starting point
        MatOfPoint2f approxCurve = new MatOfPoint2f();
        MatOfPoint2f maxCurve = new MatOfPoint2f();
        List<MatOfPoint> largest_contours = new ArrayList<MatOfPoint>();
        for (int idx = 0; idx < contours.size(); idx++) {
            temp_contour = contours.get(idx);
            double contourarea = Imgproc.contourArea(temp_contour);
            //compare this contour to the previous largest contour found
            if (contourarea > maxArea) {
                //check if this contour is a square
                MatOfPoint2f new_mat = new MatOfPoint2f( temp_contour.toArray() );
                int contourSize = (int)temp_contour.total();
                Imgproc.approxPolyDP(new_mat, approxCurve, contourSize*0.05, true);
                if (approxCurve.total() == 4) {
                    maxArea = contourarea;
                    maxCurve = approxCurve;
                    largest_contours.add(temp_contour);
                }
            }
        }

        if (largest_contours.size() >= 1) {
            MatOfPoint temp_largest = largest_contours.get(largest_contours.size()-1);
            largest_contours = new ArrayList<MatOfPoint>();
            largest_contours.add(temp_largest);
        }

        Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BayerBG2RGB);
        Imgproc.drawContours(original_image, largest_contours, -1, new Scalar(0, 255, 0), 1);

        double temp_double[] = maxCurve.get(0, 0);
        if(temp_double != null) {
            Point p1 = new Point(temp_double[0], temp_double[1]);
            Imgproc.circle(original_image, new Point(p1.x, p1.y), 20, new Scalar(255, 0, 0), 5); //p1 is colored red
        }

        temp_double = maxCurve.get(1, 0);

        if(temp_double != null) {

            Point p2 = new Point(temp_double[0], temp_double[1]);
            Imgproc.circle(original_image, new Point(p2.x, p2.y), 20, new Scalar(0, 255, 0), 5); //p2 is colored green
        }

        temp_double = maxCurve.get(2, 0);

        if(temp_double != null) {
            Point p3 = new Point(temp_double[0], temp_double[1]);
            Imgproc.circle(original_image, new Point(p3.x, p3.y), 20, new Scalar(0, 0, 255), 5); //p3 is colored blue
        }

        temp_double = maxCurve.get(3, 0);

        if(temp_double != null) {
            Point p4 = new Point(temp_double[0], temp_double[1]);
            Imgproc.circle(original_image, new Point(p4.x, p4.y), 20, new Scalar(0, 255, 255), 5); //p1 is colored violet
        }
    }

    return original_image;
}

问题是,当我在onCameraFrame中调用此方法时,绘制的轮廓不断变化,因此我无法执行任何进一步的处理,例如检测多个对象并将它们与参考图像进行比较。

我该如何在帧之间停止或延迟时间,以使应用程序有一些时间来处理每个帧,请问你们这是否是进行此类工作的正确方法?

0 个答案:

没有答案