最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用OpenCV對車道進行實時檢測的實現(xiàn)示例代碼

 更新時間:2020年06月19日 11:11:04   作者:cofisher  
這篇文章主要介紹了使用OpenCV對車道進行實時檢測的實現(xiàn)示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

項目介紹

下圖中的兩條線即為車道:


我們的任務就是通過 OpenCV 在一段視頻(或攝像頭)中實時檢測出車道并將其標記出來。其效果如下圖所示:


這里使用的代碼來源于磐懟懟大神,此文章旨在對其代碼進行解釋。

實現(xiàn)步驟

1、將視頻的所有幀讀取為圖片;

2、創(chuàng)建掩碼并應用到這些圖片上;

3、圖像閾值化;

4、用霍夫線變換檢測車道;

5、將車道畫到每張圖片上;

6、將所有圖片合并為視頻。

代碼實現(xiàn)

1、導入需要的庫

import os
import re
import cv2
import numpy as np
from tqdm import notebook
import matplotlib.pyplot as plt

其中 tqdm.notebook 是用來顯示進度條的。

2、將圖片(視頻的每一幀)加載進來

這里我們已經(jīng)將視頻的每一幀讀取為圖片了,并將它們都放進 frames 文件夾。

# 獲取幀的文件名
col_frames = os.listdir('frames/') # 讀取 frames 文件夾下的所有圖片
col_frames.sort(key=lambda f: int(re.sub('\D', '', f))) # 按名稱對圖片進行排序

# 加載幀
col_images=[]
for i in notebook.tqdm(col_frames):
  img = cv2.imread('frames/'+i)
  col_images.append(img) # 將所有圖片添加進 col_images 列表

3、選擇一張圖片進行處理

3.1 選定一張圖片

# 指定一個索引
idx = 457

# plot frame
plt.figure(figsize=(10,10))
plt.imshow(col_images[idx][:,:,0], cmap= "gray")
plt.show()

3.2 創(chuàng)建掩碼

# 創(chuàng)建0矩陣
stencil = np.zeros_like(col_images[idx][:,:,0])

# 指定多邊形的坐標
polygon = np.array([[50,270], [220,160], [360,160], [480,270]])

# 用1填充多邊形
cv2.fillConvexPoly(stencil, polygon, 1)

# 畫出多邊形
plt.figure(figsize=(10,10))
plt.imshow(stencil, cmap= "gray")
plt.show()

3.3 將掩碼應用到圖片上

# 應用該多邊形作為掩碼
img = cv2.bitwise_and(col_images[idx][:,:,0], col_images[idx][:,:,0], mask=stencil)

# 畫出掩碼后的圖片
plt.figure(figsize=(10,10))
plt.imshow(img, cmap= "gray")
plt.show()

這里的按位與操作 cv2.bitwise_and() 可以參考OpenCV 之按位運算舉例解析一文。

3.4 圖像閾值化

# 應用圖像閾值化
ret, thresh = cv2.threshold(img, 130, 145, cv2.THRESH_BINARY)

# 畫出圖像
plt.figure(figsize=(10,10))
plt.imshow(thresh, cmap= "gray")
plt.show()

其中 cv2.threshold 函數(shù)的用法可以參考Opencv之圖像閾值一文。

3.5 霍夫線變換檢測車道

lines = cv2.HoughLinesP(thresh, 1.0, np.pi/180, 30, maxLineGap=200)

# 創(chuàng)建原始幀的副本
dmy = col_images[idx][:,:,0].copy()

# 霍夫線
for line in lines:
  x1, y1, x2, y2 = line[0] # 提取出霍夫線的坐標
  cv2.line(dmy, (x1, y1), (x2, y2), (255, 0, 0), 3) # 將霍夫線畫在幀上

# 畫出幀
plt.figure(figsize=(10,10))
plt.imshow(dmy, cmap= "gray")
plt.show()

cv2.HoughLinesP() 函數(shù)介紹:

lines = HoughLinesP(image, rho, theta, threshold, minLineLength=None, maxLineGap=None)

