python?Opencv實(shí)現(xiàn)停車位識(shí)別思路詳解
1.實(shí)現(xiàn)的思路
(1)首先使用一個(gè)處理畫(huà)框的程序,將圖片中的有車和無(wú)車的停車位給畫(huà)出來(lái),并且保存坐標(biāo)(如果畫(huà)錯(cuò)了,將鼠標(biāo)移至要?jiǎng)h除的框中,右擊鼠標(biāo),即可刪除);
#定義回調(diào)函數(shù)
def mouseClick(events,x,y,flags,params):
#按下鼠標(biāo)左鍵,將點(diǎn)擊的坐標(biāo)(x,y)保存到position列表中
if (events&cv2.EVENT_LBUTTONDOWN==cv2.EVENT_LBUTTONDOWN):
position.append((x,y))
#按下鼠標(biāo)右鍵時(shí),移除選中的框
if (events&cv2.EVENT_RBUTTONDOWN==cv2.EVENT_RBUTTONDOWN):
for i,pos in enumerate(position):
(x1,y1)=pos
if (x1<x<x1+img_width and y1<y<y1+img_height):
position.pop(i)

(2)畫(huà)好之后,關(guān)閉窗口,即可看到已經(jīng)保存好坐標(biāo)的文件,下次再運(yùn)行程序時(shí),不用再畫(huà)框;程序會(huì)讀出當(dāng)前文件,將之前保存好的坐標(biāo)加載出畫(huà)出框。
#首先查看文件是否已經(jīng)包含了CarParkPos文件
try:
with open('CarParkPos','rb') as fp:
position=pickle.load(fp)
except:
# 存儲(chǔ)所有停車位的坐標(biāo)列表
position=[]
(3)主程序的思路
將攝像頭讀取的圖片進(jìn)行處理
Opencv基礎(chǔ)知識(shí)點(diǎn):
http://www.dbjr.com.cn/article/254006.htm
高斯去噪:
http://www.dbjr.com.cn/article/198212.htm
局部二值化:
http://www.dbjr.com.cn/article/248000.htm
中值濾波:
http://www.dbjr.com.cn/article/198212.htm
Opencv中獲取卷積核:
http://www.dbjr.com.cn/article/254013.htm
腐蝕操作:
http://www.dbjr.com.cn/article/214725.htm
#轉(zhuǎn)換為灰度圖
gray=cv2.cvtColor(src=frame,code=cv2.COLOR_BGR2GRAY)
#高斯去噪
gauss=cv2.GaussianBlur(src=gray,ksize=(3,3),sigmaX=0)
#圖像二值化處理
thresh=cv2.adaptiveThreshold(src=gauss,maxValue=255,adaptiveMethod=cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
thresholdType=cv2.THRESH_BINARY_INV,blockSize=21,C=16)
# 中值濾波操作
median=cv2.medianBlur(src=thresh,ksize=3)
#腐蝕操作
dilate=cv2.dilate(src=median,kernel=kernel,iterations=1) for pos in position:
(x,y)=pos
mask=dilate[y:y+img_height,x:x+img_width]
# cv2.imshow(str(x*y),mask)
#返回灰度值不為0的像素?cái)?shù),可用來(lái)判斷圖像是否全黑。
count=cv2.countNonZero(mask)
#當(dāng)計(jì)算的count低于800,表示是一個(gè)空位
if count<800:
countBlackCar+=1
color=(0,255,0)
thickness=3
else:
color=(0,0,255)
thickness=2
cv2.rectangle(img=frame, pt1=(pos[0], pos[1]),
pt2=(pos[0] + img_width, pos[1] + img_height),
color=color, thickness=thickness)
cvzone.putTextRect(img=frame, text=str(count), pos=(x + 3, y + img_height - 5),
scale=0.8, thickness=1, offset=0,colorR=color)參考視頻教程:https://www.bilibili.com/video/BV14Z4y1Q7au?t=3992.0(建議看懂視頻中的思路)
注:代碼不重要,主要是學(xué)會(huì)給出的鏈接中這位博主的思路。使用更加簡(jiǎn)單的方法解決問(wèn)題,但是呢?這種方法我認(rèn)為主要是為解決那種固定攝像頭拍攝的停車位,因?yàn)槲覀儤?biāo)注的坐標(biāo)是固定的(但是可以利用深度學(xué)習(xí)提取有車和無(wú)車的特征進(jìn)行識(shí)別,定位的可以使用Opencv來(lái)解決)。
2.整體代碼實(shí)戰(zhàn)

