Quite new to OPENCV and Image Processing.
I'm trying to use OPENCV to detect/draw bounding box's around each section.
When thresholding and dilating the images box's get removed and only the text remains causing the findContours to only find the text. I've attempted increasing the MORPH RECT in the kernel to combine the text together but has unfavorable results.
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (7,7), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 10))
dilate = cv2.dilate(thresh, kernel, iterations=1)
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=lambda x: cv2.boundingRect(x)[1])
for c in cnts:
x, y, w, h = cv2.boundingRect(c)
roi = image[y:y+h, x:x+w]
cv2.rectangle(image, (x, y), (x+w, y+h), (36,255,12), 2)
I am not sure how to manipulate the image or the contours to achieve my desired result:
Quite new to OPENCV and Image Processing.
I'm trying to use OPENCV to detect/draw bounding box's around each section.
When thresholding and dilating the images box's get removed and only the text remains causing the findContours to only find the text. I've attempted increasing the MORPH RECT in the kernel to combine the text together but has unfavorable results.
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (7,7), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 10))
dilate = cv2.dilate(thresh, kernel, iterations=1)
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=lambda x: cv2.boundingRect(x)[1])
for c in cnts:
x, y, w, h = cv2.boundingRect(c)
roi = image[y:y+h, x:x+w]
cv2.rectangle(image, (x, y), (x+w, y+h), (36,255,12), 2)
I am not sure how to manipulate the image or the contours to achieve my desired result:
Share Improve this question edited Mar 23 at 2:08 Christoph Rackwitz 15.9k5 gold badges39 silver badges51 bronze badges asked Mar 22 at 17:01 FinatikFinatik 356 bronze badges 4- That seems really overkill to me. Why no try to find the 3 y coordinates of the table rows (find lines made of mostly brown, that follows immediately all-black line), and, in each line, the x coordinate of the field (black pixels surrounding a big all-brown line)? You may answer "but that would be too specific to this example". But then (outside the fact that with only 1 example, we can't know what is the common features we can rely on), I would point out that even your morphology strategy is also specific to the example. You obviously tuned the kernel size from it, for example. – chrslg Commented Mar 22 at 17:13
- that table, before you took a screenshot of it, was HTML, right? – Christoph Rackwitz Commented Mar 23 at 2:09
- It was not, It was a PNG @ChristophRackwitz – Finatik Commented Mar 24 at 3:07
- what was it before it was a PNG? what made the PNG? – Christoph Rackwitz Commented Mar 24 at 10:58
1 Answer
Reset to default 0Read the image and remove the alpha channel. Then invert the image. Then threshold inverted at about 90% of 255 = 230. If needed, erode a small amount to make sure the white rectangles have continuous black borders. Then get external contours. (findContours only works with white regions on black background)
Does this help?
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744307069a4567766.html
评论列表(0条)