OpenCV模板匹配或功能检测,以正确分类传真类型?

时间:2018-01-03 16:56:54

标签: python python-3.x opencv feature-detection template-matching

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

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

我尝试过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. 如果我使用模板匹配,我如何使用它来根据精确或接近完全匹配对图像进行分类?

  3. 谢谢!

    第2部分:这是我去过的模板匹配代码。

    template = cv2.imread('/Users/ME/Documents/so_temp.tiff',0)
    w, h = template.shape[::-1]
    
    match_values = []
    for imagePath in glob.glob('/Users/ME/Documents' + "/*.tiff"):
        # load the image, convert it to grayscale, and initialize the
        # bookkeeping variable to keep track of the matched region
        img = cv2.imread(imagePath)
        img2 = img.copy()
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
        methods = ['cv2.TM_CCORR_NORMED']
    
        for meth in methods:
            img = img2.copy()
            method = eval(meth)
    
            # Apply template Matching
            res = cv2.matchTemplate(gray,template,method)
            min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    
            match_values.append((min_val, max_val, imagePath))
            print (min_val, max_val, imagePath)
    
            # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
            if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
                top_left = min_loc
            else:
                top_left = max_loc
            bottom_right = (top_left[0] + w, top_left[1] + h)
    
            cv2.rectangle(img,top_left, bottom_right, 255, 2)
    
            plt.subplot(121),plt.imshow(res,cmap = 'gray')
            plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
            plt.subplot(122),plt.imshow(img,cmap = 'gray')
            plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
            plt.suptitle(meth)
    
            plt.show()
    

    This is my result

    但请记住,我通过许多传真执行此操作并且模板匹配会返回太多误报,所以我需要建议我可以通过培训或图像处理来做什么来返回匹配的正确传真模板。

    如果传真图像是颠倒的,也存在识别传真的问题,这可以通过功能检测来完成,但我似乎无法通过模板匹配来完成工作。

    我希望我更清楚,谢谢你。

0 个答案:

没有答案