quangnhat185 / Plate_detect_and_recognize

Detecting and Reading vehicle's license plate from various countries (Germany, Vietnam, Japan, Thailand, Saudi, Russia, Korea, Usa, India, China)
MIT License
169 stars 125 forks source link

How to ignore specific contours in an image? #25

Open ya2431 opened 2 years ago

ya2431 commented 2 years ago

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 sortcontours(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