#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""created on thu mar 21 12:42:15 2019
@author: lg
"""import cv2
import numpy as np
# 形態學處理
def process(img):
# 高斯平滑
gaussian = cv2.gaussianblur(img, (3, 3), 0, 0, cv2.border_default)
# 中值濾波
median = cv2.medianblur(gaussian, 5)
# sobel運算元
# 梯度方向: x
sobel = cv2.sobel(median, cv2.cv_8u, 1, 0, ksize=3)
# 二值化
ret, binary = cv2.threshold(sobel, 170, 255, cv2.thresh_binary)
# 核函式
element1 = cv2.getstructuringelement(cv2.morph_rect, (9, 1))
element2 = cv2.getstructuringelement(cv2.morph_rect, (9, 7))
# 膨脹
dilation = cv2.dilate(binary, element2, iterations=1)
# 腐蝕
erosion = cv2.erode(dilation, element1, iterations=1)
# 膨脹
dilation2 = cv2.dilate(erosion, element2, iterations=3)
return dilation2
def getregion(img):
regions =
# 查詢輪廓
for contour in contours:
area = cv2.contourarea(contour)
if (area < 5000):
continue
eps = 1e-3 * cv2.arclength(contour, true)
rect = cv2.minarearect(contour)
box = cv2.boxpoints(rect)
box = np.int0(box)
height = abs(box[0][1] - box[2][1])
width = abs(box[0][0] - box[2][0])
ratio =float(width) / float(height)
if (ratio < 5 and ratio > 1.8):
return regions
def detect(img):
# 灰度化
gray = cv2.cvtcolor(img, cv2.color_bgr2gray)
prc = process(gray)
regions = getregion(prc)
print('[info]:detect %d license plates' % len(regions))
for box in regions:
cv2.drawcontours(img, [box], 0, (0, 255, 0), 2)
cv2.imshow('result', img)
#儲存結果檔名
#輸入的引數為的路徑
車牌區域定位 opencv
include include include include using namespace std using namespace cv void getbluemask iplimage src,iplimage dst int main 若每一行的畫素累加和除以上一行的畫素累加和小於乙個閾值...
車牌識別之字元切割2
首先我選擇車牌號作為投影的,可以作為車牌切割的技術。原理 車牌處理好後,用陣列儲存所有畫素值,然後在x軸上遍歷每一列的畫素值並累加,最總所投影的效果將以投影圖的形式呈現,投影圖的波峰波谷的差距就是每列累加的畫素值的差距。如果把這項技術作為車牌字元切割的技術,那麼每列畫素的累加值就是用來判斷字元區與空...
車牌識別之字元切割3
原理 垂直投影。限制切割出來的字元寬度要大於車牌總寬度的七分之一。該篇部落格無法對漢字進行完整切割,本次進行改善,可把車牌進行完整的字元切割。include opencv2 imgproc imgproc.hpp include opencv2 highgui highgui.hpp include...