参考教程:唐宇迪老师: https://www.bilibili.com/video/BV1tb4y1C7j7
1.参数配置:
step1:
step2:找到Edit Configurations…
step3:找到Parameters一栏
step4:编辑图片、模板的路径(不要有中文,不要有空格)
2.程序代码:
myutils模块部分:
import cv2
def sort_contours(cnts, method="left-to-right"):
reverse = False
i = 0
if method == "right-to-left" or method == "bottom-to-top":
reverse = True
if method == "top-to-bottom" or method == "bottom-to-top":
i = 1
#左上角横坐标的大小
#cv2.boundingRect(c)返回四个值,x,y,h,w
boundingBoxes = [cv2.boundingRect(c) for c in cnts] #用一个最小的矩形,把找到的形状包起来x,y,h,w
(cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),
key=lambda b: b[1][i], reverse=reverse)) #reverse = True 降序 , reverse = False 升序(默认),b[1][0]按x坐标排序
return cnts, boundingBoxes
def resize(image, width=None, height=None, inter=cv2.INTER_AREA): #cv2.INTER_AREA 基于局部像素的重采样
dim = None
(h, w) = image.shape[:2]
if width is None and height is None:
return image
if width is None:
r = height / float(h)
dim = (int(w * r), height)
else:
r = width / float(w)
dim = (width, int(h * r))
resized = cv2.resize(image, dim, interpolation=inter) #resize函数的参数dsize的形状是(w,h)
return resized
工作程序部分:
# 导入工具包
from imutils import contours
import numpy as np
import argparse
import cv2
import myutils
# 设置参数
ap = argparse.ArgumentParser() #argparse 模块可以让人轻松编写用户友好的命令行接口
ap.add_argument("-i", "--image", required=True,
help="path to input image")
ap.add_argument("-t", "--template", required=True,
help="path to template OCR-A image")
args = vars