I'm really new to openCV and I followed this and I dont know if what I did is right but every time there is an openCV Assertion Error in either matcher.match(train, query, match); or in Core.findHomography(object, scene, homograph); some times the application crashes and fails. Am I doing it right, what might be the cause for this problem?
Here is my code
package com.example.objectrecognition;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.calib3d.Calib3d;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.Point;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Scalar;
import org.opencv.features2d.DMatch;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.DescriptorMatcher;
import org.opencv.features2d.FeatureDetector;
import org.opencv.features2d.Features2d;
import org.opencv.features2d.KeyPoint;
import org.opencv.imgproc.Imgproc;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity implements CvCameraViewListener2 {
private static final String TAG = "OCVSample::Activity";
private CameraBridgeViewBase mOpenCvCameraView;
private Mat mRgba;
private Mat mRef;
private Mat mGray;
private Mat mBuff;
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this){
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded successfully");
mOpenCvCameraView.enableView();
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "called onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.MainActivity);
mOpenCvCameraView.setCvCameraViewListener(this);
}
@Override
public void onPause()
{
super.onPause();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
@Override
public void onResume()
{
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5, this, mLoaderCallback);
}
public void onDestroy() {
super.onDestroy();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
public void onCameraViewStarted(int width, int height) {
mRgba = new Mat();
mRef = new Mat();
mGray = new Mat();
mBuff = new Mat();
try {
mBuff = Utils.loadResource(this, R.drawable.dartlogo);
} catch (IOException e) {
e.printStackTrace();
}
Imgproc.cvtColor(mBuff, mRef, Imgproc.COLOR_RGBA2GRAY);
}
public void onCameraViewStopped(){
if (mRgba != null)
mRgba.release();
if (mGray != null)
mGray.release();
if (mRef != null)
mRef.release();
if (mBuff != null)
mBuff.release();
mRgba = null;
mGray = null;
mRef = null;
mBuff = null;
}
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
mGray = inputFrame.gray();
MatOfKeyPoint keypointsRef = new MatOfKeyPoint();
MatOfKeyPoint keypoints = new MatOfKeyPoint();
Mat extractRef = new Mat();
Mat extract = new Mat();
MatOfDMatch matchs = new MatOfDMatch();
FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
detector.detect(mGray, keypoints);
detector.detect(mRef, keypointsRef);
extractor.compute(mGray, keypoints, extract);
extractor.compute(mRef, keypointsRef, extractRef);
matcher.match(extractRef, extract, matchs);
List<DMatch> matchesList = matchs.toList();
Double max_dist = 0.0;
Double min_dist = 100.0;
for (int i = 0; i < matchesList.size(); i++) {
Double dist = (double) matchesList.get(i).distance;
if (dist < min_dist)
min_dist = dist;
if (dist > max_dist)
max_dist = dist;
}
LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
MatOfDMatch gm = new MatOfDMatch();
for (int i = 0; i < matchesList.size(); i++) {
if (matchesList.get(i).distance <= (3 * min_dist)) {
good_matches.addLast(matchesList.get(i));
}
}
gm.fromList(good_matches);
Features2d.drawMatches(mRef, keypointsRef, mGray,
keypoints, gm, mRgba, new Scalar(255, 0, 0),
new Scalar(0, 0, 255), new MatOfByte(), 2);
LinkedList<Point> objList = new LinkedList<Point>();
LinkedList<Point> sceneList = new LinkedList<Point>();
List<KeyPoint> keypoints_RefList = keypointsRef.toList();
List<KeyPoint> keypoints_List = keypoints.toList();
for (int i = 0; i < good_matches.size(); i++) {
objList.addLast(keypoints_RefList.get(good_matches.get(i).queryIdx).pt);
sceneList.addLast(keypoints_List.get(good_matches.get(i).trainIdx).pt);
}
MatOfPoint2f obj = new MatOfPoint2f();
MatOfPoint2f scene = new MatOfPoint2f();
obj.fromList(objList);
scene.fromList(sceneList);
Mat hg = Calib3d.findHomography(obj, scene, 8, 2, new Mat());
Mat obj_corners = new Mat(4,1,CvType.CV_32FC2);
Mat scene_corners = new Mat(4,1,CvType.CV_32FC2);
obj_corners.put(0, 0, new double[] {0, 0});
obj_corners.put(1, 0, new double[] {mRef.cols(), 0});
obj_corners.put(2, 0, new double[] {mRef.cols(), mRef.rows()});
obj_corners.put(3, 0, new double[] {0, mRef.rows()});
Core.perspectiveTransform(obj_corners, scene_corners, hg);
Core.line(mRgba, new Point(scene_corners.get(0,0)), new Point(scene_corners.get(1,0)), new Scalar(0, 255, 0),3);
Core.line(mRgba, new Point(scene_corners.get(1,0)), new Point(scene_corners.get(2,0)), new Scalar(0, 255, 0),3);
Core.line(mRgba, new Point(scene_corners.get(2,0)), new Point(scene_corners.get(3,0)), new Scalar(0, 255, 0),3);
Core.line(mRgba, new Point(scene_corners.get(3,0)), new Point(scene_corners.get(0,0)), new Scalar(0, 255, 0),3);
keypointsRef.release();
keypoints.release();
extractRef.release();
extract.release();
matchs.release();
keypointsRef = null;
keypoints = null;
extractRef = null;
extract = null;
matchs = null;
return mRgba;
}
}
Any other documentation or reference will be useful. Thanx in advance.