Skip to content

Instantly share code, notes, and snippets.

@salememd
Last active November 28, 2017 07:39
Show Gist options
  • Save salememd/d30920b398125803d0b58402cd424c9e to your computer and use it in GitHub Desktop.
Save salememd/d30920b398125803d0b58402cd424c9e to your computer and use it in GitHub Desktop.
public Meta getMatch(Mat frame) {
try {
FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
DescriptorExtractor descriptor = DescriptorExtractor.create(DescriptorExtractor.ORB);;
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
//first image
Mat img1 = frame.clone();//Imgcodecs.imread("C:\\Users\\Dexter\\Desktop\\opencv\\2\\333.png", 0);
Mat descriptors1 = new Mat();
MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
detector.detect(img1, keypoints1);
descriptor.compute(img1, keypoints1, descriptors1);
//second image
Mat img2 = tamplate.clone(); //Imgcodecs.imread("C:\\Users\\Dexter\\Desktop\\opencv\\2\\222.png", 0);
Mat descriptors2 = new Mat();
MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
detector.detect(img2, keypoints2);
descriptor.compute(img2, keypoints2, descriptors2);
//matcher should include 2 different image's descriptors
MatOfDMatch matches = new MatOfDMatch();
matcher.match(descriptors1, descriptors2, matches);
List<DMatch> good_matches = matches.toList();
List<DMatch> good_matches2 = new ArrayList<>();
System.out.println(" mataches : " + good_matches.size());
// if there < 450 point matches don't continue
if (good_matches.size() < 450) {
return frame;
}
Comparator<DMatch> bySizeDifferent = Comparator.comparing(user -> user.distance);
// from the > 450 get 100 to do homography on them and sort them by the distance
good_matches.stream().sorted(bySizeDifferent).limit(100).forEach(u -> {
good_matches2.add(u);
});
System.out.println("good matache : " + good_matches2.get(0).distance + " " + good_matches2.get(good_matches2.size() - 1).distance);
if (good_matches2.get(0).distance > 25) { // if the best keypoint match score < 85% drop the proces, change the value whaever you want
return frame;
}
MatOfDMatch gm = new MatOfDMatch();
gm.fromList(good_matches2);
/*
//this will draw all matches,
Scalar RED = new Scalar(255, 0, 0);
Scalar GREEN = new Scalar(0, 255, 0);
//output image
Mat outputImg = new Mat();
MatOfByte drawnMatches = new MatOfByte();
Features2d.drawMatches(img1, keypoints1, img2, keypoints2, gm, outputImg, GREEN, RED, drawnMatches, Features2d.NOT_DRAW_SINGLE_POINTS);
return outputImg;
*/
LinkedList<Point> objList = new LinkedList<Point>();
LinkedList<Point> sceneList = new LinkedList<Point>();
List<KeyPoint> keypoints_objectList = keypoints1.toList();
List<KeyPoint> keypoints_sceneList = keypoints2.toList();
//System.err.println(good_matches2.get(0).distance);
for (int i = 0; i < good_matches2.size(); i++) {
objList.addLast(keypoints_sceneList.get(good_matches2.get(i).trainIdx).pt);
sceneList.addLast(keypoints_objectList.get(good_matches2.get(i).queryIdx).pt);
}
MatOfPoint2f obj = new MatOfPoint2f();
obj.fromList(objList);
MatOfPoint2f scene = new MatOfPoint2f();
scene.fromList(sceneList);
Mat hg = Calib3d.findHomography(obj, scene, Calib3d.RANSAC, 3);
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[]{img2.cols(), 0});
obj_corners.put(2, 0, new double[]{img2.cols(), img2.rows()});
obj_corners.put(3, 0, new double[]{0, img2.rows()});
Core.perspectiveTransform(obj_corners, scene_corners, hg);
// Draw ROI
Imgproc.line(img1, new Point(scene_corners.get(0, 0)), new Point(scene_corners.get(1, 0)), new Scalar(255, 255, 0), 4);
Imgproc.line(img1, new Point(scene_corners.get(1, 0)), new Point(scene_corners.get(2, 0)), new Scalar(255, 255, 0), 4);
Imgproc.line(img1, new Point(scene_corners.get(2, 0)), new Point(scene_corners.get(3, 0)), new Scalar(255, 255, 0), 4);
Imgproc.line(img1, new Point(scene_corners.get(3, 0)), new Point(scene_corners.get(0, 0)), new Scalar(255, 255, 0), 4);
return img1;
}catch(Exception e){
return frame;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment