'''
Created on 2019年3月15日@author: Qiuyi
'''from matplotlib import pyplot as plt
import numpy as np
import cv2rawimg = cv2.imread(r"D:\LearningFiles\splitCharactersOfPicture(python+cv2)\pyimagesplit1.jpg")# rawimg = cv2.imread(r"D:\LearningFiles\splitCharactersOfPicture(python+cv2)\6E57.jpg")
fig = plt.figure(figsize=(10,15))
fig.add_subplot(2,3,1)
plt.title("raw image")
plt.imshow(rawimg)fig.add_subplot(2,3,2)
plt.title("grey scale image")# 在處理之前,我們首先應該將圖像去RGB,即在它對應的灰度圖像上進行處理。# 我們可以使用opencv python庫中的cvtColor函數來實現到灰度圖像的轉換
grayscaleimg = cv2.cvtColor(rawimg,cv2.COLOR_BGR2GRAY)
plt.imshow(grayscaleimg,cmap='gray')# 然后,我們需要對圖片進行歸一化,這樣可以減少最后分割出的數字中的噪聲# 這里我們采取了對每個像素減去圖像總像素的平均數,并設置閾值50以下的像素歸零來實現歸一化# 這樣基本上背景像素就變成0了
grayscaleimg = grayscaleimg -int(np.mean(grayscaleimg))
grayscaleimg[grayscaleimg <50]=0# counting non-zero value by row , axis y# 可以得到字符高的邊界
row_nz =[]for row in grayscaleimg.tolist():row_nz.append(len(row)- row.count(0))
fig.add_subplot(2,3,3)
plt.title("non-zero values on y (by row)")
plt.plot(row_nz)# counting non-zero value by column, x axis# 可以得到字符寬的邊界,波形的波谷即間隔
col_nz =[]for col in grayscaleimg.T.tolist():col_nz.append(len(col)- col.count(0))
fig.add_subplot(2,3,4)
plt.title("non-zero values on y (by col)")
plt.plot(col_nz)##### start split# first find upper and lower boundary of y (row)
fig.add_subplot(2,3,5)
plt.title("y boudary deleted")
upper_y =0# 遇到行不為0,即有數字時,記錄行數for i,x inenumerate(row_nz):if x !=0:upper_y = ibreak
lower_y =0for i,x inenumerate(row_nz[::-1]):if x!=0:lower_y =len(row_nz)- ibreak
sliced_y_img = grayscaleimg[upper_y:lower_y,:]
plt.imshow(sliced_y_img)# then we find left and right boundary of every digital (x, on column)
column_boundary_list =[]
record =False# list[:-1],slice all the list without the last onefor i,x inenumerate(col_nz[:-1]):# 尋找邊界iif(col_nz[i]==0and col_nz[i+1]!=0)or col_nz[i]!=0and col_nz[i+1]==0:column_boundary_list.append(i+1)
img_list =[]# i是所有左邊界,[i:i+2]切片得到每個字符的左右邊界
xl =[ column_boundary_list[i:i+2]for i inrange(0,len(column_boundary_list),2)]for x in xl:img_list.append(sliced_y_img[:,x[0]:x[1]])# del invalid image# 刪去寬度不大于5像素的錯誤圖片
img_list =[ x for x in img_list if x.shape[1]>5]# show image
fig = plt.figure()
plt.title("x boudary deleted")for i,img inenumerate(img_list):fig.add_subplot(3,4,i+1)plt.imshow(img)plt.imsave(r"C:\Users\Qiuyi\Desktop\%s.jpg"%i,img)plt.show()