我正在使用下面的脚本try 将手写文本从书写文本的行中分离出来.目前我正在try Select 行.
当直线为实线时,这似乎效果很好,但当直线为一串点时,它变得很复杂.为了解决这个问题,我试着用deflate把点变成实线,但deflate也把文本变成实线,然后把它变成水平线.我可以 for each 图像调整kernel
,但在处理千分之一的图像时,这不是一个可行的解决方案.
有人能建议我怎么做吗.这是最好的方法还是有更好的方法来 Select 这些线路?
import cv2
file_path = r'image.jpg'
image = cv2.imread(file_path)
# resize image if image is bigger then screen size
print('before Dimensions : ', image.shape)
if image.shape[0] > 1200:
image = cv2.resize(image, None, fx=0.2, fy=0.2)
print('after Dimensions : ', image.shape)
result = image.copy()
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Applying dilation to make lines solid
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
dilation = cv2.dilate(thresh, kernel, iterations = 1)
# Detect horizontal lines
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (40,1))
detect_horizontal = cv2.morphologyEx(dilation, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(detect_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
cv2.drawContours(result, [c], -1, (36,255,12), 2)
cv2.imshow('1- gray', gray)
cv2.imshow("2- thresh", thresh)
cv2.imshow("3- detect_horizontal", detect_horizontal)
cv2.imshow("4- result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()