How to detect and process objects using Android and Opencv in real time
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();
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.