I am trying to extract columns of a table in an image. I have managed to successfully identify the vertical regions of interest as shown in this:

My problem is when I am trying to extract and save those regions of interest I am getting a 6 vertical lines of the border of the bounding rectangle as opposed to the region in between them.
This is the code I am using to achieve this:
import cv2
import numpy as np
image = cv2.imread('x.png')
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]
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,50))
vertical_mask = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=1)
cnts = cv2.findContours(vertical_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
for c in cnts:
cv2.drawContours(image, [c], -1, (36,255,12), -1)
idx = 0
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
idx+=1
new_img=image[y:y+h,x:x+w]
cv2.imwrite(str(idx) + '.png', new_img)
cv2.imshow("im.png",image)
cv2.waitKey(0)
cv2.destroyAllWindows()
This is is the image of the right most border, as you can see there is some text:

Any ideas as to what might be going on?
↧