(1)ParkingSpacePicker.py
import os
import cv2
import pickle
#首先查看文件是否已經(jīng)包含了CarParkPos文件
try:
with open('CarParkPos','rb') as fp:
position=pickle.load(fp)
except:
# 存儲(chǔ)所有停車位的坐標(biāo)列表
position=[]
#停車位的高寬
img_width,img_height=47,88
#定義回調(diào)函數(shù)
def mouseClick(events,x,y,flags,params):
#按下鼠標(biāo)左鍵,將點(diǎn)擊的坐標(biāo)(x,y)保存到position列表中
if (events&cv2.EVENT_LBUTTONDOWN==cv2.EVENT_LBUTTONDOWN):
position.append((x,y))
#按下鼠標(biāo)右鍵時(shí),移除選中的框
if (events&cv2.EVENT_RBUTTONDOWN==cv2.EVENT_RBUTTONDOWN):
for i,pos in enumerate(position):
(x1,y1)=pos
if (x1<x<x1+img_width and y1<y<y1+img_height):
position.pop(i)
with open('CarParkPos','wb') as fp:
pickle.dump(position,fp)
while True:
img=cv2.imread('images/packing.png')
for pos in position:
cv2.rectangle(img=img,pt1=(pos[0],pos[1]),
pt2=(pos[0]+img_width,pos[1]+img_height),
color=(0,255,0),thickness=2)
cv2.imshow('Packing',img)
#設(shè)置鼠標(biāo)事件
cv2.setMouseCallback('Packing',mouseClick)
key=cv2.waitKey(1)
if key==27:
break
cv2.destroyAllWindows()
if __name__ == '__main__':
print('Pycharm')
(2)main.py
import os
import cv2
import pickle
import cvzone
with open('CarParkPos', 'rb') as fp:
position = pickle.load(fp)
#停車位的高寬
img_width,img_height=47,88
cap=cv2.VideoCapture('video/packing-3.mp4')
def checkParkingSpace(dilate):
countBlackCar=0
for pos in position:
(x,y)=pos
mask=dilate[y:y+img_height,x:x+img_width]
# cv2.imshow(str(x*y),mask)
#返回灰度值不為0的像素?cái)?shù),可用來(lái)判斷圖像是否全黑。
count=cv2.countNonZero(mask)
#當(dāng)計(jì)算的count低于800,表示是一個(gè)空位
if count<800:
countBlackCar+=1
color=(0,255,0)
thickness=3
else:
color=(0,0,255)
thickness=2
cv2.rectangle(img=frame, pt1=(pos[0], pos[1]),
pt2=(pos[0] + img_width, pos[1] + img_height),
color=color, thickness=thickness)
cvzone.putTextRect(img=frame, text=str(count), pos=(x + 3, y + img_height - 5),
scale=0.8, thickness=1, offset=0,colorR=color)
return countBlackCar
#獲取卷積核
kernel=cv2.getStructuringElement(shape=cv2.MORPH_RECT,ksize=(3,3))
while cap.isOpened():
#循環(huán)播放視頻文件
if cap.get(cv2.CAP_PROP_POS_FRAMES)==cap.get(cv2.CAP_PROP_FRAME_COUNT):
cap.set(cv2.CAP_PROP_POS_FRAMES,0)
ret,frame=cap.read()
# frame=cv2.resize(src=frame,dsize=(750,600))
height,width,channel=frame.shape
if not ret:
break
#轉(zhuǎn)換為灰度圖
gray=cv2.cvtColor(src=frame,code=cv2.COLOR_BGR2GRAY)
#高斯去噪
gauss=cv2.GaussianBlur(src=gray,ksize=(3,3),sigmaX=0)
#圖像二值化處理
thresh=cv2.adaptiveThreshold(src=gauss,maxValue=255,adaptiveMethod=cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
thresholdType=cv2.THRESH_BINARY_INV,blockSize=21,C=16)
# 中值濾波操作
median=cv2.medianBlur(src=thresh,ksize=3)
#腐蝕操作
dilate=cv2.dilate(src=median,kernel=kernel,iterations=1)
cntCar=checkParkingSpace(dilate)
cvzone.putTextRect(img=frame,text="BlackPosition: "+str(cntCar),
pos=(20,height-80),scale=1.0,thickness=2)
cv2.imshow('img',frame)
# cv2.imshow('thresh',thresh)
# cv2.imshow('median',median)
# cv2.imshow('dilate',dilate)
key=cv2.waitKey(30)
if key==27:
break
cv2.destroyAllWindows()
if __name__ == '__main__':
print('Pycharm')
(3)視頻效果
停車位識(shí)別演示

注:視頻自己做的比較差,建議讀者最好自己嘗試實(shí)現(xiàn)這個(gè)思路。
到此這篇關(guān)于Opencv實(shí)現(xiàn)停車位識(shí)別的文章就介紹到這了,更多相關(guān)Opencv實(shí)現(xiàn)停車位識(shí)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python的Django框架中的數(shù)據(jù)庫(kù)配置指南
這篇文章主要介紹了Python的Django框架中的數(shù)據(jù)庫(kù)配置指南,文中舉了Python內(nèi)置的SQLite的示例,需要的朋友可以參考下2015-07-07
python實(shí)現(xiàn)簡(jiǎn)單倒計(jì)時(shí)功能
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)單倒計(jì)時(shí)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
pytest生成Allure報(bào)告以及查看報(bào)告的實(shí)現(xiàn)
本文主要介紹了pytest生成Allure報(bào)告以及查看報(bào)告的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
Python爬蟲(chóng)學(xué)習(xí)之獲取指定網(wǎng)頁(yè)源碼
這篇文章主要為大家詳細(xì)介紹了Python爬蟲(chóng)學(xué)習(xí)之獲取指定網(wǎng)頁(yè)源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
python中進(jìn)程間通信及設(shè)置狀態(tài)量控制另一個(gè)進(jìn)程
這篇文章主要介紹了python中進(jìn)程間通信及設(shè)置狀態(tài)量控制另一個(gè)進(jìn)程,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05
Python實(shí)現(xiàn)將DOC文檔轉(zhuǎn)換為PDF的方法
這篇文章主要介紹了Python實(shí)現(xiàn)將DOC文檔轉(zhuǎn)換為PDF的方法,涉及Python調(diào)用系統(tǒng)win32com組件實(shí)現(xiàn)文件格式轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-07-07

