本文主要是介绍使用Python+OpenCV2进行图片中的文字分割(支持竖版),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
把图片中的文字,识别出来,并将每个字的图片抠出来;
import cv2
import numpy as npHIOG = 50
VIOG = 3
Position = []'''水平投影'''
def getHProjection(image):hProjection = np.zeros(image.shape,np.uint8)# 获取图像大小(h,w)=image.shape# 统计像素个数h_ = [0]*hfor y in range(h):for x in range(w):if image[y,x] == 255:h_[y]+=1#绘制水平投影图像for y in range(h):for x in range(h_[y]):hProjection[y,x] = 255# cv2.imshow('hProjection2',cv2.resize(hProjection, None, fx=0.3, fy=0.5, interpolation=cv2.INTER_AREA))# cv2.waitKey(0)return h_def getVProjection(image):vProjection = np.zeros(image.shape,np.uint8);(h,w) = image.shapew_ = [0]*wfor x in range(w):for y in range(h):if image[y,x] == 255:w_[x]+=1for x in range(w):for y in range(h-w_[x],h):vProjection[y,x] = 255# cv2.imshow('vProjection',cv2.resize(vProjection, None, fx=1, fy=0.1, interpolation=cv2.INTER_AREA))# cv2.waitKey(0)return w_def scan(vProjection, iog, pos = 0):start = 0V_start = []V_end = []for i in range(len(vProjection)):if vProjection[i] > iog and start == 0:V_start.append(i)start = 1if vProjection[i] <= iog and start == 1:if i - V_start[-1] < pos:continueV_end.append(i)start = 0return V_start, V_enddef checkSingle(image):h = getHProjection(image)start = 0end = 0for i in range(h):pass#分割
def CropImage(image,dest,boxMin,boxMax):a=boxMin[1]b=boxMax[1]c=boxMin[0]d=boxMax[0]cropImg = image[a:b,c:d]cv2.imwrite(dest,cropImg)#开始识别
def DOIT(rawPic):# 读入原始图像origineImage = cv2.imread(rawPic)# 图像灰度化 #image = cv2.imread('test.jpg',0)image = cv2.cvtColor(origineImage,cv2.COLOR_BGR2GRAY)# cv2.imshow('gray',image)# 将图片二值化retval, img = cv2.threshold(image,127,255,cv2.THRESH_BINARY_INV)# kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))# img = cv2.erode(img, kernel)# cv2.imshow('binary',cv2.resize(img, None, fx=0.3, fy=0.3, interpolation=cv2.INTER_AREA))#图像高与宽(h,w)=img.shape#垂直投影V = getVProjection(img)start = 0V_start = []V_end = []# 对垂直投影水平分割V_start, V_end = scan(V, HIOG)if len(V_start) > len(V_end):V_end.append(w-5)# 分割行,分割之后再进行列分割并保存分割位置for i in range(len(V_end)):#获取行图像if V_end[i] - V_start[i] < 30:continuecropImg = img[0:h, V_start[i]:V_end[i]]# cv2.imshow('cropImg',cropImg)# cv2.waitKey(0)#对行图像进行垂直投影H = getHProjection(cropImg)H_start, H_end = scan(H, VIOG, 40)if len(H_start) > len(H_end):H_end.append(h-5)for pos in range(len(H_start)):# 再进行一次列扫描DcropImg = cropImg[H_start[pos]:H_end[pos], 0:w]d_h, d_w = DcropImg.shape# cv2.imshow("dcrop", DcropImg)sec_V = getVProjection(DcropImg)c1, c2 = scan(sec_V, 0)if len(c1) > len(c2):c2.append(d_w)x = 1while x < len(c1):if c1[x] - c2[x-1] < 12:c2.pop(x-1)c1.pop(x)x -= 1x += 1# cv2.waitKey(0)if len(c1) == 1:Position.append([V_start[i],H_start[pos],V_end[i],H_end[pos]])else:for x in range(len(c1)):Position.append([V_start[i]+c1[x], H_start[pos],V_start[i]+c2[x], H_end[pos]])#根据确定的位置分割字符number=0for m in range(len(Position)):rectMin = (Position[m][0]-5,Position[m][1]-5)rectMax = (Position[m][2]+5,Position[m][3]+5)cv2.rectangle(origineImage,rectMin, rectMax, (0 ,0 ,255), 2)number=number+1#start-cropCropImage(origineImage,'result/' + '%d.jpg' % number,rectMin,rectMax)# cv2.imshow('image',cv2.resize(origineImage, None, fx=0.6, fy=0.6, interpolation=cv2.INTER_AREA))cv2.imshow('image',origineImage)cv2.imwrite('result/' + 'ResultImage.jpg' , origineImage)cv2.waitKey(0)#############################
rawPicPath = r"H:\TEMP\TEXT_PROCCESS\TEST05.jpg"
DOIT(rawPicPath)
#############################
这篇关于使用Python+OpenCV2进行图片中的文字分割(支持竖版)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!