当前位置: 首页 > ai >正文

2025-06-14【视觉】批量筛选图集中包含某种物体对象的方法

在这里插入图片描述
要查找包含的图片
在这里插入图片描述
查找结果如下:
在这里插入图片描述
其中有一个是错误的。

代码

#
#
# import aircv as ac
#
# # 读取图像
# im_source = ac.imread('large.jpg')
# im_search = ac.imread('s.jpg')
# # 查找模板
# result = ac.find_template(im_source, im_search)
# print(result)import cv2
import numpy as np
import os
from PIL import Image
import cv2def imread_chinese(filepath):"""支持中文路径的图像读取函数"""image = Image.open(filepath).convert('RGB')  # 读取图像并转为RGBreturn cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)  # 转为OpenCV格式def find_template_in_image(big_img, small_img):"""使用 ORB 算法判断 big_img 是否包含 small_img。"""orb = cv2.ORB_create()kp1, des1 = orb.detectAndCompute(small_img, None)kp2, des2 = orb.detectAndCompute(big_img, None)if des1 is None or des2 is None:return False  # 没有提取到特征描述符bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)matches = bf.match(des1, des2)matches = sorted(matches, key=lambda x: x.distance)good_matches = [m for m in matches[:10] if m.distance < 50]  # 可调参数return len(good_matches) > 2  # 设定阈值def find_images_containing_template(image_folder, template_path, output_folder="matched"):"""遍历文件夹,筛选出包含模板的小图,并保存结果。"""small_img = cv2.imread(template_path)assert small_img is not None, "无法加载模板图像"matched_images = []for filename in os.listdir(image_folder):file_path = os.path.join(image_folder, filename)if filename.lower().endswith(('.png', '.jpg', '.jpeg')):big_img = imread_chinese(file_path)if big_img is None:print(f"无法加载图像(可能损坏):{filename}")continueprint(f"正在处理:{filename}")if find_template_in_image(big_img, small_img):print(f"✅ 找到匹配:{filename}")matched_images.append(filename)# 可选:保存匹配成功的图像到指定目录if not os.path.exists(output_folder):os.makedirs(output_folder)cv2.imwrite(os.path.join(output_folder, filename), big_img)return matched_images# 示例调用
image_folder = 'images'          # 大图所在文件夹
template_path = 'small.jpg'     # 小图模板路径
output_list = find_images_containing_template(image_folder, template_path)print("包含模板的图片列表:")
for img_name in output_list:print(img_name)
http://www.xdnf.cn/news/14071.html

相关文章:

  • Spring Framework 执行链路设计
  • PC 基准测试工具 3D Mark 登陆 macOS
  • Boost dlib opencv vs2022 C++ 源码安装集成配置
  • Missing Semester计算机教育中缺失的一课:Vim
  • 端到端记忆网络 vs 神经图灵机:外部记忆的两种哲学之争
  • 腾讯云配置了国内镜像依然docker search失败
  • Vue3 + JavaScript 父组件点击按钮触发子组件事件方法
  • pytorch2.6安装
  • 编程之礼乐
  • Karate UI测试之驱动配置
  • 嵌入式学习笔记C语言阶段--14可变长数组
  • HTTP 缓存策略:强缓存与协商缓存的深入解析
  • Docker 部署 PostgreSQL 指南
  • C++ RPC 远程过程调用详细解析
  • Python爬虫-爬取票牛明星演唱会数据,进行数据分析
  • 日志分割问题
  • 卷积神经网络的参数量及尺度变化计算
  • [每周一更]-(第147期):使用 Go 语言实现 JSON Web Token (JWT)
  • WWDC25中的HDR技术洞察
  • 基于深度学习的智能图像分类系统:从零开始构建
  • BeckHoff <---> Keyence (LJ-8000) 2D相机 Profinet 通讯
  • PyTorch框架详解(1)
  • 韦东奕论文解读
  • 开机自启动文件夹
  • Vue嵌套路由
  • 机器学习入门 | 机器学习方法与模型概述
  • 【图纸管理教程-3】编码统一,效率倍增!解决一物多码问题
  • Manus邀请薅羊毛
  • WPF加载文本文件时如何设置WebBrowser的字体
  • 第三章支线八 ·构建之巅 · 工具链与打包炼金术