车牌字符分割python opencv [英] License plate character segmentation python opencv

查看:1122
本文介绍了车牌字符分割python opencv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



这是我正在使用的代码:

 #标准进口
导入cv2
导入numpy为np;来自PIL导入的

图片
params = cv2.SimpleBlobDetector_Params()

#更改阈值
params.minThreshold = 10;
params.maxThreshold = 200;

#Filter by Color

params.filterByColor = False
params.blobColor = 255

#按区域过滤。
params.filterByArea = False
params.minArea = 50

#按圆度过滤
params.filterByCircularity = False
params.minCircularity = 0.0785

##按凸度过滤
params.filterByConvexity = False
params.minConvexity = 0.87

##由Inertia过滤
params。 filterByInertia = False
params.minInertiaRatio = 0.01
#阅读图片
im = cv2.imread(C:\\xx\\testimages\\bw_plate.jpg ,cv2.IMREAD_GRAYSCALE)

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


#使用默认参数设置检测器。
detector = cv2.SimpleBlobDetector_create(params)

#检测blob。
keypoints = detector.detect(im)


#将检测到的blob绘制为红色圆圈。
#cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS确保圆的大小对应于blob的大小
im_with_keypoints = cv2.drawKeypoints(im,keypoints,np.array([]),(0,0,255),
cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

#显示关键点
cv2.imshow(Keypoints,im_with_keypoints)
cv2.waitKey(0)

我输出的代码如下:



为什么没有正确检测到0和2?另外,我如何为每个孤立的角色创建单独的jpeg文件?



我的项目的C ++实现使用

解决方案

删除背景噪音后,您可以输入如下图像:





然后您可以使用以下代码获得所需内容:

  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 cours in contours:
#获取边界框
x,y,w,h = cv2.boundingRect(ctr)
#获取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


I want to isolate every character in the following image:

and it should create a rectangular bounding box around each character. My code is creating a circular bounding box. I need to supply these isolated character images to my trained model to predict the character. I haven't done image processing before which leads me to asking such a question.

This is the code I'm using:

# 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)

My output with the following code is:

Why is it not detecting 0 and 2 properly? Also how can I create separate jpeg files for every isolated character?

The C++ implementation of my project uses CblobResult class which did the segmentation. Is there any equivalent library in python?

This is what the final output must look like for every character after segmentation:

解决方案

After removing background noises you can input image like this:

Then you can get what you want using following code:

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

这篇关于车牌字符分割python opencv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