Ask Your Question
-1

How to detect and process objects using Android and Opencv in real time

asked 2019-02-26 07:42:15 -0600

kermit gravatar image

I am developing and Android application using Opencv to detect the biggest contour, apply segmentation and compare each objects to a reference image but in real time. For now I have done the biggest contour detection using Canny edge detector, the segmentation using Canny and the objects matching using ORB but using static images. I tried a simple contour detection under onCameraFrame(), the time elapsed to process is 0.004 seconds but the drawn contour keeps changing so I can't perform segmentation and image matching on a false contour noting that this program will be used in industrial chain . Here is the ddetection code:

long e1 = Core.getTickCount();
    Mat mGray = new Mat();
    Imgproc.cvtColor(origMat, mGray, Imgproc.COLOR_BGR2GRAY);
    Imgproc.GaussianBlur(mGray, mGray, new Size(5, 5), 5);
    Imgproc.Canny(mGray, mGray, 50, 150, 3, false);
    Mat kernell = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(9,9));
    Imgproc.morphologyEx(mGray, mGray, Imgproc.MORPH_CLOSE, kernell);

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Mat hierarchy = new Mat();
    Imgproc.findContours(mGray, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
    MatOfPoint2f approxCurve = new MatOfPoint2f();

    for (int idx = 0; idx < contours.size() ; idx++) {
        MatOfPoint2f contour2f = new MatOfPoint2f( contours.get(idx).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);

        // draw enclosing rectangle (all same color, but you could use variable i to make them unique)
        Imgproc.rectangle(origMat, rect.tl(), rect.br(), new Scalar(0, 255, 0));
    }

    long e2 = Core.getTickCount();

    long e = e2 - e1;
    double time = e / Core.getTickFrequency();

    Toast.makeText(this, "" + time, Toast.LENGTH_LONG).show();
edit retag flag offensive close merge delete

Comments

and the objects matching using ORB

if you really wanted to do detection of arbitrary "objects" like that -- you're on the wrong bus. (you probably read some silly blogpost about it)

have a look here instead, most likely you'll have to learn about dnn's and how to retrain those.

berak gravatar imageberak ( 2019-02-26 07:47:16 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-02-26 07:56:05 -0600

kermit gravatar image

updated 2019-02-26 07:58:57 -0600

Thank you sir for your suggestion, and yes I am thinking of changing my approach to Deep Learning.But my problem for now is running my code in Opencv preview camera and perform image processing in real time. And objects aren't arbitrary, I have a specific image I'll be working on, it's a parfum coffret with 3 objects in it.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-02-26 07:42:15 -0600

Seen: 990 times

Last updated: Feb 26 '19