问题是我无法使用单应矩阵制作线条,功能core.line不起作用或者我做错了,因为我在控制台上没有错误。 最后我有两张图片,但两者都是一样的,唯一不同的是黑点 几乎察觉不到。
图像第一匹配:https://www.dropbox.com/s/0hse2pk8a6tx6xj/firstMatching.png?dl=0
images finalMatching:https://www.dropbox.com/s/yust0wjnbkziq7a/final.png?dl=0
public class SearchObject {
void run(String pathObject, String pathScene, String pathResult) {
System.out.println("\n searching Object");
// hier I define my images variables
Mat myObject = Highgui.imread("C:/Users/Angel/Documents/mySpace/EncontrarObjeto/".concat(pathObject), 0); //0 = CV_LOAD_IMAGE_GRAYSCALE
Mat myScene = Highgui.imread("C:/Users/Angel/Documents/mySpace/EncontrarObjeto/".concat(pathScene), 0);
FeatureDetector detector = FeatureDetector.create(FeatureDetector.SIFT); // creamos el object para detectar por puntos claves, para este caso usaremos 4 = SURF
MatOfKeyPoint keyPointsObject = new MatOfKeyPoint();
// I create the matrix kexPoints
MatOfKeyPoint keyPointsScene = new MatOfKeyPoint();
detector.detect(myObject, keyPointsObject);
// here I use the function detect of detector object
detector.detect(myScene, keyPointsScene);
DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SIFT);
// I create an object for extract the descriptors with SIFT
Mat descriptorsObject = new Mat();
Mat descriptorsScene = new Mat() ;
extractor.compute(myObject, keyPointsObject, descriptorsObject);
extractor.compute(myScene, keyPointsScene, descriptorsScene);
// creating the object to matching with FLANNBASES
DescriptorMatcher matcher = DescriptorMatcher.create(1);
MatOfDMatch matches = new MatOfDMatch();
matcher.match(descriptorsObject, descriptorsScene, matches);
List<DMatch> listaMatching = matches.toList();
Double dist_max = 0.0; // definimos los maximos y minimos
Double dist_min = 100.0;
for(int i = 0; i < descriptorsObject.rows(); i++) {
Double dist = (double) listaMatching.get(i).distance;
if(dist < dist_min)
dist_min = dist;
if(dist > dist_max)
dist_max = dist;
}
System.out.println("distance Max: " + dist_max);
System.out.println("distance Min : " + dist_min);
LinkedList<DMatch> good_matching = new LinkedList<DMatch>();
MatOfDMatch GoodM = new MatOfDMatch();
// select the good matching min <3*dist_min
for(int i = 0; i < descriptorsObject.rows(); i++) {
if(listaMatching.get(i).distance < 3*dist_min) {
good_matching.addLast(listaMatching.get(i));
}
}
GoodM.fromList(good_matching);
Mat img_matches = new Mat();
Features2d.drawMatches(myObject, keyPointsObject, myScene, keyPointsScene, GoodM, img_matches, new Scalar(255,255,0), new Scalar(0,0,255), new MatOfByte(), 2);
String firstMatching = "C:/Users/Angel/Documents/mySpace/EncontrarObjeto/PrimerMatching.png";
System.out.println(String.format("writing the first matching %s", firstMatching));
Highgui.imwrite(firstMatching, img_matches);
LinkedList<Point> listObj = new LinkedList<Point>();
LinkedList<Point> listScene = new LinkedList<Point>();
List<KeyPoint> listObject_keyPoints = keyPointsObject.toList();
List<KeyPoint> listScene_keyPoints = keyPointsScene.toList();
for(int i = 0; i<good_matching.size(); i++) {
listObj.addLast(listObject_keyPoints.get(good_matching.get(i).queryIdx).pt);
listScene.addLast(listScene_keyPoints.get(good_matching.get(i).trainIdx).pt);
}
MatOfPoint2f object = new MatOfPoint2f();
object.fromList(listObj);
MatOfPoint2f scene = new MatOfPoint2f();
scene.fromList(listScene);
// searching the Homography matrix with ransac
Mat H = Calib3d.findHomography(object, scene, Calib3d.RANSAC,0.5);
// Calib3d.correctMatches(H,object , escena, object, escena);
Mat corners_Object = new Mat(4,1,CvType.CV_32FC2);
Mat corners_Scene = new Mat(4,1,CvType.CV_32FC2);
// defining the corners object
corners_Object.put(0, 0, new double[] {0,0});
corners_Object.put(1, 0, new double[] {myObject.cols(),0});
corners_Object.put(2, 0, new double[] {myObject.cols(),myObject.rows()});
corners_Object.put(3, 0, new double[] {0,myObject.rows()});
Core.perspectiveTransform(corners_Object,corners_Scene, H);
System.out.println("number of Cols:" + H.cols() + "number of Rows" + H.rows());
System.out.println(H);
Core.line(img_matches, new Point(corners_Scene.get(0,0)), new Point(corners_Scene.get(1,0)), new Scalar(0, 0, 0),4);
Core.line(img_matches, new Point(corners_Scene.get(1,0)), new Point(corners_Scene.get(2,0)), new Scalar(0, 0, 0),4);
Core.line(img_matches, new Point(corners_Scene.get(2,0)), new Point(corners_Scene.get(3,0)), new Scalar(0, 0, 0),4);
Core.line(img_matches, new Point(corners_Scene.get(3,0)), new Point(corners_Scene.get(0,0)), new Scalar(0, 0, 0),4);
// Path final result
pathResult = "C:/Users/Angel/Documents/mySpace/EncontrarObjeto/resultado.png";
System.out.println(String.format("Writing %s", pathResult));
Highgui.imwrite(pathResult, img_matches);
}
}