国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

OpenCV第七篇:車牌識(shí)別

這篇具有很好參考價(jià)值的文章主要介紹了OpenCV第七篇:車牌識(shí)別。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

目錄

1.調(diào)整圖片大小,并獲取灰度圖

?2.雙邊濾波去除噪音:cv2.bilateralFilter()。

3.邊緣檢測(cè):cv2.Canny(image,threshold1,threshold2)

4.尋找輪廓:車牌(四邊形)

?編輯?5.圖像位運(yùn)算進(jìn)行遮罩

6.圖像剪裁

7.字符識(shí)別:OCR


OpenCV第七篇:車牌識(shí)別

1.調(diào)整圖片大小,并獲取灰度圖

import cv2

if __name__ == '__main__':
    img = cv2.imread('2.jpeg')
    # 調(diào)整圖片大小
    img = cv2.resize(img, (620, 480))
    # 灰度圖
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)




    # 顯示效果
    cv2.imshow('original', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

OpenCV第七篇:車牌識(shí)別

?2.雙邊濾波去除噪音:cv2.bilateralFilter()。

import cv2

if __name__ == '__main__':
    img = cv2.imread('2.jpeg')
    # 調(diào)整圖片大小
    img = cv2.resize(img, (620, 480))
    # 灰度圖
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # 雙邊濾波
    gray1 = cv2.bilateralFilter(gray, 13, 15, 15)

    # 顯示效果
    cv2.imshow('gray', gray)
    cv2.imshow('bilateralFilter', gray1)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

OpenCV第七篇:車牌識(shí)別

3.邊緣檢測(cè):cv2.Canny(image,threshold1,threshold2)

僅顯示強(qiáng)度梯度大于最小閾值threshold1且小于最大閾值threshold2的邊緣。

import cv2

if __name__ == '__main__':
    img = cv2.imread('2.jpeg')
    # 調(diào)整圖片大小
    img = cv2.resize(img, (620, 480))
    # 灰度圖
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # 雙邊濾波
    gray = cv2.bilateralFilter(gray, 13, 15, 15)
    # 邊緣檢測(cè)
    edged = cv2.Canny(gray, 30, 200)




    # 顯示效果
    cv2.imshow('gray', gray)
    cv2.imshow('edged', edged)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

OpenCV第七篇:車牌識(shí)別

4.尋找輪廓:車牌(四邊形)

pip install imutils
import cv2
import imutils

if __name__ == '__main__':
    img = cv2.imread('2.jpeg')
    # 調(diào)整圖片大小
    img = cv2.resize(img, (620, 480))
    # 灰度圖
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # 雙邊濾波
    gray = cv2.bilateralFilter(gray, 13, 15, 15)
    # 邊緣檢測(cè)
    edged = cv2.Canny(gray, 30, 200)


    # 尋找輪廓(圖像矩陣,輸出模式,近似方法)
    contours = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    # 配合上面一句使用:用來兼容cv2和cv3
    contours = imutils.grab_contours(contours)
    # 根據(jù)區(qū)域大小排序取前十個(gè)
    contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10]
    screenCnt = None
    # 遍歷輪廓,找到車牌輪廓
    for c in contours:
        # 計(jì)算輪廓周長(輪廓,是否閉合)
        peri = cv2.arcLength(c, True)
        # 折線化(輪廓,閾值(越小越接近曲線),是否閉合)返回折線頂點(diǎn)坐標(biāo)
        approx = cv2.approxPolyDP(c, 0.018 * peri, True)
        # 獲取四個(gè)頂點(diǎn)(即四邊形)
        if len(approx) == 4:
            screenCnt = approx
            break
    # 如果找到了四邊形
    if screenCnt is not None:
        # 根據(jù)四個(gè)頂點(diǎn)坐標(biāo)對(duì)img畫線(圖像矩陣,輪廓坐標(biāo)集,輪廓索引,顏色,線條粗細(xì))
        cv2.drawContours(img, [screenCnt], -1, (0, 0, 255), 3)



    # 顯示效果
    cv2.imshow('img', img)
    cv2.imshow('gray', gray)
    cv2.imshow('edged', edged)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

?5.圖像位運(yùn)算進(jìn)行遮罩

