Ask Your Question
5

OpenCV (java) : autocapture image in onCameraFrame()

asked 2018-10-31 04:11:06 -0500

Akhil Patel gravatar image

I am working on OpenCV CameraBridgeViewBase in android. so when application started and onCameraFrame called than image is auto captured so i don't know how to auto-capture because onCameraFrame is continuous called so when i trying to save bitmap on rectangle detection it's take some delay like 20 to 30 second. i want image capture when rectangle detected. Here is my code.

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    final Mat imgSource = inputFrame.rgba();

   rotateandscalebitmap = Bitmap.createBitmap(imgSource.cols(), imgSource.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(imgSource, rotateandscalebitmap);


    Mat imageHSV = new Mat(imgSource.size(), CvType.CV_8UC4);
    Mat imageBlurr = new Mat(imgSource.size(), CvType.CV_8UC4);
    Mat imageA = new Mat(imgSource.size(), CvType.CV_32F);
    Imgproc.cvtColor(imgSource, imageHSV, Imgproc.COLOR_BGR2GRAY);
    Imgproc.GaussianBlur(imageHSV, imageBlurr, new Size(5, 5), 0);

    Imgproc.adaptiveThreshold(imageBlurr, imageA, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 7, 5);

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Imgproc.findContours(imageA, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

    Vector<Mat> rectangles = new Vector<Mat>();
    Rect rect = null;
    for (int i = 0; i < contours.size(); i++)
    {
        if (Imgproc.contourArea(contours.get(i)) > 50) {
            rect = Imgproc.boundingRect(contours.get(i));
            if ((rect.height > 30 && rect.height < 400) && (rect.width > 400 && rect.width < 800)) {
                Rect rec = new Rect(rect.x, rect.y, rect.width, rect.height);
                rectangles.add(new Mat(imgSource, rec));
                Imgproc.rectangle(imgSource, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0), 4);

                // Imgproc.findContours(imageA, contours, new Mat(), Imgproc.RETR_EXTERNAL,Imgproc.CHAIN_APPROX_SIMPLE);
            }
        }
    }
    return imgSource;
}
edit retag flag offensive close merge delete

Comments

use imwrite() to save your Mat, don't convert to Bitmap first (much faster / more efficient)

berak gravatar imageberak ( 2018-10-31 04:20:38 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
6

answered 2018-12-04 01:59:57 -0500

Akhil Patel gravatar image

updated 2018-12-05 23:23:38 -0500

thank you droidcv it's working.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

11 followers

Stats

Asked: 2018-10-31 04:11:06 -0500

Seen: 163 times

Last updated: Dec 05