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

How to ignore specific contours in an image? #25

Open
ya2431 opened this issue May 4, 2022 · 0 comments
Open

How to ignore specific contours in an image? #25

ya2431 opened this issue May 4, 2022 · 0 comments

Comments

@ya2431
Copy link

ya2431 commented May 4, 2022

I would like it to ignore the top non-English letters as I don't need them, but I don't know how to ignore them. I thought about removing the index of these areas but unfortunately the index is not the same for all plate numbers so it's not going to work. Does anyone know how I can take the contours of the numbers and the bottom English letters only without the top non-English letters. I would highly appreciate your help.
The image:
c1
The code:
`
import cv2
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import gridspec
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
img = cv2.imread('c1.png')
img1 = img.copy()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(7,7),0)

binary = cv2.threshold(blur, 180, 255,cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

kernel3 = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
thre_mor = cv2.morphologyEx(binary, cv2.MORPH_DILATE, kernel3)
contours, hierarchy = cv2.findContours(thre_mor,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
def sort_contours(cnts,reverse = False):
i = 0
boundingBoxes = [cv2.boundingRect(c) for c in cnts]
(cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),key=lambda b: b[1][i], reverse=reverse))
return cnts
cont, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
crop_characters = []
digit_w, digit_h = 30, 60
for c in sort_contours(cont):
(x, y, w, h) = cv2.boundingRect(c)
ratio = h/w
if 1<=ratio<=10:

    if h/img.shape[0]>=0.20:
        cv2.rectangle(img1, (x, y), (x + w, y + h), (255, 0,0), 2)
    
       
        curr_num = thre_mor[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)
        crop_characters.append(curr_num)

print("Detect {} letters...".format(len(crop_characters)))
fig = plt.figure(figsize=(10,6))
plt.axis(False)
plt.imshow(img1)
plt.show()`

The output:
output

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

1 participant