HI, I find the keypoint and the match between two images with BRISK ( I have to use it ) but now I need to use RANSAC to estimate the correspondence better than the only matcher function.
Can you help me?
I think:
I take the position of keypoint in the object and in the scene and calculate Homography with it, but i don't now how to continue.
Please help me.
This is my code:
Bitmap Matches(){
FeatureDetector detector;
MatOfKeyPoint keypoints1, keypoints2;
DescriptorExtractor descriptorExtractor;
Mat descriptors1, descriptors2;
DescriptorMatcher descriptorMatcher;
MatOfDMatch matches = new MatOfDMatch();
keypoints1 = new MatOfKeyPoint();
keypoints2 = new MatOfKeyPoint();
descriptors1 = new Mat();
descriptors2 = new Mat();
detector = FeatureDetector.create(FeatureDetector.BRISK);
descriptorExtractor = DescriptorExtractor.create(DescriptorExtractor.BRISK);
descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
detector.detect(immagine1,keypoints1);
detector.detect(immagine2, keypoints2);
descriptorExtractor.compute(immagine1, keypoints1, descriptors1);
descriptorExtractor.compute(immagine2, keypoints2, descriptors2);
descriptorMatcher.match(descriptors1, descriptors2, descriptorMatcher.match(descriptors2, descriptors1, matches);
// Creo una lista di Keypoint della prima e della seconda immagine
//calculate max and min distances between keypoints
double max_dist=0;double min_dist=99;
List<DMatch> matchesList = matches.toList();
for(int i=0;i<descriptors2.rows();i++)
{
double dist = matchesList.get(i).distance;
if (dist<min_dist) min_dist = dist;
if (dist>max_dist) max_dist = dist;
}
//set up good matches, add matches if close enough
LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
MatOfDMatch gm = new MatOfDMatch();
for (int i=0;i<descriptors1.rows();i++)
{
if(matchesList.get(i).distance<max_dist)
{
good_matches.addLast(matchesList.get(i));
}
}
gm.fromList(good_matches);
//put keypoints mats into lists
List<KeyPoint> keypoints1_List = keypoints1.toList();
List<KeyPoint> keypoints2_List = keypoints2.toList();
// Creo la lista che conterrà i keypoint della seconda immagine che hanno matchato
List<KeyPoint> keypoint_scena_match = keypoints2.toList();
// matrice dei soli keypoint della scena che matchano
MatOfKeyPoint keypoint2_matched = new MatOfKeyPoint();
keypoint2_matched.fromList(keypoint_scena_match);
// Ottengo i punti dove si trovano i keypoint:
// Creo la lista dei punti di match tra oggetto e scena
//put keypoints into point2f mats so calib3d can use them to find homography
LinkedList<Point> objList = new LinkedList<Point>();
LinkedList<Point> sceneList = new LinkedList<Point>();
// Creo la lista dei punti che hanno matchato
for (int i=0; i<descriptors1.rows();i++){
objList.addLast(keypoints1_List.get(matches.toList().get(i).queryIdx).pt);
sceneList.addLast(keypoints2_List.get(matches.toList().get(i).trainIdx).pt);
for(int i=0;i<good_matches.size();i++)
{
objList.addLast(keypoints1_List.get(good_matches.get(i).trainIdx).pt);
sceneList.addLast(keypoints2_List.get(good_matches.get(i).queryIdx).pt);
}
// Creo le matrici di punti che contengono i punti che matchano
MatOfPoint2f oggeto obj = new MatOfPoint2f();
MatOfPoint2f scena scene = new MatOfPoint2f();
oggeto.fromList(objList);
scena.fromList(sceneList);
// Trovo la trasformazione avvenuta tra una matrice e l'altra:
obj.fromList(objList);
scene.fromList(sceneList);
//output image
Mat outputImg = new Mat();
MatOfByte drawnMatches = new MatOfByte();
Features2d.drawMatches(immagine1, keypoints1, immagine2, keypoints2, gm, outputImg,
Scalar.all(-1), Scalar.all(-1), drawnMatches,Features2d.NOT_DRAW_SINGLE_POINTS);
//run homography on object and scene points
Mat H = Calib3d.findHomography(oggeto, scena, Calib3d.RANSAC, 10); Calib3d.findHomography(obj, scene,Calib3d.RANSAC, 5);
Mat tmp_corners = new Mat(4,1,CvType.CV_32FC2);
Mat scene_corners = new Mat(4,1,CvType.CV_32FC2);
//get corners from object
tmp_corners.put(0, 0, new double[] {0,0});
tmp_corners.put(1, 0, new double[] {immagine1.cols(),0});
tmp_corners.put(2, 0, new double[] {immagine1.cols(),immagine1.rows()});
tmp_corners.put(3, 0, new double[]{0, immagine1.rows()});
Core.perspectiveTransform(tmp_corners, scene_corners, H);
// uso RANSAC
find corner in the scene
Core.line(outputImg, new Point(scene_corners.get(0,0)),
new Point(scene_corners.get(1,0)), new Scalar(0, 255, 0),4);
Core.line(outputImg, new Point(scene_corners.get(1,0)),
new Point(scene_corners.get(2,0)), new Scalar(0, 255, 0),4);
Core.line(outputImg, new Point(scene_corners.get(2,0)),
new Point(scene_corners.get(3,0)), new Scalar(0, 255, 0),4);
Core.line(outputImg, new Point(scene_corners.get(3,0)),
new Point(scene_corners.get(0,0)), new Scalar(0, 255, 0),4);
// Trovo la posizione dei keypoint tramite homografia
MatOfPoint2f oggetto_homografato = new MatOfPoint2f();
Imgproc.warpPerspective(oggeto, oggetto_homografato, H, new Size(immagine1.cols(), immagine1.rows()));
// prendo i keypoint giusti:
double dist_x = Math.abs(oggetto_homografato.toList().get(1).x-scena.toList().get(1).x);
double dist_y = Math.abs(oggetto_homografato.toList().get(1).y - scena.toList().get(1).y);
Toast.makeText(MainActivity.this,"Ho Trovato i match",Toast.LENGTH_SHORT).show();
/*
Mat src3 = new Mat();
Mat im1 = new Mat();
Mat im2 = new Mat();
Imgproc.cvtColor(immagine1, im1, Imgproc.COLOR_BGR2RGB);
Imgproc.cvtColor(immagine2, im2, Imgproc.COLOR_BGR2RGB);
Features2d.drawMatches(im1, keypoints1, im2, keypoints2, matches, src3);
create the ouput image
Bitmap image1 = Bitmap.createBitmap(src3.cols(), src3.rows(), image_out = Bitmap.createBitmap(outputImg.cols(),outputImg.rows(), Bitmap.Config.ARGB_8888);
Imgproc.cvtColor(src3, src3, Imgproc.COLOR_RGB2BGR);
Utils.matToBitmap(src3, image1);
Toast.makeText(MainActivity.this,"Ho finito",Toast.LENGTH_SHORT).show();*/
//Imgproc.cvtColor(outputImg,outputImg,Imgproc.COLOR_GRAY2BGR);
Utils.matToBitmap(outputImg,image_out);
return null; // for now is null but at the and it return the aside image of object and the scene
image_out;
}
I think if I find the shape of the object and then apply to this the Hmography but my difficulty is to fine the shape, i try with findContours but it does not work i need the coordinate of the point with create the contours.
This is my new code and the result. Can you help me?