輸入:

  • image: 必須是二值圖像;
  • rho: 線段以像素為單位的距離精度,double類型的,推薦用1.0
  • theta: 線段以弧度為單位的角度精度,推薦用numpy.pi/180
  • threshod: 累加平面的閾值參數(shù),int類型,超過設定閾值才被檢測出線段,值越大,基本上意味著檢出的線段越長,檢出的線段個數(shù)越少。
  • minLineLength:線段以像素為單位的最小長度。
  • maxLineGap:同一方向上兩條線段判定為一條線段的最大允許間隔,超過了設定值,則把兩條線段當成一條線段。

輸出:

lines:一個三維矩陣,其形狀符合 (m, 1, n),其中 m 表示直線個數(shù),n 表示每條直線的兩端坐標。

4、對每張圖片進行上一步驟的處理后寫入視頻

4.1 定義視頻格式

# 輸出視頻路徑
pathOut = 'roads_v2.mp4'

# 視頻每秒的幀數(shù)
fps = 30.0

# 視頻中每一幀的尺寸
height, width = img.shape
size = (width,height)

# 寫入視頻
out = cv2.VideoWriter(pathOut,cv2.VideoWriter_fourcc(*'DIVX'), fps, size)

4.2 處理所有圖片并寫入視頻文件

for img in notebook.tqdm(col_images):

  # 應用幀掩碼
  masked = cv2.bitwise_and(img[:,:,0], img[:,:,0], mask=stencil)

  # 應用圖像閾值化
  ret, thresh = cv2.threshold(masked, 130, 145, cv2.THRESH_BINARY)

  # 應用霍夫線變換
  lines = cv2.HoughLinesP(thresh, 1, np.pi/180, 30, maxLineGap=200)
  dmy = img.copy()

  #畫出檢測到的線
  try:
    for line in lines:
      x1, y1, x2, y2 = line[0]
      cv2.line(dmy, (x1, y1), (x2, y2), (255, 0, 0), 3)

    out.write(dmy)

  except TypeError: 
    out.write(img)

out.release()

完整代碼

import os
import re
import cv2
import numpy as np
from tqdm import notebook
import matplotlib.pyplot as plt

col_frames = os.listdir('frames/')
col_frames.sort(key=lambda f: int(re.sub('\D', '', f)))

col_images=[]
for i in notebook.tqdm(col_frames):
  img = cv2.imread('frames/'+i)
  col_images.append(img)

stencil = np.zeros_like(col_images[0][:,:,0])
polygon = np.array([[50,270], [220,160], [360,160], [480,270]])
cv2.fillConvexPoly(stencil, polygon, 1)

pathOut = 'roads_v2.mp4'

fps = 30.0

height, width = img.shape
size = (width,height)

out = cv2.VideoWriter(pathOut,cv2.VideoWriter_fourcc(*'DIVX'), fps, size)

for img in notebook.tqdm(col_images):

  masked = cv2.bitwise_and(img[:,:,0], img[:,:,0], mask=stencil)

  ret, thresh = cv2.threshold(masked, 130, 145, cv2.THRESH_BINARY)

  lines = cv2.HoughLinesP(thresh, 1, np.pi/180, 30, maxLineGap=200)
  dmy = img.copy()

  try:
    for line in lines:
      x1, y1, x2, y2 = line[0]
      cv2.line(dmy, (x1, y1), (x2, y2), (255, 0, 0), 3)

    out.write(dmy)

  except TypeError: 
    out.write(img)

out.release()

到此這篇關于使用OpenCV對車道進行實時檢測的實現(xiàn)示例代碼的文章就介紹到這了,更多相關OpenCV 車道實時檢測內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:

相關文章

最新評論

类乌齐县| 新田县| 四子王旗| 新平| 军事| 荣昌县| 闻喜县| 巴中市| 高唐县| 祥云县| 都昌县| 宁明县| 堆龙德庆县| 濮阳县| 赤壁市| 宝丰县| 凌云县| 琼结县| 临西县| 崇义县| 疏附县| 舟山市| 金山区| 安化县| 阳原县| 同江市| 灵山县| 宕昌县| 上虞市| 明光市| 汾阳市| 安图县| 临泽县| 德格县| 彭泽县| 巨鹿县| 永兴县| 库尔勒市| 晋中市| 富顺县| 和政县|