我一直在尝试使用opencv和python进行自定义车牌检测。我到了某种程度,似乎无法找到从多个轮廓中仅检测车牌轮廓的方法。 问题是,卡塔尔的车牌尺寸和背景颜色不规则。我无法根据颜色和尺寸裁剪板面积。我必须根据文本执行此操作,因为摄像机的图像显示了没有文字的印版和相邻区域。我怎样才能做到这一点。任何帮助,将不胜感激。到目前为止的代码如下,轮廓图如下所示。
import numpy as np
import cv2
import matplotlib.pyplot as plt
# Read the image file
image = cv2.imread(r'C:/Users/Anaconda/Anaconda3/envs/ALPR/OOOL/pictures/Car_1.jpg')
# Crop the ROI
roi = image[414:1000, 374:1500]
# cv2.imshow("ROI Image", roi)
# cv2.waitKey(0)
# RGB to Gray scale conversion
plt.rcParams['figure.figsize'] = 7, 7
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
# cv2.imshow("1 - Grayscale Conversion", gray)
# cv2.waitKey(0)
# blur the image
blur = cv2.GaussianBlur(gray, (7, 7), 0)
cv2.imshow('2 - blur', blur)
cv2.waitKey(0)
# find the sobel gradient. use the kernel size to be 3
sobelx = cv2.Sobel(blur, cv2.CV_8U, 1, 0, ksize=3)
cv2.imshow('3 - sobelx', sobelx)
cv2.waitKey(0)
# Otsu thresholding
_, th2 = cv2.threshold(sobelx, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
cv2.imshow('4 - binary', th2)
cv2.waitKey(0)
# Morphological Closing
se = cv2.getStructuringElement(cv2.MORPH_RECT, (25, 5))
closing = cv2.morphologyEx(th2, cv2.MORPH_CLOSE, se)
cv2.imshow('5 - Morph', closing)
cv2.waitKey(0)
contours, _ = cv2.findContours(closing, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for cnt in contours:
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(roi, [box], 0, (0, 255, 0), 2)
cv2.imshow('6 - contours', roi)
cv2.waitKey(0)
# validate a contour. We validate by estimating a rough area and aspect ratio check.
def validate(cnt):
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
output = False
width = rect[1][0]
height = rect[1][1]
if (width != 0) & (height != 0):
if ((height / width > 1) & (height > width)) | ((width / height > 1) & (width > height)):
if (height * width < 16000) & (height * width > 3000):
output = True
return output
# Lets draw validated contours with red.
for cnt in contours:
if validate(cnt):
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(roi, [box], 0, (0, 0, 255), 2)
cv2.imshow('7 - f_contours', roi)
cv2.waitKey(0)
有了这个我得到以下: License plate contours