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, 127, 255, cv2.THRESH_BINARY_INV)
thresh = np.array(thresh)
#统计每行黑点个数,参数为二值图数组
def countPoint(img):
re = []
for th in img:
re.append(sum(th)/255)
return re
#根据每行黑点个数选取进行切割的位置,参数为二值图数组,以及切割方向
def findPoint(img,axis):
if(axis == 'y'):
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
elif(countx[x] !=0):
end = x
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 == 'x'):
re.append(img[x[0]: x[1]])
elif(axis == 'y'):
re.append(img[:, x[0]: x[1]])
return re
#绘制切割后的图像
def cutImage(img):
count = 1
for r_x in Cut(img,'x'):
for r_y in Cut(r_x, 'y'):
cv2.imwrite("C:/Users/wang/Desktop/image/" + str(count) + ".jpg", r_y)
count += 1
cutImage(thresh)
未完待续