opencv 车牌切割
生活随笔
收集整理的這篇文章主要介紹了
opencv 车牌切割
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#!/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算子# 梯度方向: xsobel = 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 dilation2def GetRegion(img):regions = []# 查找輪廓contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)for contour in contours:area = cv2.contourArea(contour)if (area < 5000):continueeps = 1e-3 * cv2.arcLength(contour, True)approx = cv2.approxPolyDP(contour, eps, 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):regions.append(box)return regionsdef 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)#保存結果文件名cv2.imwrite('result2.jpg', img)cv2.waitKey(0)cv2.destroyAllWindows()if __name__ == '__main__':#輸入的參數為圖片的路徑img = cv2.imread('test2.png')detect(img)
總結
以上是生活随笔為你收集整理的opencv 车牌切割的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: opencv 标记有数字的区域
- 下一篇: opencv 仪表数字切割