如何使用opencv python检测并增加文本图像中两行之间的间距?
如果初始图像是这样的(上图),那么我可以成功地在两行之间引入空格并获得此图像(下图) 使用以下代码:如何使用opencv python检测并增加文本图像中两行之间的间距?,python,opencv,image-processing,Python,Opencv,Image Processing,如果初始图像是这样的(上图),那么我可以成功地在两行之间引入空格并获得此图像(下图) 使用以下代码: 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
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
导入操作系统
进口cv2
定义线与偏斜校正之间的间距(文件路径):
img=cv2.imread(os.path.expanduser(文件路径))
灰色=cv2.CVT颜色(img,cv2.COLOR\U BGR2GRAY)
th,threshed=cv2.阈值(灰色,0,255,cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
pts=cv2.findNonZero(脱粒)
ret=cv2.尖塔(pts)
(cx,cy),(w,h),ang=ret
如果w
def detect_letters(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# just to remove noise
thresh_val, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
num_labels, _, stats, centroids = cv2.connectedComponentsWithStats(thresh)
for i in range(num_labels):
leftmost_x = stats[i, cv2.CC_STAT_LEFT]
topmost_y = stats[i, cv2.CC_STAT_TOP]
width = stats[i, cv2.CC_STAT_WIDTH]
height = stats[i, cv2.CC_STAT_HEIGHT]
# enclose all detected components in a blue rectangle
cv2.rectangle(img, (leftmost_x, topmost_y), (leftmost_x + width, topmost_y + height), (255, 0, 0), 2)
cv2.imshow("window", img)
cv2.waitKey(0) & 0xFF
输入:
输出:
上述解决方案的主要目的只是在每个字母周围获得一个封闭矩形
现在你所需要做的就是把所有这些字母移到上面或下面,或者你想移到哪里就移到哪里
例如,在以下链接中查看足球是如何移动的:
正如您现在知道的每个字母的最上面和最下面的y坐标一样,您可以看到它们当前有多远,如果它们非常接近,只需按照上面的链接移动字母即可
同一行上的字母在顶点坐标或质心上的差别很小。你可以有一个公差范围来找出所有这些字母
如果有任何问题,请随时提问