Keypoint matching just works two times...? (java opencv) -
i have strange problem. i'm using code detect image in 1 (java opencv):
updated code:
public void startrecognition() { //load images, want find img_object in img_scene mat img_scene = highgui.imread("d:/opencvws/imagerecognition/src/main/resources/ascene.jpg"); mat img_object = highgui.imread("d:/opencvws/imagerecognition/src/main/resources/aobj1.jpg"); run++; system.out.println("run no: " + run); //init detector featuredetector detector = featuredetector.create(featuredetector.surf); //keypoint detection both images (keyponts_scene img_scene, keypoint_object img_object) matofkeypoint keypoints_object = new matofkeypoint(); matofkeypoint keypoints_scene = new matofkeypoint(); detector.detect(img_object, keypoints_object); detector.detect(img_scene, keypoints_scene); system.out.println("ok: " + keypoints_object.total()); system.out.println("sk: " + keypoints_scene.total()); //extractor init descriptorextractor extractor = descriptorextractor.create(2); //2 = surf; mat descriptor_object = new mat(); mat descriptor_scene = new mat() ; //compute descriptors extractor.compute(img_object, keypoints_object, descriptor_object); extractor.compute(img_scene, keypoints_scene, descriptor_scene); //init matcher descriptormatcher matcher = descriptormatcher.create(descriptormatcher.flannbased); // 1 = flannbased matcher.clear(); matofdmatch matches = new matofdmatch(); //match both descriptors matcher.match(descriptor_object, descriptor_scene, matches); list<dmatch> matcheslist = matches.tolist(); //calc min/max dist double max_dist = 0.0; double min_dist = 100.0; for(int = 0; < 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; } //filter matches linkedlist<dmatch> good_matches = new linkedlist<dmatch>(); matofdmatch gm = new matofdmatch(); //good match = distance > 2*min_distance ==> put them in list for(int = 0; < descriptor_object.rows(); i++){ if(matcheslist.get(i).distance < 2*min_dist){ good_matches.addlast(matcheslist.get(i)); } } //list -> mat gm.fromlist(good_matches); //mat resulting image mat img_matches = new mat(); //filter keypoints (use matches); first in list, iterate, afterwards ==> mat 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 = 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); //calc transformation matrix; method = 8 (ransac) ransacreprojthreshold=3 mat hg = calib3d.findhomography(obj, scene, 8,3); //init corners mat obj_corners = new mat(4,1,cvtype.cv_32fc2); mat scene_corners = new mat(4,1,cvtype.cv_32fc2); //obj obj_corners.put(0, 0, new double[] {0,0}); obj_corners.put(1, 0, new double[] {img_object.cols(),0}); obj_corners.put(2, 0, new double[] {img_object.cols(),img_object.rows()}); obj_corners.put(3, 0, new double[] {0,img_object.rows()}); //transform obj corners scene_img (stored in scene_corners) core.perspectivetransform(obj_corners,scene_corners, hg); //move points img_obg width right fit matching image point p1 = new point(scene_corners.get(0,0)[0]+img_object.cols(), scene_corners.get(0,0)[1]); point p2 = new point(scene_corners.get(1,0)[0]+img_object.cols(), scene_corners.get(1,0)[1]); point p3 = new point(scene_corners.get(2,0)[0]+img_object.cols(), scene_corners.get(2,0)[1]); point p4 = new point(scene_corners.get(3,0)[0]+img_object.cols(), scene_corners.get(3,0)[1]); //create matching image features2d.drawmatches( img_object, keypoints_object, img_scene, keypoints_scene, gm, img_matches); //draw lines matching image core.line(img_matches, p1 , p2, new scalar(0, 255, 0),4); core.line(img_matches, p2, p3, new scalar(0, 255, 0),4); core.line(img_matches, p3, p4, new scalar(0, 255, 0),4); core.line(img_matches, p4, p1, new scalar(0, 255, 0),4); // resizing... mat resizeimage = new mat(); size sz = new size(1200, 1000); imgproc.resize(img_matches, img_matches, sz); panel1.setimagewithmat(img_matches); frame1.repaint(); //tried prevent old references mix new calculation matcher.clear(); img_matches = new mat(); img_object = new mat(); img_scene = new mat(); keypoints_object = new matofkeypoint(); keypoints_scene = new matofkeypoint(); hg = new mat(); } if run startrecognition methode twice (the opencv library loaded @ startup) in running application same result both recognitions. third try detects other keypoints , calculates transformation matrix (hg). examples:
after 2nd try:

after 3rd:

can explain why? or tell me how prevent it? when restart whole program, again detect 2 times correct , afterwards varying. after several tries again calculate correct hg (from first , seceond try). can't figure out why happending.
thanks in advance
gemorra
Comments
Post a Comment