![](/img/trans.png)
[英]Extract numbers and letters from license plate image with Python OpenCV
[英]How to correctly extract letters after cleaning the background of the image with opencv and python?
我試圖從opencv圖像中分別提取字母,但是在某些情況下遇到困難。 有時,他會撿起相同的字母並在中間分開。 在某些情況下,例如字母“ i”,它無法識別該點並將其視為另一個字符。 在應用腐蝕功能之后,在搜索輪廓以提取字母之后,下面有3個輸入圖像示例。
我的代碼段:
import cv2
import numpy as np
import imutils
img = cv2.imread('captchas/image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU|cv2.THRESH_BINARY_INV)[1]
kernel = np.ones((5,4), np.uint8)
img_erode = cv2.erode(thresh, kernel, iterations = 1)
contours = cv2.findContours(img_erode.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours = contours[0] if imutils.is_cv2() else contours[1]
letter_image_regions = []
output = img_erode.copy()
for contour in contours:
(x, y, w, h) = cv2.boundingRect(contour)
if cv2.contourArea(contour) > 200:
if w / h > 0.75:
half_width = int(w / 2)
cv2.rectangle(output, (x, y), (x + half_width, y + h), (70,0,70), 3)
cv2.rectangle(output, (x, y), (x + w, y + h), (70,0,70), 3)
else:
cv2.rectangle(output, (x, y), (x + w, y + h), (70,0,70), 3)
cv2.imshow("Input", img)
cv2.imshow("Erode", img_erode)
cv2.imshow("Output", image)
cv2.waitKey(0)
if w / h > 0.75:
字母Z
被一半cv2.rectangle(output, (x, y), (x + half_width, y + h), (70,0,70), 3)
的原因是這種情況if w / h > 0.75:
並繪制cv2.rectangle(output, (x, y), (x + half_width, y + h), (70,0,70), 3)
。 因此,嘗試找到更好的條件。
要獲取整個字母i
,請在erode()
和findContour()
之前執行morphologyEx()
findContour()
。
kernel2 = np.ones((22,7), np.uint8)
morph_img = img_erode.copy()
cv2.morphologyEx(src=img_erode, op=cv2.MORPH_CLOSE, kernel=kernel2, dst=morph_img)
結果我得到了。 您可以調整kernel2
的大小以獲得更好的結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.