Java查找图片在另一个图片出现了几次
时间: 2023-09-02 09:07:38 浏览: 47
你可以使用Java中的OpenCV库来实现这个功能。下面是一个示例代码:
```java
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class ImageMatching {
public static void main(String[] args) {
// 加载OpenCV库
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// 加载目标图片和待匹配图片
Mat target = Imgcodecs.imread("target.png");
Mat input = Imgcodecs.imread("input.png");
// 创建输出图片
Mat output = new Mat();
// 使用模板匹配算法进行匹配
Imgproc.matchTemplate(input, target, output, Imgproc.TM_CCOEFF_NORMED);
// 设置匹配阈值
double threshold = 0.8;
// 查找匹配结果
while (true) {
// 查找最大值和对应的位置
Core.MinMaxLocResult result = Core.minMaxLoc(output);
// 判断最大值是否超过阈值
if (result.maxVal < threshold) {
break;
}
// 在输入图片中绘制匹配结果
Imgproc.rectangle(input, result.maxLoc, new Point(result.maxLoc.x + target.cols(), result.maxLoc.y + target.rows()), new Scalar(0, 0, 255), 2);
// 将匹配结果从输出图片中清除
Imgproc.rectangle(output, result.maxLoc, new Point(result.maxLoc.x + target.cols(), result.maxLoc.y + target.rows()), new Scalar(0), -1);
}
// 显示结果图片
HighGui.imshow("Result", input);
HighGui.waitKey();
}
}
```
这个程序会加载一个目标图片和一个待匹配图片,在待匹配图片中查找目标图片出现的位置,并在结果图片中用红色矩形框出匹配结果。你可以根据需要调整匹配阈值,来控制匹配的精度和召回率。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)