气泡板多选扫描

时间:2017-04-21 16:08:03

标签: opencv

这是我在py下重写的代码,但对此我不明白该怎么做。请帮我。或告诉我该怎么做。我不明白py变量questionCnts添加到做什么。切换到java时,用什么函数来替换contours.sort_contours函数,我查找了java但没有找到任何与此函数等效的函数。

这是我想拍的照片:

enter image here MY IMAGE SRC 1

这是我的结果:

MY RESULT

    private void scanImage(String selectImagePath) {
    Mat img = Imgcodecs.imread(selectImagePath);
    if (img.empty()) {
        Log.d(TAG, "Empty Image");
        showMgs("empty");
    }else{
        Log.d(TAG, "onClick: ");

    }

    ArrayList<MatOfPoint> contours = findContours(img);
    Quadrilateral quad = getQuadrilateral(contours, img.size());
    if(quad !=null) {
        showMgs("succes");
        Mat doc;
        doc = fourPointTransform(img, quad.points);
        Mat thresh = new Mat();
        Mat grayImage = new Mat();
        Imgproc.cvtColor(doc, grayImage, Imgproc.COLOR_RGBA2GRAY, 4);
        Imgproc.threshold(grayImage, thresh, 0, 255, Imgproc.THRESH_BINARY_INV | Imgproc.THRESH_OTSU);

        contours = new ArrayList<MatOfPoint>();
        ArrayList<Point> questionCnts = new ArrayList<Point>();
        Mat hierarchy = new Mat();
        Imgproc.findContours(thresh, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0, 0));
        hierarchy.release();
        for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++) {

            MatOfPoint2f approxCurve = new MatOfPoint2f();
            MatOfPoint2f contour2f = new MatOfPoint2f(contours.get(contourIdx).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);
            float ar = rect.width /(float)rect.height;
                if (rect.width >= 20 && rect.height >= 20 && ar >= 0.9 && ar <= 1.1) {
                    Imgproc.rectangle(doc, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(255, 0, 0, 255), 2);
          //here i don't known 
                }
        }
            showImage(doc);
        }else{
            showMgs("not succes");
        }
}

code py convert。

cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,;
cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
questionCnts = []
for c in cnts:
# compute the bounding box of the contour, then use the
# bounding box to derive the aspect ratio
(x, y, w, h) = cv2.boundingRect(c)
ar = w / float(h)

# in order to label the contour as a question, region
# should be sufficiently wide, sufficiently tall, and
# have an aspect ratio approximately equal to 1
if w >= 20 and h >= 20 and ar >= 0.9 and ar <= 1.1:
    questionCnts.append(c)  # i don't known here
questionCnts = contours.sort_contours(questionCnts,
method="top-to-bottom")[0]

正确= 0

0 个答案:

没有答案