Merging nearby rectangles(edited)
Hello everyone!
I need to merge every nearby digit rectangles so there is should be only 6 rectangles. I've been trying to achieve this for the whole week and I'm soo tired. It would be so nice if anyone could help me. I have a bunch of sorted rectangles from left to right and the task is to split them to separate digits somehow.
I grab contours using findContours cv2.findContours(threshed_img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
My question is really similar to this
Code:
import imutils
import cv2
img = cv2.imread('a.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# threshold image
ret, threshed_img = cv2.threshold(blurred, 200, 255, cv2.THRESH_BINARY_INV)
# find contours and get the external one
#edged = imutils.auto_canny(threshed_img)
ctrz = cv2.findContours(threshed_img, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(ctrz)
(cnts, boundingBoxes) = contours.sort_contours(cnts, method="left-to-right")
boundingBoxes = list(boundingBoxes)
Example of sorted rectangels that I draw(x,y,w,h) [ [126, 815, 42, 57],
[127, 922, 41, 53],
[141, 689, 35, 57],
[143, 577, 40, 51],
[148, 449, 33, 49],
[160, 298, 37, 47],
[169, 199, 39, 53],
[189, 812, 25, 26],
[193, 918, 24, 34],
[198, 576, 31, 54] ]
Drawn rectangles
Desired output
so, that's some object-detection network, not plain clasification ? or is your "merging rectangles" a problem in the preprocessing step ?
please clarify, what you're using, and anyway, show code
also, are you aware of the NMS boxes functions in cv2.dnn ?
I just classify every rectangle but I have nothing to do with them. The task is make up single number from classified rectangles
seriously, please edit your question, and make it explain, what you're doing, else noone can help you
can you maybe start all over and explain, how you obtain those rects ?
I'm really sorry. Added link to code
I need to merge nearby rectangles somehow
Instead of joining char lvl rectangles - detect them together with your object detection algorithm (word level segmentation is the word here)
How to do this? I know its sounds soo stupid but maybe u will give a little piece of code?
Hmm its actually more a conceptual thing. In object detection you have labeled input data. You have to mark the object your want to detect. Instead of marking the characters - you mark the whole word.
And then you train a model. How to do this is a bit out of scope