org.opencv.features2d.Features2d Java Examples
The following examples show how to use
org.opencv.features2d.Features2d.
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 |
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 |
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: ImageRecognition.java From onetwo with Apache License 2.0 | 5 votes |
private Mat createMatAndDrawKeypoints(Mat imageMat, MatOfKeyPoint detectKeyPoints) { //显示模板图的特征点图片 Mat outputImage = new Mat(imageMat.rows(), imageMat.cols(), Imgcodecs.CV_LOAD_IMAGE_COLOR); //在图片上显示提取的特征点 // System.out.println("在图片上显示提取的特征点"); Features2d.drawKeypoints(imageMat, detectKeyPoints, outputImage, new Scalar(255, 0, 0), 0); return outputImage; }
Example #4
Source File: MainActivity.java From OpenCV-Android-Object-Detection with MIT License | 4 votes |
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; }
Example #5
Source File: ImageTest.java From onetwo with Apache License 2.0 | 4 votes |
@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); }