Assertion error with Android opencv when using findHomography
I'm working on a Android app with Opencv for Android. I'm using ORB detector and bruteforce matcher to find a features from image one in a input frame. This goes fine (i guess) next i want to draw a box around the found features in the input frame. But this goes wrong, it goes wrong at the findHomography call. But i don't know why, in the LogCat is says the following assertion error after a few frames:
12-03 15:34:42.690: E/AndroidRuntime(22063): CvException [org.opencv.core.CvException: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/calib3d/src/fundam.cpp:235: error: (-215) count >= 4 in function int cvFindHomography(const CvMat*, const CvMat*, CvMat*, int, double, CvMat*)
The Android opencv code is as followes (edited with new code):
TemplateImageTemp = new Mat();
InputImageTemp = new Mat();
InputImage = new Mat();
TemplateImage = new Mat();
// input frame has RGBA format
InputImage = inputFrame.rgba();
File ImagePath = new File(Environment.getExternalStorageDirectory(), "lena.png");
TemplateImage = Highgui.imread(ImagePath.getAbsolutePath());
Log.i(TAG, ImagePath.getAbsolutePath());
if(TemplateImage.empty()){
Log.i(TAG, "========================= Empty ========================");
}else
{
Log.i(TAG, "========================= Loaded! ========================");
}
Imgproc.cvtColor(InputImage, InputImageTemp, Imgproc.COLOR_RGBA2RGB);
Imgproc.cvtColor(TemplateImage ,TemplateImageTemp , Imgproc.COLOR_RGBA2RGB);
// ORB detector and matcher
FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE);
MatOfKeyPoint keypoints_object = new MatOfKeyPoint();
MatOfKeyPoint keypoints_scene = new MatOfKeyPoint();
detector.detect(InputImageTemp, keypoints_object);
detector.detect(TemplateImageTemp, keypoints_scene);
Mat descriptor_object = new Mat();
Mat descriptor_scene = new Mat() ;
extractor.compute(InputImageTemp, keypoints_object, descriptor_object);
extractor.compute(TemplateImageTemp, keypoints_scene, descriptor_scene);
MatOfDMatch matches = new MatOfDMatch();
matcher.match(descriptor_object, descriptor_scene, matches);
List<DMatch> matchesList = matches.toList();
Double max_dist = 0.0;
Double min_dist = 100.0;
for(int i = 0; i < descriptor_object.rows(); i++){
Double dist = (double) matchesList.get(i).distance;
if(dist < min_dist) min_dist = dist;
if(dist > max_dist) max_dist = dist;
}
System.out.println("-- Max dist : " + max_dist);
System.out.println("-- Min dist : " + min_dist);
LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
MatOfDMatch gm = new MatOfDMatch();
for(int i = 0; i < descriptor_object.rows(); i++){
if(matchesList.get(i).distance < 3*min_dist){
good_matches.addLast(matchesList.get(i));
}
}
gm.fromList(good_matches);
TempImage = InputImageTemp;
System.out.println("==== 1 ====");
LinkedList<Point> objList = new LinkedList<Point>();
LinkedList<Point> sceneList = new LinkedList<Point>();
List<KeyPoint> keypoints_objectList = keypoints_object.toList();
List<KeyPoint> keypoints_sceneList = keypoints_scene.toList();
for(int i = 0; i<good_matches.size(); i++){
objList.addLast(keypoints_objectList.get(good_matches.get(i).queryIdx).pt);
sceneList.addLast(keypoints_sceneList.get(good_matches.get(i).trainIdx).pt);
}
MatOfPoint2f obj = new MatOfPoint2f();
obj.fromList(objList);
MatOfPoint2f scene = new MatOfPoint2f();
scene.fromList(sceneList);
Mat H = Calib3d.findHomography(obj, scene);
Mat obj_corners = new Mat(4,1,CvType.CV_32FC2);
Mat scene_corners = new Mat(4,1,CvType.CV_32FC2);
System.out.println("==== 4 ====");
obj_corners.put(0, 0, new double[] {0,0});
obj_corners.put(1, 0, new double[] {TemplateImage.cols(),0});
obj_corners.put(2, 0, new double[] {TemplateImage.cols(),TemplateImage.rows()});
obj_corners.put(3, 0, new double[] {0,TemplateImage.rows()});
System.out.println("==== 5 ====");
Core.perspectiveTransform(obj_corners,scene_corners, H);
System.out.println("==== 6 ====");
Core.line(TempImage, new Point(scene_corners.get(0,0)), new Point(scene_corners.get(1,0)), new Scalar(0, 255, 0),4);
Core.line(TempImage, new Point(scene_corners.get(1,0)), new Point(scene_corners.get(2,0)), new Scalar(0, 255, 0),4);
Core.line(TempImage, new Point(scene_corners.get(2,0)), new Point(scene_corners.get(3,0)), new Scalar(0, 255, 0),4);
Core.line(TempImage, new Point(scene_corners.get(3,0)), new Point(scene_corners.get(0,0)), new Scalar(0, 255, 0),4);
System.out.print("Number of good matches: ");
System.out.println (good_matches.size());
OutputImage = TempImage;
System.out.println("==== 8 ====");
System.out.print("Cols image out: ");
System.out.println (OutputImage.cols());
System.out.print("Rows image out: ");
System.out.println (OutputImage.rows());
System.out.print("Type image out: ");
System.out.println (OutputImage.type());
break;
Does anybody have a idea or suggestion? All feadback is welcome!
You have to check if obj
and scene
lists have 4 or more elements otherwise findHomography will fail. 4 points is the minimum needed to estimate an homography.
if(obj.size()>=4 && scene.size()>=4){
Mat H = Calib3d.findHomography(obj, scene);
}
链接地址: http://www.djcxy.com/p/35410.html