python 探测表格的横线和竖线,找到表格区域
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 探测表格的横线和竖线,找到表格区域相关的知识,希望对你有一定的参考价值。
# coding: utf-8
import cv2
class detectTable(object):
def __init__(self, image):
self.src_img = image
def run(self):
if len(self.src_img.shape) == 2: # 灰度图
gray_img = self.src_img
elif len(self.src_img.shape) ==3:
gray_img = cv2.cvtColor(self.src_img, cv2.COLOR_BGR2GRAY)
# thresh_img = cv2.adaptiveThreshold(~gray_img,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,15,-2)
thresh_img = cv2.Canny(gray_img, 50, 150, apertureSize=3)
h_img = thresh_img.copy()
v_img = thresh_img.copy()
scale = 20
h_size = int(h_img.shape[1]/scale)
h_structure = cv2.getStructuringElement(cv2.MORPH_RECT,(h_size,1)) # 形态学因子
h_erode_img = cv2.erode(h_img,h_structure,1)
h_dilate_img = cv2.dilate(h_erode_img,h_structure,1)
# cv2.imshow("h_erode",h_dilate_img)
v_size = int(v_img.shape[0] / scale)
v_structure = cv2.getStructuringElement(cv2.MORPH_RECT, (1, v_size)) # 形态学因子
v_erode_img = cv2.erode(v_img, v_structure, 1)
# cv2.imshow("v_erode_img",v_erode_img)
v_dilate_img = cv2.dilate(v_erode_img, v_structure, 1)
mask_img = h_dilate_img+v_dilate_img
joints_img = cv2.bitwise_and(h_dilate_img,v_dilate_img)
#在mask的基础上找边框
thresh1, contours1, hierarchy1 = cv2.findContours(h_dilate_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
image_copy=self.src_img.copy()
i=1
predict_area=[]
for cnt in contours1:
x, y, w, h = cv2.boundingRect(cnt)
if h>10 and w>12 and h<self.src_img.shape[0]-5 and w<self.src_img.shape[1]-5:
predict_area.append((x-3,y-3,w+x+3,y+h+3))
# cv2.rectangle(image_copy, (x, y), (x + w, y + h), (0, 255, 0), 5) # blue
cv2.imwrite("test.jpg",image_copy)
cv2.imshow("h_dilate_img",h_dilate_img)
cv2.imshow("mask",mask_img)
cv2.imshow("image_copy",image_copy)
cv2.waitKey(0)
return predict_area
def main():
temp_image=cv2.imread("./000217.png")
cc=detectTable(temp_image).run()
print(cc)
if __name__ == '__main__':
main()
以上是关于python 探测表格的横线和竖线,找到表格区域的主要内容,如果未能解决你的问题,请参考以下文章
怎样去掉word表格中的竖线,但是一定要保留横线?
latex插入表格:三线表格普通表格
latex中表格横竖线条加粗?
FastReport表格线问题
word表格被奇怪的横线分隔了,还有中间一大段空白拉不上去?
IDEA中这条竖线颜色怎么调?