python - 从嘈杂的车牌中分割每个字符
问题描述
我正在做一个尼泊尔车牌检测项目,我从车辆中检测到我的车牌,但车牌歪斜,但结果是车牌的嘈杂图像。
我想知道如何从中分割出每个字符,以便将其发送到检测部分。我试过这样做,但它只是从第二行分割字符。
def segment(image):
H = 100.
height, width, depth = image.shape
imgScale = H/height
newX,newY = image.shape[1]*imgScale, image.shape[0]*imgScale
image = cv2.resize(image,(int(newX),int(newY)))
cv2.imshow("Show by CV2",image)
cv2.waitKey(0)
cv2.imwrite("resizeimg.jpg",image)
idx =0
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_,thresh = cv2.threshold(gray,127,255,cv2.THRESH_TOZERO)
cv2.imshow("thresh",thresh)
cv2.waitKey(0)
# gray=cv2.cvtColor(plate,cv2.COLOR_BW2GRAY)
_,contours,_ = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
idx += 1
x,y,w,h = cv2.boundingRect(cnt)
roi = image[y:y+h,x:x+w]
if(w > 10 and h > 10):
cv2.imwrite(str(idx) + '.jpg', roi)
解决方案
假设你有盘子的比例,你可以在 y 轴上把盘子切成两半。从左到右,thresh 图像,morphologyEx 图像,轮廓。用同样的方法涂抹另一半。
image = cv2.imread("1.PNG")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_,thresh = cv2.threshold(gray,127,255,cv2.THRESH_TOZERO)
cv2.imshow("thresh",thresh)
element = cv2.getStructuringElement(shape=cv2.MORPH_RECT, ksize=(5, 11))
morph_img = thresh.copy()
cv2.morphologyEx(src=thresh, op=cv2.MORPH_CLOSE, kernel=element, dst=morph_img)
cv2.imshow("morph_img",morph_img)
_,contours,_ = cv2.findContours(morph_img,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
r = cv2.boundingRect(c)
cv2.rectangle(image,(r[0],r[1]),(r[0]+r[2],r[1]+r[3]),(0,0,255),2)
cv2.imshow("img",image)
cv2.waitKey(0)
cv2.destroyAllWindows()
分割字符的另一种方法是沿 x 轴和 y 轴查找灰度值之和。您可以很容易地看到 x 轴上有 3 个峰值,即 3 个字符,而 y 轴上有 1 个峰值,即您的字符所在的位置。
推荐阅读
- python - 使用带有 FacetGrid 的 seaborn 进行绘图,其中值是数据框中的 ndarray
- blazor - Razor 组件如何与服务器应用程序通信
- postgresql - 如何处理热备副本和服务器端游标问题
- angular - ElementRef 和@ViewChild,不推荐?
- unix - 在远程机器上执行代码的最佳方式
- ejabberd - 在 muc_filter_message 挂钩中检索时间戳值
- iis - 在 9LTS 中重写说话 URL 是否应该在 IIS 上开箱即用?
- java - 遍历 Map 的 ArrayList 的 ArrayList
- javascript - ExtJS - 将 Ajax 响应变量存储为全局
- python - 如何生成绘图,暂停程序直到图形关闭,然后返回主函数