在Python中以较大的图像查找图像的所有位置 [英] Find all locations of image in larger image in Python

查看:55
本文介绍了在Python中以较大的图像查找图像的所有位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我扫描了一个由于限制而无法显示的面板,但是我试图对其进行模拟":

I have a scan of a panel which I cannot show due to restrictions, but I tried to 'simulate' it:

此图片模拟了我进行的扫描:带有圆形黑色贴纸的白色背景,每个贴纸中间都有一个白色的小圆圈.某些贴纸在扫描时有些不同,但是形状/贴纸的类型始终相同.

This picture simulates the scan that I have: a white background with round black stickers, which each have a white smaller circle in the middle. Some stickers differ a bit on the scan, but the type of shape/sticker is always the same.

现在,我需要编写一个代码,该代码可以在此图像中查看并显示+返回贴纸的所有位置.我可以使用OpenCV模板匹配在一个位置找到该匹配项,但这只能找到一个较小的图像(我作为输入提供)的完全匹配项.我需要同时查找所有位置.

Now I need to write a code which is able to look in this image and show + return all locations of the stickers. I was able to find this for one location using OpenCV Template Matching, but that is only able to find one exact match with a smaller image which I provide as input. I need to find all locations at the same time.

我在这里或任何涉及我的问题的地方都找不到主题.

I could not find a topic here or anywhere that covers my question.

我希望有人能提供帮助.

I hope someone can help.

关于,加内什

推荐答案

我设法使用相对简单的python脚本来实现此功能,以进行多对象模板匹配.

I managed to get this working with a relatively simple python script for multiple object template matching.

我使用以下图像作为模板.jpg

I used the following image as a template.jpg

我写的脚本如下

import cv2
import numpy as np

#load image into variable
img_rgb = cv2.imread('scan.jpg')

#load template
template = cv2.imread('template.jpg')

#read height and width of template image
w, h = template.shape[0], template.shape[1]

res = cv2.matchTemplate(img_rgb,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
    cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,0), 2)

img_rgb = cv2.resize(img_rgb,(800,600))
cv2.imshow("result",img_rgb)
cv2.waitKey(10000)

结果为

但是,如果这些黑色贴纸的大小或多或少不一致,则可能要使用多尺度模板匹配.

However, if the size of these black stickers is not more or less consistent, you might want to use a multi-scaled approach to template matching.

这篇关于在Python中以较大的图像查找图像的所有位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