我正在使用下面的脚本try 将手写文本从书写文本的行中分离出来.目前我正在try Select 行.

当直线为实线时,这似乎效果很好,但当直线为一串点时,它变得很复杂.为了解决这个问题,我试着用deflate把点变成实线,但deflate也把文本变成实线,然后把它变成水平线.我可以 for each 图像调整kernel,但在处理千分之一的图像时,这不是一个可行的解决方案.

有人能建议我怎么做吗.这是最好的方法还是有更好的方法来 Select 这些线路?

Sample images enter image description here

enter image description here

enter image description here

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()

推荐答案

通过查找等高线,我们可以使用cv2.contourArea按面积消除较小的等高线.这将在图像包含虚线的假设下工作.

Code:

# read image, convert to grayscale and apply Otsu threshold
img = cv2.imread('text.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
th = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# create black background of same image shape
black = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)

# find contours from threshold image
contours = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]

# draw contours whose area is above certain value
area_threshold = 7
for c in contours:
    area = cv2.contourArea(c)
    if area > area_threshold:
        black = cv2.drawContours(black,[c],0,(255,255,255),2)

black:

enter image description here

为了对更多图像进行细化,可以使用一些统计度量(如均值、中值等)过滤轮廓

Python相关问答推荐

配置Sweetviz以分析对象类型列,而无需转换

带条件计算最小值

将输入管道传输到正在运行的Python脚本中

用合并列替换现有列并重命名

从groupby执行计算后创建新的子框架

如何设置视频语言时上传到YouTube与Python API客户端

递归访问嵌套字典中的元素值

在含噪声的3D点网格中识别4连通点模式

如何在Python中获取`Genericums`超级类型?

解决调用嵌入式函数的XSLT中表达式的语法移位/归约冲突

* 动态地 * 修饰Python中的递归函数

Matplotlib中的字体权重

Beautifulsoup:遍历一个列表,从a到z,并解析数据,以便将其存储在pdf中.

判断Python操作:如何从字面上得到所有decorator ?

如何求相邻对序列中元素 Select 的最小代价

如何获取包含`try`外部堆栈的`__traceback__`属性的异常

将数字数组添加到Pandas DataFrame的单元格依赖于初始化

使用xlsxWriter在EXCEL中为数据帧的各行上色

极点用特定值替换前n行

为什么在不先将包作为模块导入的情况下相对导入不起作用