代码解析#设置参数 ap=argparse.ArgumentParser()#创建一个解析对象 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(ap.parse_args())#进
时间: 2023-11-12 09:05:06 浏览: 57
行参数解析,将解析结果保存在args字典中。
这段代码是用Python中的argparse模块解析命令行参数。首先创建一个ArgumentParser对象ap,然后使用add_argument方法添加需要关注的命令行参数和选项。其中,"-i"和"--image"是命令行中的参数名,required=True表示该参数是必须的,help是该参数的帮助信息。同理,"-t"和"--template"也是命令行参数,需要用户指定模板图片的路径。
最后,使用parse_args方法解析命令行参数,并将结果保存在args字典中。这个字典中包含了用户在命令行中指定的所有参数和选项的值。通过vars函数将解析结果转换为字典形式。
相关问题
解析代码from imutils import contours import numpy as np import argparse #参数设置包 import imutils #图像处理包 import cv2 import myutils#自定义包 #设置参数 ap=argparse.ArgumentParser()#创建一个解析对
象
ap.add_argument("-i", "--image", required=True, help="path to input image")#添加参数-i/--image,表示输入图片的路径
ap.add_argument("-r", "--reference", required=True, help="path to reference OCR-A image")#添加参数-r/--reference,表示OCR-A字体的标准图片路径
args=vars(ap.parse_args())#将解析的参数保存到 args 变量中
#定义一个函数,用于将图片转换为灰度图像并进行二值化处理
def pre_process(image, inv=False):
gray=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)#将图片转换为灰度图像
if inv:
gray=cv2.bitwise_not(gray)#取反,将黑色变为白色,将白色变为黑色
gray=cv2.GaussianBlur(gray, (5,5), 0)#高斯模糊
thresh=cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]#OTSU二值化
return thresh
#读取输入图片和OCR-A标准图片
image=cv2.imread(args["image"])
ref=cv2.imread(args["reference"])
#将输入图片和标准图片转换为灰度图像并进行二值化处理
gray=pre_process(image, inv=True)
ref=pre_process(ref)
#计算输入图片和标准图片的轮廓
refCnts=cv2.findContours(ref.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
refCnts=imutils.grab_contours(refCnts)
refCnts=myutils.sort_contours(refCnts, method="left-to-right")[0]
cnts=cv2.findContours(gray.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts=imutils.grab_contours(cnts)
locs=[]
#遍历输入图片的轮廓,与标准图片的轮廓进行比较,得到每个字符的位置信息
for (i, c) in enumerate(cnts):
(x, y, w, h)=cv2.boundingRect(c)
ar=w/float(h)
if ar>2.5 and ar<4.0:
if (w>40 and w<55) and (h>10 and h<20):
locs.append((x, y, w, h))
#将字符位置按照从左到右的顺序排序
locs=sorted(locs, key=lambda x:x[0])
#遍历每个字符,将其与标准图片的字符进行比较,得到字符的值
output=[]
for (i, (gX, gY, gW, gH)) in enumerate(locs):
groupOutput=[]
group=[]
roi=gray[gY-5:gY+gH+5, gX-5:gX+gW+5]
roi=cv2.resize(roi, (57, 88))
#计算每个字符与标准字符的差异值
for (j, ref) in enumerate(refCnts):
#将字符缩放至相同尺寸
refROI=cv2.resize(ref, (int(gW*1.7), gH))
#计算字符间的差异值
score=cv2.matchShapes(refROI, roi, cv2.CONTOURS_MATCH_I1, 0.0)
group.append((score, j))
#将字符按照差异值从小到大排序
group=sorted(group, key=lambda x:x[0])
#选取差异值最小的字符作为预测字符
groupOutput.append(str(chr(65+group[0][1])))
#将字符按照从左到右的顺序组合在一起
output.extend(groupOutput)
#输出识别结果
print("".join(output))
解析代码from imutils import contours import numpy as np import argparse #参数设置包 import imutils #图像处理包 import cv2 import myutils#自定义包 #设置参数 ap=argparse.ArgumentParser()#创建一个解析对象 ap.add_argument("-i","--image",required=True,help="path to input image")#向该对象中添加你要关
于的参数 ap.add_argument("-r","--reference",required=True,help="path to reference OCR-A image") ap.add_argument("-p","--preprocess",type=str,default="thresh",help="type of preprocessing to be done") args=vars(ap.parse_args())#将参数解析为字典形式 #读取图像 image=cv2.imread(args["image"])#读取输入参数指定的图像 gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)#将图像转换为灰度图 thresh=cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU)[1]#对灰度图进行二值化处理 #预处理 reference=cv2.imread(args["reference"])#读取参数指定的OCR-A字符图像 gray2=cv2.cvtColor(reference,cv2.COLOR_BGR2GRAY)#将OCR-A字符图像转换为灰度图 if args["preprocess"]=="thresh":gray2=cv2.threshold(gray2,0,255,cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU)[1]#二值化 thresh2=gray2 cv2.imshow("thresh2",thresh2)#显示处理后的OCR-A字符图像 cv2.waitKey(0) #调用 myutils.py 中的 sort_contours 函数,对图像中的轮廓进行排序 contours= cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) contours=imutils.grab_contours(contours) contours=myutils.sort_contours(contours,method="left-to-right") #用来存储结果的字符串,初始化为空 res="" #循环处理每个字符 for c in contours: #计算外接矩形并提取字符区域 (x,y,w,h)=cv2.boundingRect(c) roi=gray[y:y+h,x:x+w] #将字符区域重新调整为固定大小 image=cv2.resize(roi,(57,88)) #计算匹配得分 scores=[] #在模板中计算每个字符的匹配得分 for (digit,template) in digits.items(): #将模板调整为固定大小 template=cv2.resize(template,(57,88)) #计算匹配得分 score=cv2.matchTemplate(image,template,cv2.TM_CCOEFF) (_,score,_,_)=cv2.minMaxLoc(score) scores.append(score) #获取匹配得分最高的模板的索引值 maxIndex=int(np.argmax(scores)) res+=str(maxIndex) #在图像上绘制出结果 cv2.rectangle(image,(0,0),(57,88),(0,0,255),3) cv2.putText(image,res,(2,56),cv2.FONT_HERSHEY_SIMPLEX,0.75,(0,0,255),2) cv2.imshow("image",image) cv2.waitKey(0) #重点在于对输入参数进行解析,并读取对应的图像文件。然后进行二值化、轮廓排序等操作,最后对每个字符进行匹配。若匹配得分最高的模板索引值为 i,则将其转换为字符串,并依次拼接起来,最终得到完整的字符序列。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)