1

嗨,我几天前开始使用 JavaCv,我看过这个用 c++ 编写的教程,我正在尝试在 java 中对其进行调整,所以我的代码如下:

    Mat source_image = Imgcodecs.imread(ocrReadFrom);
    Mat gray_image = new Mat();
    Mat blur_image = new Mat();
    Mat threshold = new Mat();
    Mat result = new Mat();

    cvtColor(source_image, gray_image, opencv_imgproc.CV_BGR2GRAY);
    medianBlur(gray_image, blur_image, 3);
    adaptiveThreshold(blur_image, threshold, 255, 1, 1, 11, 2);
    Mat element = getStructuringElement(MORPH_ELLIPSE, new Size(3, 3), new Point(1, 1));
    morphologyEx(threshold, result, MORPH_CLOSE, element);

    //-----------------------------------------------------------------------
    ArrayList< MatOfPoint> contours = new ArrayList<>();
    Mat hierarchy = new Mat();
    findContours(result, contours, hierarchy, opencv_imgproc.CV_RETR_CCOMP,
            opencv_imgproc.CV_CHAIN_APPROX_SIMPLE);
    int i = 0;
    for (MatOfPoint contour : contours) {
        System.out.println("processing"+i);
        Rect r = Imgproc.boundingRect(contour);
        double area0 = contourArea(contour);
        if (area0 < 120) {
            drawContours(threshold, contours, i, new Scalar(0, 255, 0), 1, opencv_imgproc.CV_FILLED, hierarchy, 8, new Point(1, 1));
            continue;
        }
        i++;
    }

    //------------------------------------------------------------------------
    Imgcodecs.imwrite("contours22.jpg", threshold);
    Imgcodecs.imwrite("contours2.jpg", result);

当我启动此代码时,我得到

            processing0
            processing1
            processing2
            OpenCV Error: Assertion failed (connectivity == 8 || connectivity == 4) in cv::LineIterator::LineIterator, file C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\imgproc\src\drawing.cpp, line 168
            error: cv::Exception: C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\imgproc\src\drawing.cpp:168: error: (-215) connectivity == 8 || connectivity == 4 in function cv::LineIterator::LineIterator         

我应该怎么办 ????

4

0 回答 0