(附完整python源码)基于tensorflow、opencv的入门案例_发票识别二:字符分割

发票识别二

1.imread读入“发票代码区域”或者“发票号码码区域”;

2.调整高度,直方图均衡化,形态学,阈值分割。颜色反转:文字置为白色,背景置为黑色;

3.获取目标区域的最小外接矩形。消除被包围的轮廓,消除不合理尺寸的矩形;

4.为了便于后期输入到cnn网络,将图像设置为28×28,不拉伸,全0填充(仿射变换—平移缩放),保存并排序;

5.通过SVM判断矩形框内的图片是否数字,按从左到右顺序保存序列数字。

6.分割完毕,使用一个简单cnn模型逐个识别即可。(见发票识别三)


#encoding:utf-8
import cv2
import numpy as np
import util_funs
#将img的高度调整为28,先后对图像进行如下操作:直方图均衡化,形态学,阈值分割
def pre_treat(img):
	height_ = 28
	ratio_ = float(img.shape[1])/float(img.shape[0])
	gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
	gray = cv2.resize(gray,(int(ratio_*height_),height_))
	gray = cv2.equalizeHist(gray)
	_, binary = cv2.threshold(gray, 50, 255, cv2.THRESH_BINARY)
	img_ = 255 - binary #反转:文字置为白色,背景置为黑色
	return img_
#删掉不合理尺寸的矩形
def get_roi(contours):
	rect_list = []
	for i in range(len(contours)):
		rect = cv2.boundingRect(contours[i])
		if  rect2[3] > 10:
			rect_list.append(rect2)
	return rect_list
#获取目标区域的最小外接矩形
def get_rect(img):
	_,contours,hierarchy = cv2.findContours(img_, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
	rect_list = get_roi(contours)
	#消除内部轮廓,比如“0”内外各有1个轮廓
	del_rect = delete_(rect_list)
	return del_rect.solve()
#将图像设置为28×28,不拉伸,全0填充(仿射变换——平移缩放)
def change_(img):  
    length = 28  
    h,w = img.shape  
    H = np.float32([[1,0,(length-w)/2],[0,1,(length-h)/2]])  
    img = cv2.warpAffine(img,H,(length,length))  
    M = cv2.getRotationMatrix2D((length/2,length/2),0,26/float(img.shape[0]))  
    return cv2.warpAffine(img,M,(length,length))  
def get_img_list(rect_list):
     #保存所有初轮筛选出的图片于img_list中
	img_list = []
	for rect in rect_list:
		w1,w2 = rect2[0],rect2[0]+rect2[2]  
		h1,h2 = rect2[1],rect2[1]+rect2[3]
		img_list.append(change_(img.copy()[h1:h2,w1:w2])) 
	return img_list
def get_num_by_SVM(img_list):
	#通过SVM判断矩形框内的图片是否文数字
	#加载已经训练好的m文件。(训练过程在下面)
	svm_judge = joblib.load("train_model.m")
	num_list = []
	for img in img_list:
		if svm_judge.predict(img) == 1:
              		num_list.append(img)
	return num_list

def seg_num(img):
	img = pre_treat(img)
	rect_list = get_rect(img_)
	#从左到右边排序。"util_funs.py"见发票识别一
	rect_list = util_funs.sort_region(region,flag = 0)
	#保存所有初轮筛选出的图片于img_list中
	img_list = get_img_list(rect_list)
	img_num = get_num_by_SVM(img_list)

if __name__ == "__main__":
	img = cv2.imread("发票代码.jpg")
	img_num = seg_num(img)

你可能感兴趣的:((附完整python源码)基于tensorflow、opencv的入门案例_发票识别二:字符分割)