如何减少使用Opencv检测边界矩形的时间?

时间:2019-02-22 10:37:07

标签: java android opencv image-processing opencv4android

我正在开发一个Android应用程序,以使用Android和Opencv检测图像的边界矩形,然后应用ORB查找与其他图像的相似性。

这是我要做的:

   for (int i = contours.size() - 1 ; i >= 0 ; i--) {
        double area= Imgproc.contourArea(contours.get(i),false);  //  Find the area of contour
        if(area<min_area)
            contours.remove(i);
    }

  private void process_detect_edges(Bitmap imageToProcess) {
    Mat mRGBA = new Mat();
    imageToProcess = getResizedBitmap(imageToProcess, 500);
    Utils.bitmapToMat(imageToProcess, mRGBA);

    Mat mGray = new Mat();
    Imgproc.cvtColor(mRGBA, mGray, Imgproc.COLOR_BGR2GRAY);
    Imgproc.GaussianBlur(mGray, mGray, new Size(3, 3), 2);
    Imgproc.Canny(mGray, mGray, 20, 80, 3, false);
    Mat kernell = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(9,9));
    Imgproc.morphologyEx(mGray, mGray, Imgproc.MORPH_CLOSE, kernell);

    //find the contours
    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Mat hierarchy = new Mat();
    Imgproc.findContours(mGray, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

    for (int i = contours.size() - 1 ; i >= 0 ; i--) {
        double area= Imgproc.contourArea(contours.get(i),false);  //  Find the area of contour
        if(area<min_area)
            contours.remove(i);
    }
    MatOfPoint2f approxCurve = new MatOfPoint2f();
    for (int idx = 0; idx < contours.size() ; idx++) {

        //Convert contours(i) from MatOfPoint to MatOfPoint2f
        MatOfPoint2f contour2f = new MatOfPoint2f( contours.get(idx).toArray() );
        //Processing on mMOP2f1 which is in type MatOfPoint2f
        double approxDistance = Imgproc.arcLength(contour2f, true)*0.02;
        Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);

        //Convert back to MatOfPoint
        MatOfPoint points = new MatOfPoint( approxCurve.toArray() );

        // Get bounding rect of contour
        Rect rect = Imgproc.boundingRect(points);
        rectList.add(rect);
        Mat miniature = new Mat(mRGBA, new Rect(rect.tl(), rect.br()));
        mats.add(miniature);

        // draw enclosing rectangle (all same color, but you could use variable i to make them unique)
        Imgproc.rectangle(mRGBA, rect.tl(), rect.br(), new Scalar(0, 255, 0));
    }

我当前面临的问题是应用程序找到那些边界矩形所需的时间有点长(最多2秒)

如何减少处理图像所需的时间少于1秒?

编辑:

我尝试仅在imageView中设置预处理的位图,而不在其上应用任何Opencv函数,这花费了相同的时间,因此我认为主要问题不在图像处理中,而是在加载位图以进行处理来自上一个活动。

0 个答案:

没有答案