安卓利用openCV实现两张图片的相似度对比的详细代码及过程
时间: 2024-02-25 10:53:46 浏览: 146
实现两张图片的相似度对比可以使用OpenCV中的ORB算法来提取图片的特征点,然后使用Brute-Force匹配算法来匹配特征点,最后计算匹配点的数量来表示两张图片的相似度。以下是详细的代码及过程:
1. 导入OpenCV库
```java
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.Point;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.FeatureDetector;
import org.opencv.features2d.Features2d;
import org.opencv.features2d.FlannBasedMatcher;
import org.opencv.features2d.KeyPoint;
import org.opencv.features2d.Matcher;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
```
2. 加载图片
```java
// 加载图片
Mat img1 = Imgcodecs.imread("path/to/image1.jpg");
Mat img2 = Imgcodecs.imread("path/to/image2.jpg");
```
3. 提取特征点
```java
// 创建ORB特征点检测器和描述子提取器
FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
// 检测特征点并提取描述子
MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
Mat descriptors1 = new Mat();
Mat descriptors2 = new Mat();
detector.detect(img1, keypoints1);
detector.detect(img2, keypoints2);
extractor.compute(img1, keypoints1, descriptors1);
extractor.compute(img2, keypoints2, descriptors2);
```
4. 匹配特征点
```java
// 创建Brute-Force匹配器
Matcher matcher = new BFMatcher();
// 匹配特征点
MatOfDMatch matches = new MatOfDMatch();
matcher.match(descriptors1, descriptors2, matches);
```
5. 计算相似度
```java
// 计算匹配点数量
double similarity = matches.toArray().length;
// 显示匹配结果
Mat outputImg = new Mat();
Features2d.drawMatches(img1, keypoints1, img2, keypoints2, matches, outputImg);
HighGui.imshow("Matches", outputImg);
HighGui.waitKey();
```
完整代码如下:
```java
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.Point;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.FeatureDetector;
import org.opencv.features2d.Features2d;
import org.opencv.features2d.FlannBasedMatcher;
import org.opencv.features2d.KeyPoint;
import org.opencv.features2d.Matcher;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class ImageComparator {
public static void main(String[] args) {
// 加载OpenCV库
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// 加载图片
Mat img1 = Imgcodecs.imread("path/to/image1.jpg");
Mat img2 = Imgcodecs.imread("path/to/image2.jpg");
// 创建ORB特征点检测器和描述子提取器
FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.ORB);
// 检测特征点并提取描述子
MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
Mat descriptors1 = new Mat();
Mat descriptors2 = new Mat();
detector.detect(img1, keypoints1);
detector.detect(img2, keypoints2);
extractor.compute(img1, keypoints1, descriptors1);
extractor.compute(img2, keypoints2, descriptors2);
// 创建Brute-Force匹配器
Matcher matcher = new BFMatcher();
// 匹配特征点
MatOfDMatch matches = new MatOfDMatch();
matcher.match(descriptors1, descriptors2, matches);
// 计算匹配点数量
double similarity = matches.toArray().length;
// 显示匹配结果
Mat outputImg = new Mat();
Features2d.drawMatches(img1, keypoints1, img2, keypoints2, matches, outputImg);
HighGui.imshow("Matches", outputImg);
HighGui.waitKey();
}
}
```
阅读全文