简体   繁体   English

使用来自 opencv 的 findcontours 提取表

[英]extraction of table using findcontours from opencv

I need to extract the table from few sets of engineering drawings.我需要从几组工程图纸中提取表格。 The main goal is to detect and extract the table with I tried using find Contours but seems like it doesn't output the right result for me.主要目标是通过我尝试使用 find Contours 来检测和提取表格,但似乎 output 对我来说不是正确的结果。 original image: original engineering drawing expected output: this is the result I want原图:原工程图预期 output:这是我想要的结果

code代码

image = cv2.imread('01.png')
original = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]


cnts = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.05 * peri, True)
    if len(approx) == 4:
        cv2.drawContours(thresh, [c], -1, (255,255,255), -1)


kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (40,10))
close = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2)


cnts = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.05 * peri, True)
    x,y,w,h = cv2.boundingRect(approx)
    aspect_ratio = w / float(h)
    area = cv2.contourArea(approx)
    if len(approx) == 4:
        cv2.drawContours(image, [c], -1, (36,255,12), -1)
        crop = original[y:y+h, x:x+w]

cv2.imwrite('image.png', image)
cv2.imwrite('ROI.png', crop)
cv2.waitKey()

If you have all image same size and shape and excreted part is always on same side.如果您的所有图像大小和形状都相同,并且排泄的部分总是在同一侧。 why are you going toward findContours.你为什么要去findContours。 I think, you should define ROI coordinate and crop the part.我认为,您应该定义 ROI 坐标并裁剪该部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM