I am currently working on a university project for which I want to explore different computer vision techniques. My goal is to do automatic number plate recognition (ALPR) on chinese license plates. So far, I have trained a detection model (YOLOv5) to locate the license plates. The bounding boxes of the model don't account for any skew in the images, so I chose to just crop them for now. That's what the images look like right now: Example license plate crop
I want to use OpenCV to do edge detection on the license plates and do further cropping and perspective transformations with that. I want the images to contain only the license plate so I can later do character segmentation with the best input images possible.
Unfortunately, I am very new to computer vision and can't find a preprocessing that works for most of the images and enables OpenCV to reliably detect the rectangular license plates. It's really hard for me to do preprocessing that works for different lighting conditions and colors of the vehicles. I would apprechiate some advice on how I could go about this (or alternatively, whether I should go for a different approach)
This is what I did so far with OpenCV to achieve edge detection:
- These are my preprocessing steps:
- Conversion to grayscale
- Gaussian blur to remove some noise (how should I choose the kernel size?)
- Histogram equalization to increase contrast
- Canny edge detection
- Further preprocessing:
- Dilation to close gaps (how should I choose the kernel size?)
- Erosion to shrink the edges again
- Finding contours
- Looking for the contour which best approximates a rectangle and has the largest area
Without tweaking the thresholds for canny edge detection every time, this approach doesn't even find a rectangular contour for the license plates. This is what a result looks like after tweaking: Example result Description from top to bottom:
- Original image
- after preprocessing
- after canny edge detection
- after further preprocessing
- With contours
- With the best contour
- The contour stretched to fill the image
I didn't just dump my code in here as I assume the biggest problem to be the preprocessing of the images.
Faster and easier to crop image by using
mouseevent
. It is straight forward. It will save iterator files. And the rest you can do Canny, contours, etc.Snippet:
Screenshot before and after: