根据帖子Vertical projection and horizontal projection 我试图得到没有运气的字符分离结果。
img = cv2.imread(image_name, 0)
img = cv2.bitwise_not(img)
y = verticalProjection(img)
plt.plot(y)
height_hist = np.histogram(y, bins=400)
# Find bin with the second largest number of values:
bin = np.argsort(height_hist[0])[-2]
print(bin)
# Get the limit values of the bin:
y_low, y_high = height_hist[1][bin], height_hist[1][bin + 1]
print(y_low, y_high)
# Go over the vertical projection values and separate to characters:
zero = y[0] # Assuming the first projected value is outside of the word
char_list = {}
i = 0
char_list_dict = defaultdict(list)
inside_char = False
counter = 0
while i < len(y):
if y[i] != zero:
start = i # start of char
# Find end of current char:
for j in range(i, len(y)):
if y_low <= y[i] and y[i] <= y_high:
end = j # end of char
char_list_dict[counter].append([start, end]) # add to char list
i = end
# Find the start of the next char:
for j in range(i, len(y)):
if y_low > y[i] or y[i] > y_high:
i = j
counter = counter + 1
else:
i += 1
print(char_list)
lines = [[]]
prev = char_list[0][0]
for x0, x1 in char_list_dict:
print("coords:", x1.values())
image_slice=y[x0:180]
plt.plot(image_slice)
plt.show()
all_slices = []