import cv2
import imutils
import numpy as np

if __name__ == '__main__':
    img = cv2.imread('2.jpeg')
    # 調(diào)整圖片大小
    img = cv2.resize(img, (620, 480))
    # 灰度圖
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # 雙邊濾波
    gray = cv2.bilateralFilter(gray, 13, 15, 15)
    # 邊緣檢測(cè)
    edged = cv2.Canny(gray, 30, 200)

    """尋找輪廓(圖像矩陣,輸出模式,近似方法)"""
    contours = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    # 配合上面一句使用:用來兼容cv2和cv3
    contours = imutils.grab_contours(contours)
    # 根據(jù)區(qū)域大小排序取前十個(gè)
    contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10]
    screenCnt = None
    # 遍歷輪廓,找到車牌輪廓
    for c in contours:
        # 計(jì)算輪廓周長(輪廓,是否閉合)
        peri = cv2.arcLength(c, True)
        # 折線化(輪廓,閾值(越小越接近曲線),是否閉合)返回折線頂點(diǎn)坐標(biāo)
        approx = cv2.approxPolyDP(c, 0.018 * peri, True)
        # 獲取四個(gè)頂點(diǎn)(即四邊形)
        if len(approx) == 4:
            screenCnt = approx
            break
    # 如果找到了四邊形
    if screenCnt is not None:
        # 根據(jù)四個(gè)頂點(diǎn)坐標(biāo)對(duì)img畫線(圖像矩陣,輪廓坐標(biāo)集,輪廓索引,顏色,線條粗細(xì))
        cv2.drawContours(img, [screenCnt], -1, (0, 0, 255), 3)

    """遮罩"""
    # 創(chuàng)建一個(gè)灰度圖一樣大小的圖像矩陣
    mask = np.zeros(gray.shape, np.uint8)
    # 將創(chuàng)建的圖像矩陣的車牌區(qū)域畫成白色
    cv2.drawContours(mask, [screenCnt], 0, 255, -1, )
    # 圖像位運(yùn)算進(jìn)行遮罩
    new_image = cv2.bitwise_and(img, img, mask=mask)


    # 顯示效果
    cv2.imshow('img', img)
    cv2.imshow('gray', gray)
    cv2.imshow('edged', edged)
    cv2.imshow('new_image', new_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

OpenCV第七篇:車牌識(shí)別

6.圖像剪裁

import cv2
import imutils
import numpy as np

if __name__ == '__main__':
    img = cv2.imread('2.jpeg')
    # 調(diào)整圖片大小
    img = cv2.resize(img, (620, 480))
    # 灰度圖
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # 雙邊濾波
    gray = cv2.bilateralFilter(gray, 13, 15, 15)
    # 邊緣檢測(cè)
    edged = cv2.Canny(gray, 30, 200)

    """尋找輪廓(圖像矩陣,輸出模式,近似方法)"""
    contours = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    # 配合上面一句使用:用來兼容cv2和cv3
    contours = imutils.grab_contours(contours)
    # 根據(jù)區(qū)域大小排序取前十個(gè)
    contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10]
    screenCnt = None
    # 遍歷輪廓,找到車牌輪廓
    for c in contours:
        # 計(jì)算輪廓周長(輪廓,是否閉合)
        peri = cv2.arcLength(c, True)
        # 折線化(輪廓,閾值(越小越接近曲線),是否閉合)返回折線頂點(diǎn)坐標(biāo)
        approx = cv2.approxPolyDP(c, 0.018 * peri, True)
        # 獲取四個(gè)頂點(diǎn)(即四邊形)
        if len(approx) == 4:
            screenCnt = approx
            break
    # 如果找到了四邊形
    if screenCnt is not None:
        # 根據(jù)四個(gè)頂點(diǎn)坐標(biāo)對(duì)img畫線(圖像矩陣,輪廓坐標(biāo)集,輪廓索引,顏色,線條粗細(xì))
        cv2.drawContours(img, [screenCnt], -1, (0, 0, 255), 3)

    """遮罩"""
    # 創(chuàng)建一個(gè)灰度圖一樣大小的圖像矩陣
    mask = np.zeros(gray.shape, np.uint8)
    # 將創(chuàng)建的圖像矩陣的車牌區(qū)域畫成白色
    cv2.drawContours(mask, [screenCnt], 0, 255, -1, )
    # 圖像位運(yùn)算進(jìn)行遮罩
    new_image = cv2.bitwise_and(img, img, mask=mask)

    """圖像剪裁"""
    # 獲取車牌區(qū)域的所有坐標(biāo)點(diǎn)
    (x, y) = np.where(mask == 255)
    # 獲取底部頂點(diǎn)坐標(biāo)
    (topx, topy) = (np.min(x), np.min(y))
    # 獲取底部坐標(biāo)
    (bottomx, bottomy,) = (np.max(x), np.max(y))
    # 剪裁
    Cropped = gray[topx:bottomx, topy:bottomy]

    # 顯示效果
    cv2.imshow('img', img)
    cv2.imshow('gray', gray)
    cv2.imshow('edged', edged)
    cv2.imshow('Cropped', Cropped)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

OpenCV第七篇:車牌識(shí)別

7.字符識(shí)別:OCR

import cv2
import imutils
import numpy as np

if __name__ == '__main__':
    img = cv2.imread('2.jpeg')
    # 調(diào)整圖片大小
    img = cv2.resize(img, (620, 480))
    # 灰度圖
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # 雙邊濾波
    gray = cv2.bilateralFilter(gray, 13, 15, 15)
    # 邊緣檢測(cè)
    edged = cv2.Canny(gray, 30, 200)

    """尋找輪廓(圖像矩陣,輸出模式,近似方法)"""
    contours = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    # 配合上面一句使用:用來兼容cv2和cv3
    contours = imutils.grab_contours(contours)
    # 根據(jù)區(qū)域大小排序取前十個(gè)
    contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10]
    screenCnt = None
    # 遍歷輪廓,找到車牌輪廓
    for c in contours:
        # 計(jì)算輪廓周長(輪廓,是否閉合)
        peri = cv2.arcLength(c, True)
        # 折線化(輪廓,閾值(越小越接近曲線),是否閉合)返回折線頂點(diǎn)坐標(biāo)
        approx = cv2.approxPolyDP(c, 0.018 * peri, True)
        # 獲取四個(gè)頂點(diǎn)(即四邊形)
        if len(approx) == 4:
            screenCnt = approx
            break
    # 如果找到了四邊形
    if screenCnt is not None:
        # 根據(jù)四個(gè)頂點(diǎn)坐標(biāo)對(duì)img畫線(圖像矩陣,輪廓坐標(biāo)集,輪廓索引,顏色,線條粗細(xì))
        cv2.drawContours(img, [screenCnt], -1, (0, 0, 255), 3)

    """遮罩"""
    # 創(chuàng)建一個(gè)灰度圖一樣大小的圖像矩陣
    mask = np.zeros(gray.shape, np.uint8)
    # 將創(chuàng)建的圖像矩陣的車牌區(qū)域畫成白色
    cv2.drawContours(mask, [screenCnt], 0, 255, -1, )
    # 圖像位運(yùn)算進(jìn)行遮罩
    new_image = cv2.bitwise_and(img, img, mask=mask)

    """圖像剪裁"""
    # 獲取車牌區(qū)域的所有坐標(biāo)點(diǎn)
    (x, y) = np.where(mask == 255)
    # 獲取底部頂點(diǎn)坐標(biāo)
    (topx, topy) = (np.min(x), np.min(y))
    # 獲取底部坐標(biāo)
    (bottomx, bottomy,) = (np.max(x), np.max(y))
    # 剪裁
    Cropped = gray[topx:bottomx, topy:bottomy]

    """OCR識(shí)別"""
    text = pytesseract.image_to_string(Cropped, config='--psm 11')
    print("車牌結(jié)果:", text)

    # 顯示效果
    cv2.imshow('img', img)
    cv2.imshow('gray', gray)
    cv2.imshow('edged', edged)
    cv2.imshow('new_image', Cropped)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

OpenCV第七篇:車牌識(shí)別??文章來源地址http://www.zghlxwxcb.cn/news/detail-456877.html

到了這里,關(guān)于OpenCV第七篇:車牌識(shí)別的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包