如何从相机预览android opencv中提取对象

时间:2017-11-28 20:06:47

标签: java android opencv

我正在尝试使用Android上的OpenCV扫描MTG卡。我有它可以检测卡的边缘,甚至在它周围画一个轮廓,但我很困惑如何从背景中提取卡,然后退出相机预览。到目前为止,这是我的代码:

public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
    Mat result = new Mat();
    Mat mask = new Mat();
    List<MatOfPoint> contours = new ArrayList<>();
    Mat hierarchy = new Mat();
    mRgba = inputFrame.rgba();
    Imgproc.Canny(mRgba, result, 40, 120);
    Imgproc.GaussianBlur(result, result, new Size(9,9), 2, 2);
    Imgproc.findContours(result, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE, new Point(0,0));
    Imgproc.drawContours(mask, contours, -1, new Scalar(0, 255, 0), 1);
    hierarchy.release();

    for ( int contourIdx=0; contourIdx < contours.size(); contourIdx++ )
    {
        // Minimum size allowed for consideration
        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);

        Imgproc.rectangle(mRgba, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(255, 0, 0, 255), 3);
    }
    Bitmap card = Bitmap.createBitmap(result.cols(), result.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(result, card);
    return mRgba;
}

}

以下是运行代码时的示例。正如您所看到的那样,卡片现在被红色矩形勾勒出来,但是如何在矩形中提取它,将其保存到垫子或位图然后退出相机? screenshot of outlined card

1 个答案:

答案 0 :(得分:0)

我没有测试过,但你不能用......

|id|username|password|rank  |
|1 | jeff   | Passwor| admin|
|2 | bob    | passow | user |