Java Code Examples for org.opencv.features2d.DescriptorMatcher#create()

The following examples show how to use org.opencv.features2d.DescriptorMatcher#create() . 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 OpenCV-Android-Object-Detection with MIT License 6 votes vote down vote up
private void initializeOpenCVDependencies() throws IOException {
    mOpenCvCameraView.enableView();
    detector = FeatureDetector.create(FeatureDetector.ORB);
    descriptor = DescriptorExtractor.create(DescriptorExtractor.ORB);
    matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
    img1 = new Mat();
    AssetManager assetManager = getAssets();
    InputStream istr = assetManager.open("a.jpeg");
    Bitmap bitmap = BitmapFactory.decodeStream(istr);
    Utils.bitmapToMat(bitmap, img1);
    Imgproc.cvtColor(img1, img1, Imgproc.COLOR_RGB2GRAY);
    img1.convertTo(img1, 0); //converting the image to match with the type of the cameras image
    descriptors1 = new Mat();
    keypoints1 = new MatOfKeyPoint();
    detector.detect(img1, keypoints1);
    descriptor.compute(img1, keypoints1, descriptors1);

}
 
Example 3
Source File: FeatureMatcherOp.java    From StormCV with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
protected void prepareOpenCVOp(Map conf, TopologyContext context) throws Exception {
	this.connectorHolder = new ConnectorHolder(conf);
	matcher = DescriptorMatcher.create( matcherType );
	prototypes = new HashMap<Mat, String>();
	
	for(String location : protoLocations){
		FileConnector fc = connectorHolder.getConnector(location);
		fc.setExtensions(ext);
		fc.moveTo(location);
		List<String> images = fc.list();
		for(String img : images){
			fc.moveTo(img);
			File imageFile = fc.getAsFile();
			Mat proto = calculateDescriptors(ImageIO.read(imageFile));
			prototypes.put(proto, img.substring(img.lastIndexOf('/')+1));
			logger.info("Prototype "+img+" loaded and prepared for matching");
			if(!(fc instanceof LocalFileConnector)) imageFile.delete();
		}
	}
}
 
Example 4
Source File: KMeansMatcher.java    From mvisc with GNU General Public License v3.0 5 votes vote down vote up
public KMeansMatcher()
{
	model = null;
	featureDetector = FeatureDetector.create(FeatureDetector.PYRAMID_ORB);
	descriptorExtractor = DescriptorExtractor.create(DescriptorExtractor.BRIEF);
	matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_SL2);
}
 
Example 5
Source File: ImageRecognition.java    From onetwo with Apache License 2.0 5 votes vote down vote up
/***
     * 匹配关键点
     * @author weishao zeng
     * @param destKeyPoints
     * @param srcKeyPoints
     * @return
     */
    private List<MatOfDMatch> matchKeyPoints(MatOfKeyPoint destKeyPoints, MatOfKeyPoint srcKeyPoints) {
    	 List<MatOfDMatch> matches = new LinkedList<>();
         DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_L1);
//         System.out.println("寻找最佳匹配");
         /**
          * knnMatch方法的作用就是在给定特征描述集合中寻找最佳匹配
          * 使用KNN-matching算法,令K=2,则每个match得到两个最接近的descriptor,然后计算最接近距离和次接近距离之间的比值,当比值大于既定值时,才作为最终match。
          */
         descriptorMatcher.knnMatch(destKeyPoints, srcKeyPoints, matches, 2);
         return matches;
    }
 
Example 6
Source File: PartialMatcher.java    From StormCV with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Override
protected void prepareOpenCVOp(Map conf, TopologyContext context) throws Exception {
	this.connectorHolder = new ConnectorHolder(conf);
	matcher = DescriptorMatcher.create( matcherType );
	prototypes = new HashMap<Integer, String>();
	
	int nrTasks = context.getComponentTasks(context.getThisComponentId()).size();
	int taskIndex = context.getThisTaskIndex();
	
	List<String> original = new ArrayList<String>();
	original.addAll(protoLocations);
	protoLocations.clear();
	for(String dir : original){
		protoLocations.addAll(expand(dir));
	}
	
	FileConnector fc = null;
	List<Mat> training = new ArrayList<Mat>();
	for(int i=taskIndex; i<protoLocations.size(); i+=nrTasks){
		String imgFile = protoLocations.get(i);
		fc = connectorHolder.getConnector(imgFile);
		fc.moveTo(imgFile);
		File imageFile = fc.getAsFile();
		BufferedImage img = ImageIO.read(imageFile);
		if(img == null) continue;
		Mat proto = calculateDescriptors(img);
		prototypes.put(training.size(), imgFile.substring(imgFile.lastIndexOf('/')+1));
		training.add(proto);
		logger.info(this.getClass().getName()+"["+taskIndex+"] "+imgFile+" loaded and prepared for matching");
		if(!(fc instanceof LocalFileConnector)) imageFile.delete();
	}
	matcher.add(training);
	matcher.train();
}
 
