如果初始图像如上所示,那么我可以成功地在两行之间引入空格,并得到下面的图像。
使用以下代码:
上述代码将消除偏斜并引入空间。 但在某些情况下,上述代码无效。 这些情况包括:
图像的输出为
如何处理此类情况?
注意:
我尝试将其调整为更大的尺寸,并进行逐像素迭代和构建自定义算法以解决此问题,但这需要大量时间来解决,并且有时会出现内存错误。
请注意:上述代码的输入实际上是此处提供的图像的反向图像(白色背景)。
![enter image description here](https://istack.dev59.com/SxT6o.webp)
import os
import cv2
def space_between_lines_and_skewness_correction(file_path):
img = cv2.imread(os.path.expanduser(file_path))
grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
th, threshed = cv2.threshold(grey, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
pts = cv2.findNonZero(threshed)
ret = cv2.minAreaRect(pts)
(cx, cy), (w, h), ang = ret
if w < h:
w, h = h, w
ang += 90
M = cv2.getRotationMatrix2D((cx, cy), ang, 1.0)
rotated = cv2.warpAffine(threshed, M, (img.shape[1], img.shape[0]))
hist = cv2.reduce(rotated, 1, cv2.REDUCE_AVG).reshape(-1)
th = 2
H, W = img.shape[:2]
delimeter = [y for y in range(H - 1) if hist[y] <= th < hist[y + 1]]
arr = []
y_prev = 0
y_curr = 0
for y in delimeter:
y_prev = y_curr
y_curr = y
arr.append(rotated[y_prev:y_curr, 0:W])
arr.append(rotated[y_curr:H, 0:W])
space_arr = np.zeros((10, W))
final_img = np.zeros((1, W))
for im in arr:
v = np.concatenate((space_arr, im), axis=0)
final_img = np.concatenate((final_img, v), axis=0)
return final_img
上述代码将消除偏斜并引入空间。 但在某些情况下,上述代码无效。 这些情况包括:
![enter image description here](https://istack.dev59.com/cvbIU.webp)
![enter image description here](https://istack.dev59.com/y52g2.webp)