bounding boxes not found
kubasiak opened this issue · 4 comments
Hi,
First of all thank you for this repo. I enjoyed it a lot.
I see that there is an issue with the boxes around characters.
When using
cv2.findContours
sometimes the contours are not found. I find that it is in large extent because of the frames that are around the plates.
I have solved it with removing a margin of 5% from the plate picture.
(
Later of course the coordinates of the frames must be adjusted for this margin in respect to the original picture.)
It is not the most elegant solution but it helped:
cont, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
marginh=0
marginw=0
if(len(cont)<=1):
print('havent found any characters - cutting margings')
m=0.05
marginh=round(m*maxh)
marginw=round(m*maxw)
cut_plate_image=plate_image[marginh:maxh-marginh,marginw:maxw-marginw]
cut_binary=binary[marginh:maxh-marginh,marginw:maxw-marginw]
cont, _ = cv2.findContours(cut_binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
test_roi = cut_plate_image.copy()
cut_plate_image=plate_image[marginh:maxh-marginh,marginw:maxw-marginw]
cut_binary=binary[marginh:maxh-marginh,marginw:maxw-marginw]
And then of course adjust when cropping images:
# extract the img
curr_num = cut_binary[y:y+h,x:x+w]
curr_num = cv2.resize(curr_num, dsize=(digit_w, digit_h))
_, curr_num = cv2.threshold(curr_num, 220, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
curr_num_orig = cut_plate_image[y:y+h,x:x+w]
crop_characters.append(curr_num)
crop_characters_orig.append(curr_num_orig)
I have the same issue
how didi you define maxh ?
Instead of :
_,cont, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
write :
_,cont, _ = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
and everything is fixed!!!!
@eaglemas I'm working with RETR_TREE too currently. Although it can work okay at such scenarios, RETR_TREE might also create extra contours. For example, for digits like 0 since all the contours hierarchy is calculated we might get 2 contours one inside and the other on the external.
It would be better if we go with RETR_EXTERNAL and crop the portions of image.