简体   繁体   中英

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. original image: original engineering drawing expected output: this is the result I want

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. I think, you should define ROI coordinate and crop the part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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