Detect the white background license plate region using opencv(Edge Detection, Contour Detection)

95 Views Asked by At

I want to detect the white number plate region using contour detection and edge detection using opencventer image description here

I have tried a variety of solutions. These work for sharp images but don't work for low res images and images that are not sharp enough.

import cv2 as cv2
import numpy as np

# ------------------------Preprocessing Start----------------------------------------
# Read the image file
image = cv2.imread(
    'nepali-dataset/IMG_0124.jpg')


# Convert to Grayscale Image
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Before denoising", gray_image)
cv2.waitKey(0)

gray_image = cv2.fastNlMeansDenoising(gray_image, None, 20, 7, 21)
cv2.imshow("After denoising", gray_image)
cv2.waitKey(0)


# # # Canny Edge Detection
canny_edge = cv2.Canny(gray_image, 170, 200)
cv2.imshow("Traditional canny_edge detect", canny_edge)
cv2.waitKey(0)

# morphological operations

kernel = np.ones((5, 15), np.uint8)

canny_edge = cv2.dilate(canny_edge, None)
cv2.imshow("dilate", canny_edge)
cv2.waitKey(0)

canny_edge = cv2.erode(canny_edge, None)
cv2.imshow("Erode", canny_edge)
cv2.waitKey(0)

# -------------------------Preprocessing End---------------------------------------

# -------------------------Candidate area detection Start---------------------------------------
# Find contours based on Edges
contours, new = cv2.findContours(
    canny_edge.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:30]

# Initialize license Plate contour and x,y coordinates
contour_with_license_plate = None
license_plate = None
x = None
y = None
w = None
h = None

# Find the contour with 4 potential corners and creat ROI around it
for contour in contours:
    # Find Perimeter of contour and it should be a closed contour
    perimeter = cv2.arcLength(contour, True)
    approx = cv2.approxPolyDP(contour, 0.01 * perimeter, True)
    if len(approx) == 4:  # see whether it is a Rect
        print("Got ctr")
        contour_with_license_plate = approx
        x, y, w, h = cv2.boundingRect(contour)
        license_plate = gray_image[y:y + h, x:x + w]
        image = cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 10)

# # Removing Noise from the detected image, before sending to Tesseract
# license_plate = cv2.bilateralFilter(license_plate, 11, 17, 17)
# (thresh, license_plate) = cv2.threshold(license_plate, 150, 180, cv2.THRESH_BINARY)


# image = cv2.rectangle(image, (x,y), (x+w,y+h), (0,255,0), 10)
image = cv2.resize(image, (600, 600))
cv2.imshow("License Plate Detection", image)
cv2.waitKey(0)
# ---------------------Candidate area detection End-------------------------------------------

Is there any way to detect white background rectangles or someway else to detect it?

0

There are 0 best solutions below