为什么使用OpenCv无法检测到最大轮廓?

时间:2019-01-30 13:56:47

标签: java android opencv image-processing opencv4android

我目前正在使用OpenCv开发一个android应用程序,该过程的第一步是从相机中检测并提取图像的最大轮廓。

我应用了Canny边缘检测器和findContours()函数,但是它无法正常工作,并且从未检测到真实轮廓。 我尝试使用Canny和阈值参数,并尝试消除图像二值化而没有任何运气。

这是原始图像:         https://i.imgur.com/gGYuDmy.png

,结果如下:         https://i.imgur.com/NxVx8Ru.png

用于检测和绘制轮廓的Java代码:

    private Mat findLargestRectangle_2(Mat original_image) {
    imgSource = new Mat(original_image.size(), CvType.CV_8UC1);
    //imgSource = original_image;

    //convert the image to black and white
    Imgproc.cvtColor(original_image, imgSource, Imgproc.COLOR_BGR2GRAY);

    Imgproc.equalizeHist(imgSource, imgSource);

    Imgproc.adaptiveThreshold(imgSource, imgSource, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 15, 40);

    //apply gaussian blur to smoothen lines of dots
    Imgproc.GaussianBlur(imgSource, imgSource, new Size(5, 5), 1);

    //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;
    int maxAreaIdx = -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();
        Mat largest_contour = contours.get(0);
        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;
                    maxAreaIdx = idx;
                    largest_contours.add(temp_contour);
                    largest_contour = 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(imgSource, 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(imgSource, 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(imgSource, 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(imgSource, 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(imgSource, new Point(p4.x, p4.y), 20, new Scalar(0, 255, 255), 5); //p1 is colored violet
        }
    }

    return imgSource;
}

我在这一过程中停留了几天,希望你们能帮助我找出问题所在。

0 个答案:

没有答案