改进OpenCV的模板匹配算法

问题描述 投票:0回答:2

使用 OpenCV 进行汽车检测

我正在开展一个小型个人项目,我必须知道显示的图像是否是汽车。我正在使用基于 OpenCV python 的基本模板匹配。

正面图像/模板图像

汽车俯视图:- https://i.sstatic.net/zXr1U.jpg

通过使用一张正图像与另一张图像进行简单的模板匹配即可给出所需的结果。

负像

但是当我们使用像 https://i.sstatic.net/YLVwc.jpg 这样的负图像时,模板查找器会发现这是正匹配。

尝试过的方法但失败了:-

  1. 增加模板匹配的阈值:- 增加阈值会导致许多实际的正图像停止匹配

  2. OpenCV 中存在不同类型的模板匹配技术:- 没有给出任何更好的结果

  3. 使用多个模板来加强正负匹配:- 并不适用于所有情况

是否有更好的方法来消除我们收到的模板匹配误报。是否有任何基于特征匹配或边缘检测的技术可以用来代替模板匹配来改进我的算法。

algorithm opencv image-processing object-detection template-matching
2个回答
2
投票

简单但在您的情况下可能值得尝试的方法是将模板分为(“4”)部分 - 角,然后在每个部分上应用模板匹配 只有找到 4 个零件后才接受

还有:

  • 在增加阈值的同时添加不同的模板

  • 应用一些标准化(如果尚未应用)


0
投票

您可以使用 OpenFDCM,它更快并且不依赖 OpenCV。看看这里,它叫OpenFDCM。

您可以在 Google Colab 此处尝试。

pip install openfdcm
import openfdcm

templates = # A list of 4xN array where each array is a template represented as N lines [x1, y1, x2, y2]^T
scene = # A 4xM array representing the M scene lines

# Perform template matching
max_tmpl_lines, max_scene_lines = 4, 4  # Combinatory search parameters.
depth = 30              # The [0, pi] discretization.
coeff = 5.0             # A weighting factor to enhance the angular cost vs distance cost in FDCM algorithm.
scene_padding = 1.5     # A ratio to pad the scene images used in the FDCM algorithm, use if best match may appear on image boundaries.
distance_type = openfdcm.distance.L2 # or openfdcm.distance.L2_SQUARED or openfdcm.distance.L1
#num_threads = 4

threadpool = openfdcm.ThreadPool() # could pass num_threads here, but default is optimal
featuremap_params = openfdcm.Dt3CpuParameters(depth, coeff, scene_padding, distance_type)
search_strategy = openfdcm.DefaultSearch(max_tmpl_lines, max_scene_lines)
optimizer_strategy = openfdcm.BatchOptimize(10, threadpool)
matcher = openfdcm.DefaultMatch()
penalizer = openfdcm.ExponentialPenalty(tau=1.5)

# Build FDCm feature map and search
start_time = time.time()
featuremap = openfdcm.build_cpu_featuremap(scene, featuremap_params, threadpool)
raw_matches = openfdcm.search(matcher, search_strategy, optimizer_strategy, featuremap, templates, scene)
penalized_matches = openfdcm.penalize(penalizer, raw_matches, openfdcm.get_template_lengths(templates))
sorted_matches = openfdcm.sort_matches(penalized_matches)
search_time = time.time() - start_time

print(f"Template matching search completed in {search_time:.4f} seconds.")

best_match = sorted_matches[0]                 # Best match (lower score) is first
best_match_id = best_match.tmpl_idx
best_matched_tmpl = templates[best_match_id]
result_rotation = best_match.transform[0:2, 0:2]
result_translation = best_match.transform[0:2, 2]
© www.soinside.com 2019 - 2024. All rights reserved.