You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenCV模板匹配与ORB特征检测失效问题求助

星星标记检测问题:模板匹配与ORB特征检测失效的解决方案求助

我需要检测图像中添加的两颗星星标记,连接成矩形以提取答题区域,当前卡在标记的准确检测上。尝试了模板匹配和ORB特征检测,均未得到预期结果。

基础素材

  • 基础图像:基础图像
  • 星星模板:星星模板

尝试1:模板匹配

使用以下代码进行模板匹配:

# Load the base image and the black star image
base_image = cv2.imread(path_saida_jpg, cv2.IMREAD_COLOR)
star_template = cv2.imread(path_base_star, cv2.IMREAD_COLOR)

base_gray = cv2.cvtColor(base_image, cv2.COLOR_BGR2GRAY)
star_template_gray = cv2.cvtColor(star_template, cv2.COLOR_BGR2GRAY)

# Perform template matching to find the stars
result = cv2.matchTemplate(base_image, star_template, cv2.TM_CCOEFF_NORMED)
threshold = 0.80  
locations = np.where(result >= threshold)

# Draw rectangles around the found stars
for loc in zip(*locations[::-1]):
    h, w = star_template.shape[:2]
    cv2.rectangle(base_image, loc, (loc[0] + w, loc[1] + h), (0, 255, 0), 2)  # Draw a green rectangle

    cv2.imwrite('result_image.jpg', base_image)

问题:locations始终为空,仅当阈值降至0.6时能找到匹配,但结果完全不符合预期:
模板匹配低阈值结果


尝试2:ORB特征检测

考虑到可能存在缩放问题,尝试ORB特征检测:初始用64px模板无结果,放大至512px效果更差,代码如下:

base_image = cv2.imread(path_saida_jpg, cv2.IMREAD_COLOR)
star_template = cv2.imread(path_base_star, cv2.IMREAD_COLOR)

base_gray = cv2.cvtColor(base_image, cv2.COLOR_BGR2GRAY)
star_template_gray = cv2.cvtColor(star_template, cv2.COLOR_BGR2GRAY)

orb = cv2.ORB_create()

kp1, des1 = orb.detectAndCompute(base_gray, None)
kp2, des2 = orb.detectAndCompute(star_template_gray, None)
 
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)

matches = bf.match(des1, des2)

matches = sorted(matches, key=lambda x: x.distance)

result_image = cv2.drawMatches(base_image, kp1, star_template, kp2, matches[:10], None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)

# Export the result
cv2.imwrite('result_image.jpg', result_image)

结果:匹配结果完全不正确:
ORB匹配结果


我是图像处理新手,已经参考过ChatGPT和GeeksforGeeks的资料,仍无法解决问题,恳请提供可行的解决方案。

内容的提问来源于stack exchange,提问作者Max

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 04:33:13