簡體   English   中英

如何從相機預覽Android OpenCV中提取對象

[英]how to extract an object from the camera preview 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;
}

}

這是運行代碼時的示例。 如您所見,該卡現在由一個紅色矩形勾勒出輪廓,但是如何提取矩形中的內容,將其保存到墊子或位圖,然后退出相機呢? 概述卡的屏幕截圖

我沒有測試,但是您不能只使用...

return new Mat(mRgba, rect);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM