如何使用opencv集中和调整数字大小? [英] How to centralize and resize digits using opencv?

查看:55
本文介绍了如何使用opencv集中和调整数字大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对一些扫描的表格(手工填写)进行 OCR.这是我第一次使用计算机视觉做一些严肃的事情.到目前为止,我能够找到包含日期字段数字的方块:

I want to OCR some scanned forms (filled by hand). It is the first time I do something serious using computer vision. So far I'm able to locate the squares containing digits for a date field:

查看 OpenCV 附带的示例手写数字数据集,我看到数字被集中并调整为 (20, 20):

Looking at the example handwritten digit dataset that comes with OpenCV, I see digits are centralized and resized to (20, 20):

由于这可能是一个相当普遍的问题,我想知道该算法是否已经在 OpenCV(或 numpyscipy等),这样我就不必重新发明轮子了.

Since this may be a fairly common problem, I'm wondering if the algorithm is already implemented in OpenCV (or numpy, scipy, etc) so I don't have to reinvent the wheel.

问题是:Python 中是否有内置管道来规范化样本?

推荐答案

不确定的内置管道,但您可以通过执行以下操作(基于我的评论)来实现自己的,因为您已经有了轮廓:

A built in pipeline not sure, but you could implement your own, given you already have the contours, by doing the following (based on my comment):

获取轮廓的边界矩形(因此以它为中心)并裁剪该部分:

Obtain Bounding rectangle of contour (therefore centering on it) and crop that part :

x,y,w,h = cv2.boundingRect(cnt)
imgCrop = img[x:(x+w), y:(y+h)]

将图像调整为所需大小(例如 20 x 20):

Resize image to desired size (say 20 x 20):

imgResized = cv2.resize(imgCrop, (20,20))   

您还可以按特定比例调整轴的大小,例如:

You can also resize axes by a specific ratio like:

imgResized = cv2.resize(imgCrop, (0,0), fx=0.5, fy=0.5)  

或使用 scipy(如 这个问题):

or with scipy (as suggested in this question):

imgResized = scipy.misc.imresize(imgCrop, 0.5)  

奖励:检查 this 关于使用 Python 和 OpenCV 进行基本图像处理的很棒的教程,其中展示了其他方法来调整大小,同时考虑纵横比和插值以获得更好的结果,从中提取:

Bonus: Check this great tutorial on basic image manipulation with Python and OpenCV, where they show other way to resize taking into account aspect ratio and interpolations for better results, extracting from it:

imgResized = cv2.resize(imgCrop, (20,20), interpolation = cv2.INTER_AREA)

这篇关于如何使用opencv集中和调整数字大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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