![](/img/trans.png)
[英]Reading license plate from image using OpenCV Python and Tesseract
[英]Extract numbers and letters from license plate image with Python OpenCV
我想检测然后从这张图片中提取字母和数字。 我刚刚开始学习 OpenCV,我认为这可以用那个库来完成。 您有我使用的图像,并且在下面需要 output。 这是我拥有的代码:
import cv2
# read original image
img = cv2.imread('image.jpg')
cv2.imshow('original', img)
cv2.waitKey(0)
# convert it to gray and apply filter
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #convert to grey scale
gray = cv2.bilateralFilter(gray, 11, 17, 17)
cv2.imshow('gray', gray)
cv2.waitKey(0)
#apply treshold
thresh = cv2.threshold(gray, 10, 255, cv2.THRESH_OTSU)[1]
cv2.imshow('thresh', thresh)
cv2.waitKey(0)
这是图像:
我的目标是获取每个字母和数字的分离图像(我在绘画中这样做):
那么,我该怎么做才能得到这个? 保持相同的字母和数字顺序是完美的,例如:
MXF51051
这是一种使用简单阈值 + 轮廓过滤的方法
我们首先转换为灰度,然后转换为 Otsu 阈值以获得二值图像
接下来我们使用cv2.findContours()
找到轮廓。 为了保持字母/数字的相同顺序,我们使用imutils.contours.sort_contours()
和left-to-right
参数来确保当我们遍历轮廓时,每个轮廓都以正确的顺序排列。 对于每个轮廓,我们使用最小和最大区域阈值进行过滤,以确保我们只保留带有所需文本的轮廓。 获得过滤后的 ROI 后,我们使用 Numpy 切片提取/保存 ROI。 这是过滤后的蒙版,仅包含所需的文本
检测到的数字和字母
以正确顺序提取的 ROI
import cv2
import numpy as np
from imutils import contours
image = cv2.imread('1.jpg')
mask = np.zeros(image.shape, dtype=np.uint8)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
(cnts, _) = contours.sort_contours(cnts, method="left-to-right")
ROI_number = 0
for c in cnts:
area = cv2.contourArea(c)
if area < 800 and area > 200:
x,y,w,h = cv2.boundingRect(c)
ROI = 255 - thresh[y:y+h, x:x+w]
cv2.drawContours(mask, [c], -1, (255,255,255), -1)
cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)
ROI_number += 1
cv2.imshow('mask', mask)
cv2.imshow('thresh', thresh)
cv2.waitKey()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.