Example 7
Source File: ObjectDetection.java    From FTCVision with MIT License 4 votes vote down vote up
/**
 * Instantiate an object detector based on the FAST, BRIEF, and BRUTEFORCE_HAMMING algorithms
 */
public ObjectDetection() {
    detector = FeatureDetector.create(FeatureDetectorType.FAST.val());
    extractor = DescriptorExtractor.create(DescriptorExtractorType.BRIEF.val());
    matcher = DescriptorMatcher.create(DescriptorMatcherType.BRUTEFORCE_HAMMING.val());
}
 
Example 8
Source File: ImageTest.java    From onetwo with Apache License 2.0 4 votes vote down vote up
@Test
	public void imgMatching2() throws Exception {
		System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
//		Mat src_base = Imgcodecs.imread("D:\\test\\test5.jpg");
//		Mat src_test = Imgcodecs.imread("D:\\test\\test3.jpg");

		Mat src_base = Imgcodecs.imread("g:/test/find-src.jpg");
		Mat src_test = Imgcodecs.imread("g:/test/find-dest2.jpg");
		
		Mat gray_base = new Mat();
		Mat gray_test = new Mat();
		// 转换为灰度
		Imgproc.cvtColor(src_base, gray_base, Imgproc.COLOR_RGB2GRAY);
		Imgproc.cvtColor(src_test, gray_test, Imgproc.COLOR_RGB2GRAY);
		// 初始化ORB检测描述子
		FeatureDetector featureDetector = FeatureDetector.create(FeatureDetector.ORB);//特别提示下这里opencv暂时不支持SIFT、SURF检测方法,这个好像是opencv(windows) java版的一个bug,本人在这里被坑了好久。
		DescriptorExtractor descriptorExtractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
		// 关键点及特征描述矩阵声明
		MatOfKeyPoint keyPoint1 = new MatOfKeyPoint(), keyPoint2 = new MatOfKeyPoint();
		Mat descriptorMat1 = new Mat(), descriptorMat2 = new Mat();
		// 计算ORB特征关键点
		featureDetector.detect(gray_base, keyPoint1);
		featureDetector.detect(gray_test, keyPoint2);
		

        Mat output=new Mat();
        Features2d.drawKeypoints(gray_base, keyPoint1, output );
        Imgcodecs.imwrite("g:/test/out.jpg", output);
        
		// 计算ORB特征描述矩阵
		descriptorExtractor.compute(gray_base, keyPoint1, descriptorMat1);
		descriptorExtractor.compute(gray_test, keyPoint2, descriptorMat2);
		float result = 0;
		// 特征点匹配
		System.out.println("test5:" + keyPoint1.size());
		System.out.println("test3:" + keyPoint2.size());
		if (!keyPoint1.size().empty() && !keyPoint2.size().empty()) {
			// FlannBasedMatcher matcher = new FlannBasedMatcher();
			DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_L1);
			MatOfDMatch matches = new MatOfDMatch();
			matcher.match(descriptorMat1, descriptorMat2, matches);
			// 最优匹配判断
			double minDist = 100;
			DMatch[] dMatchs = matches.toArray();
			int num = 0;
			for (int i = 0; i < dMatchs.length; i++) {
				if (dMatchs[i].distance <= 2 * minDist) {
					result += dMatchs[i].distance * dMatchs[i].distance;
					num++;
				}
			}
			// 匹配度计算
			result /= num;
		}
		System.out.println(result);
	}
 
Example 9
Source File: ObjectDetection.java    From FTCVision with MIT License 2 votes vote down vote up
/**
 * Instantiate an object detector based on custom algorithms
 *
 * @param detector  Keypoint detection algorithm
 * @param extractor Keypoint descriptor extractor
 * @param matcher   Descriptor matcher
 */
public ObjectDetection(FeatureDetectorType detector, DescriptorExtractorType extractor, DescriptorMatcherType matcher) {
    this.detector = FeatureDetector.create(detector.val());
    this.extractor = DescriptorExtractor.create(extractor.val());
    this.matcher = DescriptorMatcher.create(matcher.val());
}