import cv2
import numpy as np
#读入图片,将图片转化为2值图,最后转化为数组
image = cv2.imread('C:/Users/wang/Desktop/test.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY_INV)
thresh = np.array(thresh)
path = "C:/Users/wang/Desktop/image/"
#统计每行黑点个数,参数为二值图数组
def countPoint(img):
re = []
for th in img:
re.append(sum(th)/255)
return re
#根据每行黑点个数选取进行切割的位置,参数为二值图数组,以及切割方向
def findPoint(img,axis):
if(axis == 1):
img = img.T
start = -1
end = -1
result = []
countx = countPoint(img)
for x in range(len(countx)):
if(countx[x] != 0 and start<0):
start = x-1
end = x+1
elif(countx[x] !=0):
end = x+1
elif(countx[x] == 0 and start>0):
result.append([start, end])
start, end = -1, -1
return result
#根据切割位置进行分割,参数为二值图数组,以及切割方向
def Cut(img,axis):
point = findPoint(img, axis)
re = []
for x in point:
if (axis == 0):
re.append(img[x[0]: x[1]])
elif(axis == 1):
re.append(img[:, x[0]: x[1]])
return re
#切割图像
def cutImage(img):
count = 1
for r_x in Cut(img,0):
i = 1
r_y = Cut(r_x,1)
while(i=1.5 and c<=2.0 ):
r = np.concatenate([r_y[i-1],r_y[i]],axis=1)
if(r.shape[1]>20 or r.shape[1]<13):
pass
else:
cv2.imwrite(path + str(count) + '.jpg', r)
count += 1
else:
cv2.imwrite(path + str(count) + '.jpg', r_y[i - 1])
count += 1
i+=1
cv2.imwrite(path + str(count) + '.jpg', r_y[i - 1])
count += 1
cutImage(thresh)
待说明