OPENCV提取图片中的文字
@Fu Xianjun. All Rights Reserved.
前言
日常生活中我们可能需要从图片中提取文字,然而神奇的OPENCV也能帮助我们完成这个,而且更加的随心所欲,可能把
提示:以下是本篇文章正文内容,下面案例可供参考
一、使用OPENCV提取图片文字
我们需要使用各种函数,还有之前的透视等等,来处理这个图片,并且还要提取文字
二、使用步骤
1.引入库
代码如下(示例):
import cv2 import numpy as np import os
对,没错,就这么点
2.代码如下
代码如下(示例):
def order_point(pts):
rect = np.zeros((4,2),dtype= "float32")
s = pts.sum(axis = 1)
rect[0]= pts[np.argmin(s)]
rect[2]= pts[np.argmax(s)]
d = np.diff(pts,axis = 1)
rect[1]= pts[np.argmin(d)]
rect[3]= pts[np.argmax(d)]
return rect
def four_point_transform(img,pts):
rect= order_point(pts)
(tl,tr,br,bl) = rect
widthA = np.sqrt(((br[0]-bl[0])**2)+((br[1]-bl[1])**2))
widthB = np.sqrt(((tr[0]-tl[0])**2)+((tr[1]-tl[1])**2))
maxWidth = max(int(widthA),int(widthB))
heightA = np.sqrt(((tr[0]-br[0])**2)+((tr[1]-br[1])**2))
heightB = np.sqrt(((bl[0]-tl[0])**2)+((bl[1]-tl[1])**2))
maxHeight = max(int(heightA),int(heightB))
dst = np.array([[0,0],[maxWidth -1,0],[maxWidth -1,maxHeight -1],[0,maxHeight -1]],dtype = "float32")
M= cv2.getPerspectiveTransform(rect,dst)
warp = cv2.warpPerspective(img,M,(maxWidth,maxHeight))
return warp
img = cv2.imread("1.jpg")
h,w = img.shape[:2]
ratio = 500/h
img= cv2.resize(img,(int(1000*w/h),1000))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edge = cv2.Canny(gray,75,255)
cnts,h = cv2.findContours(edge,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
# cv2.drawContours(img,cnts,-1,(255,0,0),2)
cnts = sorted(cnts, key = cv2.contourArea,reverse = True)
for c in cnts:
peri = cv2.arcLength(c,True)
approx = cv2.approxPolyDP(c,0.02*peri,True)
if len(approx)==4:
screenCnt = approx
break
cv2.drawContours(img,screenCnt,-1,(255,0,0),10)
warp = four_point_transform(img,screenCnt.reshape(4,2))
cv2.imwrite("123.jpg",warp)
cmd = "resseract 123.jpg result"
os.system(cmd)
cv2.imshow("img",img)
cv2.imshow("warp",warp)
cv2.waitKey()
cv2.destroyAllWindows()
下面就是提取之后的结果
-
下面就是透视成的图片,和提取后的文字
总结
很简单粗暴的就提取出来啦,就是这样
