与 python-openCv 匹配的简单模板
Simple template matching with python-openCv
我正在尝试检测图像中的一些简单 'red patterns'。这是我遵循的算法:
1) 过滤掉所有其他颜色而不是 'red' 并创建黑白图像。我使用 'cvtColor' 和适当的掩码,然后我应用 'GaussianBlur' 来减少噪音。到目前为止一切都很好。
2) 我使用如下函数 'matchTemplate' 来检测图像中的 'arrow' 模板。
问题: 当 'arrow' 模板在照片中时,它被正确检测到。
但是当它不在照片中时,算法会检测到其他一些错误的形状。
有人可以修改代码,以便当箭头模板不在图片中时,不会检测到任何内容。
这是我的代码:
template = cv2.imread(address,0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(self.image['blured'], template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(self.image['blured'],top_left, bottom_right, 255, 2)
cv2.rectangle(self.image['normal'], top_left, bottom_right, 255,2)
结果如下:
错误检测:
我的模板图像,我从主照片中精确裁剪的:
有人能看出我的错误吗?我是图像处理的新手。提前致谢。
你必须看看你的 max_val
并设置一个阈值。
假设图像包含箭头时max_val
为x1
,图像不包含箭头时为x2
,则应为x1 > x2
。作为第一个暂定值,您可以选择 threshold=(x1+x2)/2
,然后如果 max_val > threshold
则在图像中找到图案,否则找不到图案。
slides through image
, compares the overlapped patches of size w
\times h
against templ
using the specified method and stores the
comparison results in result
.
因此您的图像 res
将始终具有最大值,而不管图像中是否存在箭头。
非常感谢 Alessandro Jacopson。
我修改了代码,通过这种方式解决了问题。这几乎是你提出的:
template = cv2.imread(address,0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(self.image['blured'], template,cv2.TM_CCOEFF_NORMED)
threshold = 0.9
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(self.image['blured'], pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
cv2.rectangle(self.image['normal'], pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
以这种方式检测到的模式非常准确。
新问题:对方向和旋转敏感。如果它是 rotated.for 大约 45 度的例子,它不会检测到模式。
是否有任何参数我可以设置,并使算法找到模式,尽管它是旋转或缩放的?
我正在尝试检测图像中的一些简单 'red patterns'。这是我遵循的算法: 1) 过滤掉所有其他颜色而不是 'red' 并创建黑白图像。我使用 'cvtColor' 和适当的掩码,然后我应用 'GaussianBlur' 来减少噪音。到目前为止一切都很好。
2) 我使用如下函数 'matchTemplate' 来检测图像中的 'arrow' 模板。
问题: 当 'arrow' 模板在照片中时,它被正确检测到。 但是当它不在照片中时,算法会检测到其他一些错误的形状。 有人可以修改代码,以便当箭头模板不在图片中时,不会检测到任何内容。 这是我的代码:
template = cv2.imread(address,0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(self.image['blured'], template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(self.image['blured'],top_left, bottom_right, 255, 2)
cv2.rectangle(self.image['normal'], top_left, bottom_right, 255,2)
结果如下:
错误检测:
我的模板图像,我从主照片中精确裁剪的:
有人能看出我的错误吗?我是图像处理的新手。提前致谢。
你必须看看你的 max_val
并设置一个阈值。
假设图像包含箭头时max_val
为x1
,图像不包含箭头时为x2
,则应为x1 > x2
。作为第一个暂定值,您可以选择 threshold=(x1+x2)/2
,然后如果 max_val > threshold
则在图像中找到图案,否则找不到图案。
slides through
image
, compares the overlapped patches of sizew
\timesh
againsttempl
using the specified method and stores the comparison results inresult
.
因此您的图像 res
将始终具有最大值,而不管图像中是否存在箭头。
非常感谢 Alessandro Jacopson。 我修改了代码,通过这种方式解决了问题。这几乎是你提出的:
template = cv2.imread(address,0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(self.image['blured'], template,cv2.TM_CCOEFF_NORMED)
threshold = 0.9
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(self.image['blured'], pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
cv2.rectangle(self.image['normal'], pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
以这种方式检测到的模式非常准确。 新问题:对方向和旋转敏感。如果它是 rotated.for 大约 45 度的例子,它不会检测到模式。 是否有任何参数我可以设置,并使算法找到模式,尽管它是旋转或缩放的?