總目錄
圖像處理總目錄←點(diǎn)擊這里
十九、停車(chē)場(chǎng)車(chē)位識(shí)別
19.1、項(xiàng)目說(shuō)明
唐宇迪老師的——OPENCV項(xiàng)目實(shí)戰(zhàn) 學(xué)習(xí)
本項(xiàng)目的目的是設(shè)計(jì)一個(gè)停車(chē)場(chǎng)車(chē)位識(shí)別的系統(tǒng),能夠判斷出當(dāng)前停車(chē)場(chǎng)中哪些車(chē)位是空的。
任務(wù)共包含部分:
- 對(duì)圖像預(yù)處理
- 從停車(chē)場(chǎng)的監(jiān)控視頻中提取圖片
- 對(duì)圖片進(jìn)行一系列的預(yù)處理,去噪、識(shí)別圖像中的車(chē)位、定位停車(chē)位的輪廓
- 提取每個(gè)停車(chē)位區(qū)域的圖片并保存到指定的數(shù)據(jù)文件路徑
- 訓(xùn)練神經(jīng)網(wǎng)絡(luò)模型,能夠識(shí)別停車(chē)場(chǎng)中有哪些剩余車(chē)位
- 使用keras訓(xùn)練一個(gè)2分類(lèi)的模型,卷積網(wǎng)絡(luò)選擇vgg,采用keras提供的api,并凍結(jié)前10層
- 從視頻中取出每一幀,挨個(gè)訓(xùn)練,實(shí)現(xiàn)實(shí)時(shí)
著重介紹圖像處理(神經(jīng)網(wǎng)絡(luò)是一個(gè)簡(jiǎn)單的二分類(lèi)問(wèn)題,有車(chē)和沒(méi)車(chē)的一個(gè)判斷)
19.2、圖像預(yù)處理
19.2.1、讀取圖像
從視頻中截取兩張圖,進(jìn)行讀取操作
19.2.2、過(guò)濾背景
select_rgb_white_yellow
函數(shù)實(shí)現(xiàn)
HSV各種顏色的取值范圍white_mask = cv2.inRange(image, lower, upper)
低于120,或者高于255的都處理為0(黑色)
將其與原始圖像做與操作,這樣的話(huà),只有原始圖像是255的像素點(diǎn)留下來(lái)了,把無(wú)關(guān)的操作過(guò)濾掉
cv2.bitwise_and(image, image, mask=white_mask)
19.2.3、灰度圖
convert_gray_scale
函數(shù)實(shí)現(xiàn)
cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
19.2.4、邊緣檢測(cè)
detect_edges
函數(shù)實(shí)現(xiàn)
cv2.Canny(image, low_threshold, high_threshold)
19.2.5、裁剪區(qū)域
select_region
函數(shù)實(shí)現(xiàn)
手動(dòng)畫(huà)點(diǎn)
rows, cols = image.shape[:2]
pt_1 = [cols * 0.05, rows * 0.90]
pt_2 = [cols * 0.05, rows * 0.70]
pt_3 = [cols * 0.30, rows * 0.55]
pt_4 = [cols * 0.6, rows * 0.15]
pt_5 = [cols * 0.90, rows * 0.15]
pt_6 = [cols * 0.90, rows * 0.90]
mask填充
為選定區(qū)域填充白色
mask = np.zeros_like(image)
cv2.fillPoly(mask, vertices, 255)
區(qū)域切割
cv2.bitwise_and(image, mask)
19.2.6、檢測(cè)直線(xiàn)
hough_lines
和draw_lines
函數(shù)實(shí)現(xiàn)
通過(guò) 霍夫變換 (邊緣檢測(cè)后使用)后檢測(cè)到停車(chē)位的方框直線(xiàn)
cv2.HoughLinesP(image, rho=0.1, theta=np.pi / 10, threshold=15, minLineLength=9, maxLineGap=4)
- rho距離精度
- theta角度精度
- threshod超過(guò)設(shè)定閾值才被檢測(cè)出線(xiàn)段
- minLineLengh 線(xiàn)的最短長(zhǎng)度,比這個(gè)短的都被忽略
- MaxLineCap 兩條直線(xiàn)之間的最大間隔,小于此值,認(rèn)為是一條直線(xiàn)
檢測(cè)完之后進(jìn)行畫(huà)線(xiàn)操作
19.2.7、區(qū)域列車(chē)位劃分
identify_blocks
函數(shù) 實(shí)現(xiàn)
過(guò)濾部分直線(xiàn)
一列一列的為一組,過(guò)濾掉不符合的
for line in lines:
for x1, y1, x2, y2 in line:
if abs(y2 - y1) <= 1 and 25 <= abs(x2 - x1) <= 55:
cleaned.append((x1, y1, x2, y2))
直線(xiàn)x1排序
默認(rèn)是打亂排序的
x1是每一列車(chē)位的最左邊位置
sorted(cleaned, key=operator.itemgetter(0, 1))
分為n列
找到多個(gè)列,相當(dāng)于每列是一排車(chē)
根據(jù)線(xiàn)之間的距離進(jìn)行判斷
for i in range(len(list1) - 1):
distance = abs(list1[i + 1][0] - list1[i][0])
if distance <= clus_dist:
if not dIndex in clusters.keys(): clusters[dIndex] = []
clusters[dIndex].append(list1[i])
clusters[dIndex].append(list1[i + 1])
else:
dIndex += 1
坐標(biāo)
通過(guò)循環(huán),得到每一列的具體坐標(biāo)(矩形框的四個(gè)坐標(biāo))
for key in clusters:
all_list = clusters[key]
cleaned = list(set(all_list))
if len(cleaned) > 5:
cleaned = sorted(cleaned, key=lambda tup: tup[1])
avg_y1 = cleaned[0][1]
avg_y2 = cleaned[-1][1]
avg_x1 = 0
avg_x2 = 0
for tup in cleaned:
avg_x1 += tup[0]
avg_x2 += tup[2]
avg_x1 = avg_x1 / len(cleaned)
avg_x2 = avg_x2 / len(cleaned)
rects[i] = (avg_x1, avg_y1, avg_x2, avg_y2)
i += 1
畫(huà)出車(chē)位
通過(guò)觀察可以看出,右邊的區(qū)域更符合條件(左邊的第一列第二列劃分不好)
19.2.8、區(qū)域每個(gè)車(chē)位劃分
draw_parking
函數(shù)實(shí)現(xiàn)
注意第一列和最后一列是單排停車(chē)位
其余為雙排停車(chē)位,分類(lèi)討論
for i in range(0, num_splits + 1):
y = int(y1 + i * gap)
cv2.line(new_image, (x1, y), (x2, y), color, thickness)
if key > 0 and key < len(rects) - 1:
# 豎直線(xiàn)
x = int((x1 + x2) / 2)
cv2.line(new_image, (x, y1), (x, y2), color, thickness)
19.3、訓(xùn)練神經(jīng)網(wǎng)絡(luò)
判斷車(chē)位上面有沒(méi)有車(chē)
19.3.1、切割停車(chē)位圖片
save_images_for_cnn
函數(shù)實(shí)現(xiàn)
得到每一個(gè)停車(chē)位上面的圖片,判斷是否有車(chē)
人工對(duì)車(chē)位進(jìn)行分割: 分為有車(chē)和沒(méi)車(chē)兩種類(lèi)型
19.3.2、訓(xùn)練模型
train.py
文件進(jìn)行模型訓(xùn)練
使用vgg16模型進(jìn)行訓(xùn)練
model = applications.VGG16(weights='imagenet', include_top=False, input_shape=(img_width, img_height, 3))
數(shù)據(jù)量較少,對(duì)vgg原模型進(jìn)行“凍”起來(lái)操作,只需要改自己的全連接層和輸出層
# 凍起來(lái)操作:對(duì)前10層的網(wǎng)絡(luò)結(jié)構(gòu)不進(jìn)行更改
for layer in model.layers[:10]:
layer.trainable = False
19.3.3、基于視頻的車(chē)位檢測(cè)
predict_on_image
函數(shù)處理某幀的情況
測(cè)試了上面剛開(kāi)始的兩張圖像
19.4、最終效果
predict_on_video
函數(shù)處理視頻
將每一幀圖像保存下來(lái),做成動(dòng)圖效果
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-536368.html
項(xiàng)目源碼
https://github.com/lzh66666/park_opencv文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-536368.html
到了這里,關(guān)于OpenCV圖像處理——停車(chē)場(chǎng)車(chē)位識(shí)別的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!