图像预处理
彩图二值化以减小参数量,再进行腐蚀膨胀去除噪点。
image = cv2.imread(‘F://demo.jpg’, 0) # 读取为灰度图
_, image = cv2.threshold(image, 50, 255, cv2.THRESH_BINARY) # 二值化
kernel1 = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7)) # 腐蚀膨胀核
kernel2 = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) # 腐蚀膨胀核
image = cv2.erode(image, kernel=kernel1) # 腐蚀
image = cv2.dilate(image, kernel=kernel2) # 膨胀
确定字符区域
考虑最理想的情况,图中的字符是端正没有倾斜歪曲的。将像素灰度矩阵分别进行列相加、行相加,则在得到的列和、行和数组中第一个非 0 元素索引到最后一个非 0 元素索引包裹的区间即就是字符区域。
h, w = image.shape # 原图的高和宽
list1 = [] # 列和
list2 = [] # 行和
for i in range(w):