倾斜的字符串数字因为垂直投影后的直方图存在区域连接的问题,直接对数字进行分割较为困难,因此在数字串分割前对数字串进行倾斜校正,校正后分割数字明显可以提高分割的准确度。
import numpy as np
import os
import cv2
import random
import string
import glob
import os.path
import time
from PIL import Image
def rotate(image, angle, center=None, scale=1.0):
(w, h) = image.shape[0:2]
if center is None:
center = (w // 2, h // 2)
wrapMat = cv2.getRotationMatrix2D(center, angle, scale)
return cv2.warpAffine(image, wrapMat, (h, w))
# 使用矩形框
def getCorrect():
# 读取图片,灰度化
path = 'D:\\picture\\test1\\test\\num\\' ##原图存放地址
save_correct = 'D:\\picture\\test1\\test\\correct\\' ##倾斜矫正后的图片存放地址
paths = glob.glob(os.path.join(path, '*.jpg'))
for filename in paths:
img = cv2.imread(filename)
print(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 转换了灰度化
# 图像取非
grayNot = cv2.bitwise_not(gray)
# OTSU二值化
threImg = cv2.threshold(grayNot, 0, 255, cv2.THRESH_OTSU, )[1]
# 获得有文本区域的点集,求点集的最小外接矩形框,并返回旋转角度
coords = np.column_stack(np.where(threImg > 0))
angle = cv2.minAreaRect(coords)[-1]
if angle < -45:
angle = -(angle + 90)
else:
angle = -angle
# 仿射变换,将原图校正
dst = rotate(threImg, angle) #threImg校正二值化图片,src校正原图
#cv2.imshow("dst", dst)
salt = ''.join(random.sample(string.digits, 8)) # 随机输出8位由数字组成的字符串
cv2.imwrite(save_correct + salt + '.jpg', dst) # 存储裁剪得到的图像
#cv2.waitKey()
#print(angle)
###############分割字符#####################
# 1、读取图像,并把图像转换为灰度图像并显示
## img = cv2.imread("D:/picture/test1/1.png") # 读取图片
#img_gray = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY) # 转换了灰度化
# cv2.imshow('gray', img_gray) # 显示图片
# 2、将灰度图像二值化,设定阈值是100
#img_thre = img_gray
#cv2.threshold(img_gray, 0, 255, cv2.THRESH_OTSU, img_thre)
#cv2.imshow('threshold', img_thre)
# 3、保存黑白图片
# cv2.imwrite('thre_res.png', img_thre)
# 4、分割字符
white = [] # 记录每一列的白色像素总和
black = [] # ..........黑色.......
height = dst.shape[0]
width = dst.shape[1]
white_max = 0
black_max = 0
# 计算每一列的黑白色像素总和
for i in range(width):
s = 0 # 这一列白色总数
t = 0 # 这一列黑色总数
for j in range(height):
if dst[j][i] == 255:
s += 1
if dst[j][i] == 0:
t += 1
white_max = max(white_max, s)
black_max = max(black_max, t)
white.append(s)
black.append(t)
# print(s)
# print(t)
arg = False # False表示白底黑字;True表示黑底白字
if black_max > white_max:
arg = True
# 分割图像
def find_end(start_):
end_ = start_ + 1
for m in range(start_ + 1, width - 1):
if (black[m] if arg else white[m]) > (
0.85 * black_max if arg else 0.85 * white_max): # 0.95这个参数请多调整,对应下面的0.05,这个参数控制分割的阈值
end_ = m
break
return end_
n = 1
start = 1
end = 2
while n < width - 2:
n += 1
if (white[n] if arg else black[n]) > (0.15 * white_max if arg else 0.15 * black_max):
# 上面这些判断用来辨别是白底黑字还是黑底白字
# 0.05这个参数请多调整,对应上面的0.95
start = n
end = find_end(start)
n = end
if end - start > 5:
cj = dst[1:height, start:end]
#cv2.imshow('caijian', cj)
salt = ''.join(random.sample(string.digits, 8)) # 随机输出8位由数字组成的字符串
cv2.imwrite(r"D:\\picture\\test1\\test\\seg\\" + salt + '.jpg',cj) # 存储裁剪得到的图像
#cv2.waitKey(0)
if __name__ == "__main__":
start = time.time()
getCorrect()
end = time.time()
print("Execution Time:", end - start)