Hi,
i'm trying to redo this tutorial in Java... but it doesn't work. I've sometimes (i know it's weird) some errors when i'm truing to get the keypoints from the good matches, and the findHomography function didn't work, and the error message isn't very clear for me :
OpenCV Error: Assertion failed (npoints >= 0 && points2.checkVector(2) == npoints && points1.type() == points2.type()) in unknown function, file ..\..\..\src\opencv\modules\calib3d\src\fundam.cpp, line 1074
I realy hope someone will be able to help me... i'm a novice with OpenCV
Here is my code, sorry it's a bit long :
import java.util.LinkedList;
import java.util.List; import org.opencv.calib3d.Calib3d; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.core.MatOfDMatch; import org.opencv.core.MatOfKeyPoint; import org.opencv.core.MatOfPoint; import org.opencv.core.MatOfPoint2f; import org.opencv.core.Point; 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.highgui.Highgui;
class FindObject { public void run(String pathObject, String pathScene) {
System.out.println("\nRunning FindObject");
Mat img_object = Highgui.imread("D:/workspace/HelloCV/".concat(pathObject), 0); //0 = CV_LOAD_IMAGE_GRAYSCALE
Mat img_scene = Highgui.imread("D:/workspace/HelloCV/".concat(pathScene), 0);
FeatureDetector detector = FeatureDetector.create(4); //4 = SURF
MatOfKeyPoint keypoints_object = new MatOfKeyPoint();
MatOfKeyPoint keypoints_scene = new MatOfKeyPoint();
detector.detect(img_object, keypoints_object);
detector.detect(img_scene, keypoints_scene);
DescriptorExtractor extractor = DescriptorExtractor.create(2); //2 = SURF;
Mat descriptor_object = new Mat();
Mat descriptor_scene = new Mat() ;
extractor.compute(img_object, keypoints_object, descriptor_object);
extractor.compute(img_scene, keypoints_scene, descriptor_scene);
DescriptorMatcher matcher = DescriptorMatcher.create(1); // 1 = FLANNBASED
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;
}
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);
Mat img_matches = new Mat();
Features2d.drawMatches(
img_object,
keypoints_object,
img_scene,
keypoints_scene,
gm,
img_matches,
new Scalar(255,0,0),
new Scalar(0,0,255),
new MatOfByte(),
2);
MatOfPoint2f obj = new MatOfPoint2f();
MatOfPoint2f scene = new MatOfPoint2f();
//I've sometimes a bug here, depending of the pictures...
for(int i = 0; i<good_matches.size(); i++){
//Récupération des points clés des bonnes correspondances
obj.push_back(keypoints_object.row(good_matches.get(i).queryIdx));
scene.push_back(keypoints_object.row(good_matches.get(i).trainIdx));
}
//DON'T WORK :
//OpenCV Error: Assertion failed (npoints >= 0 && points2.checkVector(2) == npoints && points1.type() == points2.type()) in unknown function
Mat H = Calib3d.findHomography(obj, scene);
LinkedList<Point> cornerList = new LinkedList<Point>();
cornerList.add(new Point(0,0));
cornerList.add(new Point(img_object.cols(),0));
cornerList.add(new Point(img_object.cols(),img_object.rows()));
cornerList.add(new Point(0,img_object.rows()));
MatOfPoint obj_corners = new MatOfPoint();
obj_corners.fromList(cornerList);
MatOfPoint scene_corners = new MatOfPoint();
//Later... when findHomoraphy will work
//Core.perspectiveTransform(obj_corners, scene_corners, H);
//Green Lines on the pictures... later
/*
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4); //de 1 à 2
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4); //de 2 à 3
Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4); //de 3 à 0
*/
String filename = "result.png";
System.out.println(String.format("Writing %s", filename));
Highgui.imwrite(filename, img_matches);
} }
public class Finder { public static void main(String[] args) { // Load the native library. System.loadLibrary("opencv_java244");
new FindObject().run("objectclean.jpg", "scene2clean.jpg");
} }