本文介绍了Android OpenCV纸张检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这个问题是之前提出的,但是我没有找到解决问题的样本或解决方案.我是opencv的新手,我想使用OpenCV CameraPreview进行纸张检测.在我的示例应用程序中,我将opencv 3.0.0与静态初始化一起使用.我了解对象识别可以通过以下步骤完成:

I think this question is asked before but i didnt find a sample or solution for my problem. I am new to opencv and i want to use the OpenCV CameraPreview for paper sheet detection. In my sample app i use opencv 3.0.0 with static initialization.I understand that object recognition can done with these steps:

  1. 将输入图像设为Canny
  2. 模糊Canny图片
  3. 在模糊的Canny图像上找到轮廓
  4. 搜索矩形等
  5. 绘制线条或用半透明颜色填充矩形

我的问题是我现在无法对图像进行模糊处理,但是我不知道如何找到轮廓和矩形并用半透明颜色填充它们.

My problem is now that i can canny and blur the image but i dont know how to find contours and rectangles and fill them with a half transparent color.

这是我当前的onCameraFrame函数:

Here is my current onCameraFrame Function:

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    Mat input = inputFrame.rgba();
    Mat output = input.clone();
    Imgproc.Canny(input, output, 50, 50);
    Imgproc.blur(output, output,new Size(5,5));
    //Find Contours
    //Search for biggest Contour/Rectangle
    //Fill Rectangle with half transparent Color
    return output;
}

有人可以帮助我解决纸张检测问题,并且有适用于android/java的代码示例吗?谢谢

Can anybody help me to solve the problem of paper sheet detection and has a code sample for android/java?Thank you

推荐答案

以下代码来自打开便笺扫​​描器我正在开发的应用程序,您可以使用它来查找更多信息.

The following code is from the Open Note Scanner app I am developing, you can use it to look for more information.

函数findDocument将返回一个四边形对象,该对象封装了带有轮廓的MatOfPoint和带有单个点的Point [].您可以调用它,并使用返回的对象调用Imgproc.drawContours()完成图像.

the function findDocument will return a Quadrilateral object that encapsulates a MatOfPoint with the contour and a Point[] with individual points. You can call it and with the returned object call the Imgproc.drawContours() to finish your image.

所有代码均以来自pyimagesearch的出色教程

注意:这是从我的代码中快速移植方法的过程,它没有语法错误,但是我没有对其进行测试.

NOTICE: this was a quick transplant of the methods from my code, it doesn't have syntax errors but I didn't tested it.

package com.todobom.opennotescanner.views;

import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

public class detectDocument {

    /**
     *  Object that encapsulates the contour and 4 points that makes the larger
     *  rectangle on the image
     */
    public static class Quadrilateral {
        public MatOfPoint contour;
        public Point[] points;

        public Quadrilateral(MatOfPoint contour, Point[] points) {
            this.contour = contour;
            this.points = points;
        }
    }

    public static Quadrilateral findDocument( Mat inputRgba ) {
        ArrayList<MatOfPoint> contours = findContours(inputRgba);
        Quadrilateral quad = getQuadrilateral(contours);
        return quad;
    }

    private static ArrayList<MatOfPoint> findContours(Mat src) {

        double ratio = src.size().height / 500;
        int height = Double.valueOf(src.size().height / ratio).intValue();
        int width = Double.valueOf(src.size().width / ratio).intValue();
        Size size = new Size(width,height);

        Mat resizedImage = new Mat(size, CvType.CV_8UC4);
        Mat grayImage = new Mat(size, CvType.CV_8UC4);
        Mat cannedImage = new Mat(size, CvType.CV_8UC1);

        Imgproc.resize(src,resizedImage,size);
        Imgproc.cvtColor(resizedImage, grayImage, Imgproc.COLOR_RGBA2GRAY, 4);
        Imgproc.GaussianBlur(grayImage, grayImage, new Size(5, 5), 0);
        Imgproc.Canny(grayImage, cannedImage, 75, 200);

        ArrayList<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Mat hierarchy = new Mat();

        Imgproc.findContours(cannedImage, contours, hierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

        hierarchy.release();

        Collections.sort(contours, new Comparator<MatOfPoint>() {

            @Override
            public int compare(MatOfPoint lhs, MatOfPoint rhs) {
                return Double.valueOf(Imgproc.contourArea(rhs)).compareTo(Imgproc.contourArea(lhs));
            }
        });

        resizedImage.release();
        grayImage.release();
        cannedImage.release();

        return contours;
    }

    private static Quadrilateral getQuadrilateral(ArrayList<MatOfPoint> contours) {

        for ( MatOfPoint c: contours ) {
            MatOfPoint2f c2f = new MatOfPoint2f(c.toArray());
            double peri = Imgproc.arcLength(c2f, true);
            MatOfPoint2f approx = new MatOfPoint2f();
            Imgproc.approxPolyDP(c2f, approx, 0.02 * peri, true);

            Point[] points = approx.toArray();

            // select biggest 4 angles polygon
            if (points.length == 4) {
                Point[] foundPoints = sortPoints(points);

                return new Quadrilateral(c, foundPoints);
            }
        }

        return null;
    }

    private static Point[] sortPoints(Point[] src) {

        ArrayList<Point> srcPoints = new ArrayList<>(Arrays.asList(src));

        Point[] result = { null , null , null , null };

        Comparator<Point> sumComparator = new Comparator<Point>() {
            @Override
            public int compare(Point lhs, Point rhs) {
                return Double.valueOf(lhs.y + lhs.x).compareTo(rhs.y + rhs.x);
            }
        };

        Comparator<Point> diffComparator = new Comparator<Point>() {

            @Override
            public int compare(Point lhs, Point rhs) {
                return Double.valueOf(lhs.y - lhs.x).compareTo(rhs.y - rhs.x);
            }
        };

        // top-left corner = minimal sum
        result[0] = Collections.min(srcPoints, sumComparator);

        // bottom-right corner = maximal sum
        result[2] = Collections.max(srcPoints, sumComparator);

        // top-right corner = minimal diference
        result[1] = Collections.min(srcPoints, diffComparator);

        // bottom-left corner = maximal diference
        result[3] = Collections.max(srcPoints, diffComparator);

        return result;
    }

}

这篇关于Android OpenCV纸张检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 21:25