目錄
1.調(diào)整圖片大小,并獲取灰度圖
?2.雙邊濾波去除噪音:cv2.bilateralFilter()。
3.邊緣檢測(cè):cv2.Canny(image,threshold1,threshold2)
4.尋找輪廓:車牌(四邊形)
?編輯?5.圖像位運(yùn)算進(jìn)行遮罩
6.圖像剪裁
7.字符識(shí)別:OCR
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()
?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()
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()
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()
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()
文章來源:http://www.zghlxwxcb.cn/news/detail-456877.html
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()
??文章來源地址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)!