Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python&;OpenCV:如何向无网格表格添加行_Python_Opencv_Computer Vision_Ocr_Cv2 - Fatal编程技术网

Python&;OpenCV:如何向无网格表格添加行

Python&;OpenCV:如何向无网格表格添加行,python,opencv,computer-vision,ocr,cv2,Python,Opencv,Computer Vision,Ocr,Cv2,我有下表: 我想写一个脚本,根据表格文本上的自然中断创建线条。结果如下所示: 是否有一个OpenCV实现使绘制这些线成为可能?我看了一下问题的答案,但都不起作用。解决此问题的最佳方法是什么?这里有一种在Python/OpenCV中获取水平线的方法,通过计算图像每行中的白色像素数来找到它们的中心y值。可以通过类似的过程添加垂直线 输入: 结果: 转换为灰度和阈值,使所有白色的东西都保持白色,其他的都变为黑色。然后计算每行中非零像素的数量(np.count\u nonzero)。你的线条会画

我有下表:

我想写一个脚本,根据表格文本上的自然中断创建线条。结果如下所示:


是否有一个OpenCV实现使绘制这些线成为可能?我看了一下问题的答案,但都不起作用。解决此问题的最佳方法是什么?

这里有一种在Python/OpenCV中获取水平线的方法,通过计算图像每行中的白色像素数来找到它们的中心y值。可以通过类似的过程添加垂直线

输入:


结果:


转换为灰度和阈值,使所有白色的东西都保持白色,其他的都变为黑色。然后计算每行中非零像素的数量(np.count\u nonzero)。你的线条会画在每组白色的线条中间,正是我需要的,谢谢!出于某种原因,大脑阻塞非常有用——比我能想出的更新更优雅:我运行了代码,它运行得很好。再次感谢您周到的回答!请参阅我答案中的添加内容,了解一个稍微简单的方法。简单的方法效果很好。如果不太麻烦的话,我想知道如何使用更简单的方法实现垂直线。我尝试将
column=cv2.resize(gray,(1,hh),interpolation=cv2.INTER_AREA)thresh=cv2.threshold(column,254,255,cv2.thresh_BINARY)[1]
更改为
row=cv2.resize(gray,(1,ww),interpolation=cv2.INTER_AREA)thresh=cv2.threshold(row,254,255,cv2.thresh_BINARY)[1]
但是调用
cv2.drawcours时,请参见仍在按行绘制的等高线。我错过什么了吗?这是我使用opencv的第一天,我几乎用第一个版本就可以工作了。我只是对中心值有点问题。我目前有
xcenter=y+h//2
,这会导致行稍微偏离。(我使用了另一个图像,因为实体标题干扰了列标识。)
import cv2
import numpy as np

# read image
img = cv2.imread("table.png")
hh, ww = img.shape[:2]

# convert to grayscale 
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# threshold gray image
thresh = cv2.threshold(gray, 254, 255, cv2.THRESH_BINARY)[1]

# count number of non-zero pixels in each row
count = np.count_nonzero(thresh, axis=1)

# threshold count at ww (width of image)
count_thresh = count.copy()
count_thresh[count==ww] = 255
count_thresh[count<ww] = 0
count_thresh = count_thresh.astype(np.uint8)

# get contours
contours = cv2.findContours(count_thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]

# loop over contours and get bounding boxes and ycenter and draw horizontal line at ycenter
result = img.copy()
for cntr in contours:
    x,y,w,h = cv2.boundingRect(cntr)
    ycenter = y+h//2
    cv2.line(result, (0,ycenter), (ww-1,ycenter), (0, 0, 0), 2)

# write results
cv2.imwrite("table_thresh.png", thresh)
cv2.imwrite("table_lines.png", result)

# display results
cv2.imshow("THRESHOLD", thresh)
cv2.imshow("RESULT", result)
cv2.waitKey(0)
import cv2
import numpy as np

# read image
img = cv2.imread("table.png")
hh, ww = img.shape[:2]

# convert to grayscale 
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# average gray image to one column
column = cv2.resize(gray, (1,hh), interpolation = cv2.INTER_AREA)

# threshold on white
thresh = cv2.threshold(column, 254, 255, cv2.THRESH_BINARY)[1]

# get contours
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]

# loop over contours and get bounding boxes and ycenter and draw horizontal line at ycenter
result = img.copy()
for cntr in contours:
    x,y,w,h = cv2.boundingRect(cntr)
    ycenter = y+h//2
    cv2.line(result, (0,ycenter), (ww-1,ycenter), (0, 0, 0), 2)

# write results
cv2.imwrite("table_lines2.png", result)

# display results
cv2.imshow("RESULT", result)
cv2.waitKey(0)