OpenCV模板匹配或特征检测以正确分类传真类型?

2024-06-02 09:13:28 发布

您现在位置:Python中文网/ 问答频道 /正文

抱歉,这是我第一次发问。在

我需要一个过程来识别其他图像中的图像,以便能够对图像进行分类(例如,根据公司符号对传真进行排序)。在

我尝试过openCV模板匹配,但我似乎没有那么准确,如果我试图通过循环多个图像来使用它,我似乎找不到一个准确的阈值来告诉我有一个正确的匹配。在

我也尝试过用ORB来匹配openCV的特性,但是我的图像可能不够复杂,所以这些特性在整个页面上都匹配。在

对于模板匹配,我遵循文档中的基本代码:

# load the image image, convert it to grayscale, and detect edges
template = cv2.imread('PATH TO TEMPLATE IMAGE')
template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
template = cv2.Canny(template, 50, 200)
(tH, tW) = template.shape[:2]
cv2.imshow("Template", template)

match_list = []
# loop over the images to find the template in
for imagePath in glob.glob('PATH TO DIRECTORY OF IMAGES'):
    # load the image, convert it to grayscale, and initialize the
    # bookkeeping variable to keep track of the matched region
    image = cv2.imread(imagePath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    found = None

    # loop over the scales of the image
    for scale in np.linspace(0.2, 1.0, 20)[::-1]:
        # resize the image according to the scale, and keep track
        # of the ratio of the resizing
        resized = imutils.resize(gray, width = int(gray.shape[1] * scale))
        r = gray.shape[1] / float(resized.shape[1])

        # if the resized image is smaller than the template, then break
        # from the loop
        if resized.shape[0] < tH or resized.shape[1] < tW:
            break

        # detect edges in the resized, grayscale image and apply template
        # matching to find the template in the image
        edged = cv2.Canny(resized, 50, 200)
        result = cv2.matchTemplate(edged, template, cv2.TM_CCOEFF_NORMED)
        (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(result)
    match_list.append((maxVal, imagePath))

print(match_list)

我的问题是:

  1. 在使用哪种openCV进程方面,我的思路正确吗?

  2. 如果我使用模板匹配,我如何使用它来分类基于和精确匹配或接近精确匹配的图像?

谢谢!在

第2部分:这是我使用的模板匹配代码。在

^{pr2}$

This is my result

但请记住,我在许多传真上都这样做,模板匹配返回了太多误报,因此我需要一些建议,通过培训或图像处理,返回与模板匹配的正确传真。在

还有一个问题,如果传真图像是颠倒的,可以通过特征检测来识别传真,但我似乎无法通过模板匹配来工作。在

我希望我说得更清楚,谢谢。在


Tags: andofthetoin图像image模板