首页 > 解决方案 > 使用 OpenCV 从车牌中分割字符

问题描述

我清理了一块专利板来读取字符。现在,我陷入了必须分割字符的部分。

对于清洁盘子的阶段,我这样做:

在此处输入图像描述

对此:

在此处输入图像描述

现在的想法是能够分割字符,然后能够使用我开发的神经网络读取它,对于分割我携带这个但仍然不明白为什么它不起作用:

# Create sort_contours() function to grab the contour of each digit from left to right
def sort_contours(cnts,reverse = False):
    i = 0
    boundingBoxes = [cv2.boundingRect(c) for c in cnts]
    (cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),
                                        key=lambda b: b[1][i], reverse=reverse))
    return cnts

cont, _  = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# creat a copy version "test_roi" of plat_image to draw bounding box
test_roi = plate_image.copy()

# Initialize a list which will be used to append charater image
crop_characters = []

# define standard width and height of character
digit_w, digit_h = 40, 80

for c in sort_contours(cont):
    (x, y, w, h) = cv2.boundingRect(c)
    ratio = h/w
    if 1<=ratio<=3.5: # Only select contour with defined ratio
        if h/plate_image.shape[0]>=0.1: # Select contour which has the height larger than 50% of the plate
            # Draw bounding box arroung digit number
            cv2.rectangle(test_roi, (x, y), (x + w, y + h), (0, 255,0), 2)

            # Sperate number and gibe prediction
            curr_num = thre_mor[y:y+h,x:x+w]
            curr_num = cv2.resize(curr_num, dsize=(digit_w, digit_h))
            _, curr_num = cv2.threshold(curr_num, 220, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
            crop_characters.append(curr_num)

print("Detect {} letters...".format(len(crop_characters)))
fig = plt.figure(figsize=(10,6))
plt.axis(False)
plt.imshow(test_roi)

我怎样才能使实施正确?欢迎任何帮助!

标签: pythonpython-3.ximageopencvimage-processing

解决方案


如果您想通过神经网络标记它们,则不需要清理的第一步。如果您查看区域建议神经网络(rnn、fast-rnn、faster-rnn、yolo、mask-rnn 等),它们将一次性完成分割和分类。

如果您确实想进行细分,请先查看连接的组件。它将选择所有连接在一起的正像素。


推荐阅读