簡體   English   中英

車牌字符分割python opencv

[英]License plate character segmentation python opencv

我想隔離下圖中的每個字符: 在此處輸入圖片說明

並且應該在每個字符周圍創建一個矩形邊框。 我的代碼正在創建一個圓形邊界框。 我需要將這些孤立的角色圖像提供給我訓練有素的模型以預測角色。 我沒有做過圖像處理,這導致我提出這樣的問題。

這是我正在使用的代碼:

# Standard imports
import cv2
import numpy as np;

from PIL import Image
params = cv2.SimpleBlobDetector_Params()

# Change thresholds
params.minThreshold = 10;
params.maxThreshold = 200;

#Filter by Color

params.filterByColor=False
params.blobColor=255

# Filter by Area.
params.filterByArea = False
params.minArea = 50

# Filter by Circularity
params.filterByCircularity = False
params.minCircularity = 0.0785
#
# # Filter by Convexity
params.filterByConvexity = False
params.minConvexity = 0.87
#
# # Filter by Inertia
params.filterByInertia = False
params.minInertiaRatio = 0.01
# Read image
    im = cv2.imread("C:\\xx\\testimages\\bw_plate.jpg", cv2.IMREAD_GRAYSCALE)

cv2.threshold(im,200,255,cv2.THRESH_BINARY_INV,im)


# Set up the detector with default parameters.
detector = cv2.SimpleBlobDetector_create(params)

# Detect blobs.
keypoints = detector.detect(im)


# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob
im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0, 0, 255),
                                      cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Show keypoints
cv2.imshow("Keypoints", im_with_keypoints)
cv2.waitKey(0)

我的輸出與以下代碼是: 在此處輸入圖片說明

為什么不能正確檢測0和2? 另外,如何為每個孤立的字符創建單獨的jpeg文件?

我的項目的C ++實現使用CblobResult類進行細分。 python中有任何等效的庫嗎?

這是分段后每個字符的最終輸出結果: 在此處輸入圖片說明

消除背景噪音后,您可以像這樣輸入圖像:

然后,您可以使用以下代碼獲得所需的內容:

import cv2
img = cv2.imread('test4.jpg', 0)
cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU,img)
image, contours, hier = cv2.findContours(img, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
contours = sorted(contours, key=lambda ctr: cv2.boundingRect(ctr)[0])
cv2.imshow("contours", img)
cv2.waitKey(0)
d=0
for ctr in contours:
    # Get bounding box
    x, y, w, h = cv2.boundingRect(ctr)
    # Getting ROI
    roi = image[y:y+h, x:x+w]

    cv2.imshow('character: %d'%d,roi)
    cv2.imwrite('character_%d.png'%d, roi)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    d+=1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM