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

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.dtcms.com/a/250311.html

相关文章:

  • 解决ModuleNotFoundError: No module named ‘open_clip‘
  • 多项目状态如何集中监控与汇总
  • 基于开源AI大模型与智能工具的优质内容引流策略研究——以AI智能名片及S2B2C商城小程序源码应用为例
  • 禾川伺服驱动器与EtherCAT主站转Profinet网关的双向数据交换
  • 纯 CSS 实现的的3种扫光效果
  • 基于STM32人脸识别系统
  • (LeetCode每日一题) 2566. 替换一个数字后的最大差值 ( 贪心 )
  • pytorch2.6安装
  • React项目常用目录结构
  • ResizeObserver的错误
  • 人工智能学习23-BP-图像编码
  • 5.5.1_哈夫曼树
  • UltraISO绿色便携版v9 下载与安装教程
  • StampedLock入门教程
  • 在线五子棋
  • 从大模型到 AI 应用,一共需要几步?
  • 【项目实训#10】HarmonyOS API文档RAG检索系统后端实现
  • windows 开发
  • 吴恩达机器学习笔记(2)—单变量线性回归
  • 10.OpenCV—联合QT界面显示
  • Spring Boot 自动配置原理深度解析与自定义 Starter 实战
  • 3.1.2_栈的顺序存储实现
  • Nature Machine Intelligence 北京通研院朱松纯团队开发视触觉传感仿人灵巧手,实现类人自适应抓取
  • 深度解析Vue.js组件开发与实战案例
  • JavaSE超详细笔记-网络编程篇-基于黑马
  • evo工具
  • 【Git】使用 SSH 协议 解决 Git 推送失败问题
  • OPPO闹钟失灵:用户信任崩塌,市场地位岌岌可危
  • LangChain面试内容整理-知识点12:检索器(Retriever)接口与实现
  • Python开发功能实用