Source Code : The homography tutorial in java
The homography tutorial in java
CODE
<code>classFindObject{<br> publicvoid run(String pathObject,String pathScene,String pathResult){<span>System.out.println("\nRunning FindObject");<span>Mat img_object =Highgui.imread("D:/workspaceSeirich/HelloCV/".concat(pathObject),0);//0 = CV_LOAD_IMAGE_GRAYSCALEMat img_scene =Highgui.imread("D:/workspaceSeirich/HelloCV/".concat(pathScene),0);<span>FeatureDetector detector =FeatureDetector.create(4);//4 = SURF <span>MatOfKeyPoint keypoints_object =newMatOfKeyPoint();MatOfKeyPoint keypoints_scene =newMatOfKeyPoint();<br><br>detector.detect(img_object, keypoints_object);<br>detector.detect(img_scene, keypoints_scene);<span>DescriptorExtractor extractor =DescriptorExtractor.create(2);//2 = SURF;<span>Mat descriptor_object =newMat();Mat descriptor_scene =newMat();<br><br>extractor.compute(img_object, keypoints_object, descriptor_object);<br>extractor.compute(img_scene, keypoints_scene, descriptor_scene);<span>DescriptorMatcher matcher =DescriptorMatcher.create(1);// 1 = FLANNBASEDMatOfDMatch matches =newMatOfDMatch();<br><br>matcher.match(descriptor_object, descriptor_scene, matches);List<DMatch> matchesList = matches.toList();<span>Double max_dist =0.0;Double min_dist =100.0;<span>for(int i =0; i < descriptor_object.rows(); i++){<br> Double dist =(double) matchesList.get(i).distance;<br> if(dist < min_dist) min_dist = dist;<br> if(dist > max_dist) max_dist = dist;}<span>System.out.println("-- Max dist : "+ max_dist);System.out.println("-- Min dist : "+ min_dist); <span>LinkedList<DMatch> good_matches =newLinkedList<DMatch>();MatOfDMatch gm =newMatOfDMatch();<span>for(int i =0; i < descriptor_object.rows(); i++){<br> if(matchesList.get(i).distance <3*min_dist){<br> good_matches.addLast(matchesList.get(i));<br> }}<br><br>gm.fromList(good_matches);<span>Mat img_matches =newMat();<br>Features2d.drawMatches(<br> img_object,<br> keypoints_object, <br> img_scene,<br> keypoints_scene, <br> gm, <br> img_matches, <br> newScalar(255,0,0), <br> newScalar(0,0,255), <br> newMatOfByte(), <br> 2);<span>LinkedList<Point> objList =newLinkedList<Point>();LinkedList<Point> sceneList =newLinkedList<Point>();<span>List<KeyPoint> keypoints_objectList = keypoints_object.toList();List<KeyPoint> keypoints_sceneList = keypoints_scene.toList();<span>for(int i =0; i<good_matches.size(); i++){<br> objList.addLast(keypoints_objectList.get(good_matches.get(i).queryIdx).pt);<br> sceneList.addLast(keypoints_sceneList.get(good_matches.get(i).trainIdx).pt);}<br><br>MatOfPoint2f obj =new MatOfPoint2f();<br>obj.fromList(objList);<br><br>MatOfPoint2f scene =new MatOfPoint2f();<br>scene.fromList(sceneList);<span>Mat H = Calib3d.findHomography(obj, scene);<span>LinkedList<Point> cornerList =newLinkedList<Point>();<br>cornerList.add(newPoint(0,0));<br>cornerList.add(newPoint(img_object.cols(),0));<br>cornerList.add(newPoint(img_object.cols(),img_object.rows()));<br>cornerList.add(newPoint(0,img_object.rows()));<span>MatOfPoint obj_corners =newMatOfPoint();<br>obj_corners.fromList(cornerList);<span>MatOfPoint scene_corners =newMatOfPoint();<span>//ERROR HERE ://OpenCV Error: Assertion failed (scn + 1 == m.cols && (depth == CV_32F || depth == CV_64F)) in unknown function, file ..\..\..\src\opencv\modules\core\src\matmul.cpp, line 1926Core.perspectiveTransform(obj_corners, scene_corners, H);<span>//Draw the lines... later, when the homography will work/*<br>Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);<br>Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);<br>Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);<br>Core.line(img_matches, new Point(), new Point(), new Scalar(0,255,0), 4);<br>*/<span>//Sauvegarde du résultatSystem.out.println(String.format("Writing %s", pathResult));Highgui.imwrite(pathResult, img_matches);</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
} }
<code>publicclassFinder{<br> publicstaticvoid main(String[] args){<br> System.loadLibrary("opencv_java244");<br> newFindObject().run("object.jpg","scene.jpg","resultat.png");<br> }}</code>