findHomography - The line come out not around object
I'm doing feature matching. For now , I can find features and draw descriptor but the line came out was not around the object even not a rectangle , I stuck on it for few days. Thanks for the help. here is my resoult
private void Featrue_found(){
MatOfKeyPoint templateKeypoints = new MatOfKeyPoint();
MatOfKeyPoint keypoints = new MatOfKeyPoint();
MatOfDMatch matches = new MatOfDMatch();
Object = new Mat(CvType.CV_32FC2);
Object = Highgui.imread(Environment.getExternalStorageDirectory()+ "/Android/data/" + getApplicationContext().getPackageName() + "/Files/Object.jpg", Highgui.CV_LOAD_IMAGE_UNCHANGED);
Resource = new Mat(CvType.CV_32FC2);
Resource = Highgui.imread(Environment.getExternalStorageDirectory()+ "/Android/data/" + getApplicationContext().getPackageName() + "/Files/Resource.jpg", Highgui.CV_LOAD_IMAGE_UNCHANGED);
Mat imageOut = Resource.clone();
//Don't use "FeatureDetector.FAST" result was too bad
//"FeatureDetector.SURF"、"FeatureDetector.SURF" non-free method
//"FeatureDetector.ORB" is better for now
//FeatureDetector.SIFT cna't use
FeatureDetector myFeatures = FeatureDetector.create(FeatureDetector.ORB);
myFeatures.detect(Resource, keypoints);
myFeatures.detect(Object, templateKeypoints);
DescriptorExtractor Extractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
Mat descriptors1 = new Mat();
Mat descriptors2 = new Mat();
Extractor.compute(Resource, keypoints, descriptors1);
Extractor.compute(Resource, templateKeypoints, descriptors2);
//add Feature descriptors
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE);
matcher.match(descriptors1, descriptors2, matches);
List<DMatch> matches_list = matches.toList();
MatOfDMatch good_matches = new MatOfDMatch();
double max_dist = 0; double min_dist = 99;
//-- Quick calculation of max and min distances between keypoints
for( int i = 0; i < descriptors1.rows(); i++ )
{
double dist = matches_list.get(i).distance;
if( dist < min_dist )
min_dist = dist;
if( dist > max_dist )
max_dist = dist;
}
//-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
for( int i = 0; i < descriptors1.rows(); i++ )
{
if( matches_list.get(i).distance < 3*min_dist ){
MatOfDMatch temp = new MatOfDMatch();
temp.fromArray(matches.toArray()[i]);
good_matches.push_back(temp);
}
}
Features2d.drawMatches(Resource, keypoints, Object, templateKeypoints, good_matches, imageOut);
LinkedList<Point> objList = new LinkedList<Point>();
LinkedList<Point> sceneList = new LinkedList<Point>();
List<DMatch> good_matches_list = good_matches.toList();
List<KeyPoint> keypoints_objectList = templateKeypoints.toList();
List<KeyPoint> keypoints_sceneList = keypoints.toList();
for(int i = 0; i<good_matches_list.size(); i++)
{
objList.addLast(keypoints_objectList.get(good_matches_list.get(i).queryIdx).pt);
sceneList.addLast(keypoints_sceneList.get(good_matches_list.get(i).trainIdx).pt);
}
MatOfPoint2f obj = new MatOfPoint2f();
obj.fromList(objList);
MatOfPoint2f scene = new MatOfPoint2f();
scene.fromList(sceneList);
//findHomography
//Mat hg = Calib3d.findHomography(obj, scene);
Mat hg = Calib3d.findHomography(obj, scene, Calib3d.RANSAC, min_dist);
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[] {Object.cols(),0});
obj_corners.put(2, 0, new double[] {Object.cols(),Object.rows()});
obj_corners.put(3, 0, new double[] {0,Object.rows()});
//obj_corners:input
Core.perspectiveTransform(obj_corners, scene_corners, hg);
Core.line(imageOut, new Point(scene_corners.get(0,0)), new Point(scene_corners.get(1,0)), new Scalar(0, 255, 0),4);
Core.line(imageOut, new Point(scene_corners.get(1,0)), new Point(scene_corners.get(2,0)), new Scalar(0, 255, 0),4);
Core.line(imageOut, new Point(scene_corners.get(2,0)), new Point(scene_corners.get(3,0)), new Scalar(0, 255, 0),4);
Core.line(imageOut, new Point(scene_corners.get(3,0)), new Point(scene_corners.get(0,0)), new Scalar(0, 255, 0),4 ...
Did you find a solution?