Java Code Examples for org.opencv.core.MatOfDMatch#toList()

The following examples show how to use org.opencv.core.MatOfDMatch#toList() . 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: KMeansMatcher.java    From mvisc with GNU General Public License v3.0 4 votes vote down vote up
public ArrayList<int[]> getBestMatches()
{
	
	List<MatOfDMatch> matches = new ArrayList<MatOfDMatch>();
	int[] matchMatrix = new int[numPhotos];
    ArrayList<ArrayList<Integer>> clusterImageMap = model.getClusterImageMap();
    ArrayList<int[]> bestMatchArray = new ArrayList<int[]>();
    
	//get current query image keypoints and descriptors, match with k-means centroid descriptors
	featureDetector.detect(queryImage, queryImageKeypoints);
	descriptorExtractor.compute(queryImage, queryImageKeypoints, queryImageDescriptors);
	matcher.knnMatch(queryImageDescriptors, model.getCentroids(), matches, (int) kMax);
	for (MatOfDMatch mdm : matches)
	{
		for (DMatch dm : mdm.toList())
		{
			int detectedKIndex = dm.trainIdx;
			for (int photoIndex : clusterImageMap.get(detectedKIndex))
				matchMatrix[photoIndex]++;
		}
	}

	//compute n best matches
	int currentBestMatchCount = -1;
	int currentBestMatchIndex = -1;
	for (int i = 0; i < numPhotos; i++)
	{
		currentBestMatchCount = (int) matchMatrix[i];
		currentBestMatchIndex = i;
		bestMatchArray.add(new int[] { currentBestMatchIndex, currentBestMatchCount });
	}
	Collections.sort(bestMatchArray, new Comparator<int[]>()
	{
		public int compare(int[] o1, int[] o2)
		{
			if (o1[1] < o2[1])
				return 1;
			else if (o1[1] > o2[1])
				return -1;
			return 0;
		}
	});
	
	return bestMatchArray;
}
 
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;
    }