Java Code Examples for org.opencv.features2d.Features2d#drawMatches()

The following examples show how to use org.opencv.features2d.Features2d#drawMatches() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageTest.java    From onetwo with Apache License 2.0 7 votes vote down vote up
public static Mat FeatureSiftLannbased(Mat src, Mat dst){
	FeatureDetector fd = FeatureDetector.create(FeatureDetector.SIFT);
	DescriptorExtractor de = DescriptorExtractor.create(DescriptorExtractor.SIFT);
	DescriptorMatcher Matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
	
	MatOfKeyPoint mkp = new MatOfKeyPoint();
	fd.detect(src, mkp);
	Mat desc = new Mat();
	de.compute(src, mkp, desc);
	Features2d.drawKeypoints(src, mkp, src);
	
	MatOfKeyPoint mkp2 = new MatOfKeyPoint();
	fd.detect(dst, mkp2);
	Mat desc2 = new Mat();
	de.compute(dst, mkp2, desc2);
	Features2d.drawKeypoints(dst, mkp2, dst);
	
	
	// Matching features
	MatOfDMatch Matches = new MatOfDMatch();
	Matcher.match(desc, desc2, Matches);
	
	List<DMatch> l = Matches.toList();
	List<DMatch> goodMatch = new ArrayList<DMatch>();
	for (int i = 0; i < l.size(); i++) {
		DMatch dmatch = l.get(i);
		if (Math.abs(dmatch.queryIdx - dmatch.trainIdx) < 10f) {
			goodMatch.add(dmatch);
		}
		
	}
	
	Matches.fromList(goodMatch);
	// Show result
	Mat OutImage = new Mat();
	Features2d.drawMatches(src, mkp, dst, mkp2, Matches, OutImage);
	
	return OutImage;
}
 
Example 2
Source File: MainActivity.java    From MOAAP with MIT License 6 votes vote down vote up
static Mat drawMatches(Mat img1, MatOfKeyPoint key1, Mat img2, MatOfKeyPoint key2, MatOfDMatch matches, boolean imageOnly){
    //https://github.com/mustafaakin/image-matcher/tree/master/src/in/mustafaak/imagematcher
    Mat out = new Mat();
    Mat im1 = new Mat();
    Mat im2 = new Mat();
    Imgproc.cvtColor(img1, im1, Imgproc.COLOR_GRAY2RGB);
    Imgproc.cvtColor(img2, im2, Imgproc.COLOR_GRAY2RGB);
    if ( imageOnly){
        MatOfDMatch emptyMatch = new MatOfDMatch();
        MatOfKeyPoint emptyKey1 = new MatOfKeyPoint();
        MatOfKeyPoint emptyKey2 = new MatOfKeyPoint();
        Features2d.drawMatches(im1, emptyKey1, im2, emptyKey2, emptyMatch, out);
    } else {
        Features2d.drawMatches(im1, key1, im2, key2, matches, out);
    }
    //Bitmap bmp = Bitmap.createBitmap(out.cols(), out.rows(), Bitmap.Config.ARGB_8888);
    Imgproc.cvtColor(out, out, Imgproc.COLOR_BGR2RGB);
    Imgproc.putText(out, "Frame", new Point(img1.width() / 2,30), Core.FONT_HERSHEY_PLAIN, 2, new Scalar(0,255,255),3);
    Imgproc.putText(out, "Match", new Point(img1.width() + img2.width() / 2,30), Core.FONT_HERSHEY_PLAIN, 2, new Scalar(255,0,0),3);
    return out;
}
 
Example 3
Source File: MainActivity.java    From OpenCV-Android-Object-Detection with MIT License 4 votes vote down vote up
public Mat recognize(Mat aInputFrame) {

        Imgproc.cvtColor(aInputFrame, aInputFrame, Imgproc.COLOR_RGB2GRAY);
        descriptors2 = new Mat();
        keypoints2 = new MatOfKeyPoint();
        detector.detect(aInputFrame, keypoints2);
        descriptor.compute(aInputFrame, keypoints2, descriptors2);

        // Matching
        MatOfDMatch matches = new MatOfDMatch();
        if (img1.type() == aInputFrame.type()) {
            matcher.match(descriptors1, descriptors2, matches);
        } else {
            return aInputFrame;
        }
        List<DMatch> matchesList = matches.toList();

        Double max_dist = 0.0;
        Double min_dist = 100.0;

        for (int i = 0; i < matchesList.size(); i++) {
            Double dist = (double) matchesList.get(i).distance;
            if (dist < min_dist)
                min_dist = dist;
            if (dist > max_dist)
                max_dist = dist;
        }

        LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
        for (int i = 0; i < matchesList.size(); i++) {
            if (matchesList.get(i).distance <= (1.5 * min_dist))
                good_matches.addLast(matchesList.get(i));
        }

        MatOfDMatch goodMatches = new MatOfDMatch();
        goodMatches.fromList(good_matches);
        Mat outputImg = new Mat();
        MatOfByte drawnMatches = new MatOfByte();
        if (aInputFrame.empty() || aInputFrame.cols() < 1 || aInputFrame.rows() < 1) {
            return aInputFrame;
        }
        Features2d.drawMatches(img1, keypoints1, aInputFrame, keypoints2, goodMatches, outputImg, GREEN, RED, drawnMatches, Features2d.NOT_DRAW_SINGLE_POINTS);
        Imgproc.resize(outputImg, outputImg, aInputFrame.size());

        return outputImg;
    }