【问题标题】:License plate character segmentation python opencv车牌字符分割 python opencv
【发布时间】:2018-06-06 05:49:48
【问题描述】:

我想隔离下图中的每个字符:

它应该在每个字符周围创建一个矩形边界框。我的代码正在创建一个圆形边界框。我需要将这些孤立的字符图像提供给我训练有素的模型来预测字符。我之前没有做过图像处理,这导致我提出这样的问题。

这是我正在使用的代码:

# 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中是否有任何等效的库?

这是分割后每个字符的最终输出必须是这样的:

【问题讨论】:

    标签: python opencv image-processing ocr image-segmentation


    【解决方案1】:

    去除背景噪音后,您可以像这样输入图像:

    然后你可以使用下面的代码得到你想要的:

    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
    

    【讨论】:

    • 您好,您能否也添加您为消除背景噪音而编写的代码?分割代码完美运行。
    • 因为这个问题是关于分段的,所以我确实手动去除了噪音。
    猜你喜欢
    • 2020-09-16
    • 1970-01-01
    • 2021-05-25
    • 2017-09-30
    • 1970-01-01
    • 2021-03-27
    • 2020-03-28
    • 2019-04-18
    相关资源
    最近更新 更多