Python_科学计算库

Python_科学计算库 标签:extern sar int bsp += nbsp sha IS

Python_科学计算库

标签:externsarintbsp+=nbspshaISEhid

文字识别

# -*- coding: utf-8 -*-
\”\”\”
简介:用样本训练数据,再识别
\”\”\”

import cv2
import numpy as np
from PIL import Image #Python Image Lib
import skimage.feature as feature2d
import sklearn.neighbors as nhb
from sklearn.externals import joblib #对训练模型保存或读取
#cvhog=cv2.HOGDescriptor()

#预处理图片
def imgPrepare(filename):
img=cv2.imread(filename,0)
img=np.uint8(img/img.ptp()*255)
img=np.where(img>128,255,img)
img=np.where(img<=128,0,img)
img=np.bitwise_not(img)
return img

#横切
def splitchar(img,axis=1):
idxrowb=np.all(img<64,axis=axis)
idxrowi=np.uint8(idxrowb).ravel()
dy=idxrowi[1:]-idxrowi[:-1]
#print(dy)
rowb=np.argwhere(dy==255).ravel()
rowe=np.argwhere(dy==1).ravel()
#print(rowb,rowe)
if axis==1:
imglines=[img[b:e+1,:] for b,e in zip(rowb,rowe)]
else:
imglines=[img[:,b:e+1] for b,e in zip(rowb,rowe)]

return imglines

#切块
def splitBox(img):
idxrowb=np.all(img<64,axis=1)
idxrowi=np.uint8(idxrowb).ravel()
dy=idxrowi[1:]-idxrowi[:-1]
#print(dy)
rowb=np.argwhere(dy==255).ravel()
rowe=np.argwhere(dy==1).ravel()
b=0
e=-1
if len(rowe)>0:
e=rowe[-1]+1
if len(rowb)>0:
b=rowb[0]
return img[b:e,:]

#

作者: 安兔兔

为您推荐

返回顶部