Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bounding boxes not found #12

Open
kubasiak opened this issue Aug 12, 2020 · 4 comments
Open

bounding boxes not found #12

kubasiak opened this issue Aug 12, 2020 · 4 comments

Comments

@kubasiak
Copy link

kubasiak commented Aug 12, 2020

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)
@modevera
Copy link

modevera commented Sep 7, 2020

I have the same issue
how didi you define maxh ?

@eaglemas
Copy link

eaglemas commented Dec 26, 2020

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!!!!

@atharva21jadhav
Copy link

@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.

@amandp13
Copy link

@kubasiak Can you please provide me the optimal code for this issue as I am getting confused by your soluton?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants