濮阳杆衣贸易有限公司

主頁 > 知識庫 > python+opencv實(shí)現(xiàn)車道線檢測

python+opencv實(shí)現(xiàn)車道線檢測

熱門標(biāo)簽:鎮(zhèn)江人工外呼系統(tǒng)供應(yīng)商 高德地圖標(biāo)注字母 400電話辦理費(fèi)用收費(fèi) 千呼ai電話機(jī)器人免費(fèi) 外呼系統(tǒng)前面有錄音播放嗎 深圳網(wǎng)絡(luò)外呼系統(tǒng)代理商 柳州正規(guī)電銷機(jī)器人收費(fèi) 騰訊地圖標(biāo)注有什么版本 申請辦個400電話號碼

python+opencv車道線檢測(簡易實(shí)現(xiàn)),供大家參考,具體內(nèi)容如下

技術(shù)棧:python+opencv

實(shí)現(xiàn)思路:

1、canny邊緣檢測獲取圖中的邊緣信息;
2、霍夫變換尋找圖中直線;
3、繪制梯形感興趣區(qū)域獲得車前范圍;
4、得到并繪制車道線;

效果展示:

代碼實(shí)現(xiàn):

import cv2
import numpy as np


def canny():
 gray = cv2.cvtColor(lane_image, cv2.COLOR_RGB2GRAY)
 #高斯濾波
 blur = cv2.GaussianBlur(gray, (5, 5), 0)
 #邊緣檢測
 canny_img = cv2.Canny(blur, 50, 150)
 return canny_img


def region_of_interest(r_image):
 h = r_image.shape[0]
 w = r_image.shape[1]
 # 這個區(qū)域不穩(wěn)定,需要根據(jù)圖片更換
 poly = np.array([
 [(100, h), (500, h), (290, 180), (250, 180)]
 ])
 mask = np.zeros_like(r_image)
 # 繪制掩膜圖像
 cv2.fillPoly(mask, poly, 255)
 # 獲得ROI區(qū)域
 masked_image = cv2.bitwise_and(r_image, mask)
 return masked_image


if __name__ == '__main__':
 image = cv2.imread('test.jpg')
 lane_image = np.copy(image)
 canny = canny()
 cropped_image = region_of_interest(canny)
 cv2.imshow("result", cropped_image)
 cv2.waitKey(0)

霍夫變換加線性擬合改良:

效果圖:

代碼實(shí)現(xiàn):

主要增加了根據(jù)斜率作線性擬合過濾無用點(diǎn)后連線的操作;

import cv2
import numpy as np


def canny():
 gray = cv2.cvtColor(lane_image, cv2.COLOR_RGB2GRAY)
 blur = cv2.GaussianBlur(gray, (5, 5), 0)

 canny_img = cv2.Canny(blur, 50, 150)
 return canny_img


def region_of_interest(r_image):
 h = r_image.shape[0]
 w = r_image.shape[1]

 poly = np.array([
 [(100, h), (500, h), (280, 180), (250, 180)]
 ])
 mask = np.zeros_like(r_image)
 cv2.fillPoly(mask, poly, 255)
 masked_image = cv2.bitwise_and(r_image, mask)
 return masked_image


def get_lines(img_lines):
 if img_lines is not None:
 for line in lines:
 for x1, y1, x2, y2 in line:
 # 分左右車道
 k = (y2 - y1) / (x2 - x1)
 if k  0:
  lefts.append(line)
 else:
  rights.append(line)


def choose_lines(after_lines, slo_th): # 過濾斜率差別較大的點(diǎn)
 slope = [(y2 - y1) / (x2 - x1) for line in after_lines for x1, x2, y1, y2 in line] # 獲得斜率數(shù)組
 while len(after_lines) > 0:
 mean = np.mean(slope) # 計(jì)算平均斜率
 diff = [abs(s - mean) for s in slope] # 每條線斜率與平均斜率的差距
 idx = np.argmax(diff) # 找到最大斜率的索引
 if diff[idx] > slo_th: # 大于預(yù)設(shè)的閾值選取
 slope.pop(idx)
 after_lines.pop(idx)
 else:
 break

 return after_lines


def clac_edgepoints(points, y_min, y_max):
 x = [p[0] for p in points]
 y = [p[1] for p in points]

 k = np.polyfit(y, x, 1) # 曲線擬合的函數(shù),找到xy的擬合關(guān)系斜率
 func = np.poly1d(k) # 斜率代入可以得到一個y=kx的函數(shù)

 x_min = int(func(y_min)) # y_min = 325其實(shí)是近似找了一個
 x_max = int(func(y_max))

 return [(x_min, y_min), (x_max, y_max)]


if __name__ == '__main__':
 image = cv2.imread('F:\\A_javaPro\\test.jpg')
 lane_image = np.copy(image)
 canny_img = canny()
 cropped_image = region_of_interest(canny_img)
 lefts = []
 rights = []
 lines = cv2.HoughLinesP(cropped_image, 1, np.pi / 180, 15, np.array([]), minLineLength=40, maxLineGap=20)
 get_lines(lines) # 分別得到左右車道線的圖片

 good_leftlines = choose_lines(lefts, 0.1) # 處理后的點(diǎn)
 good_rightlines = choose_lines(rights, 0.1)

 leftpoints = [(x1, y1) for left in good_leftlines for x1, y1, x2, y2 in left]
 leftpoints = leftpoints + [(x2, y2) for left in good_leftlines for x1, y1, x2, y2 in left]

 rightpoints = [(x1, y1) for right in good_rightlines for x1, y1, x2, y2 in right]
 rightpoints = rightpoints + [(x2, y2) for right in good_rightlines for x1, y1, x2, y2 in right]

 lefttop = clac_edgepoints(leftpoints, 180, image.shape[0]) # 要畫左右車道線的端點(diǎn)
 righttop = clac_edgepoints(rightpoints, 180, image.shape[0])

 src = np.zeros_like(image)

 cv2.line(src, lefttop[0], lefttop[1], (255, 255, 0), 7)
 cv2.line(src, righttop[0], righttop[1], (255, 255, 0), 7)

 cv2.imshow('line Image', src)
 src_2 = cv2.addWeighted(image, 0.8, src, 1, 0)
 cv2.imshow('Finally Image', src_2)

 cv2.waitKey(0)

待改進(jìn):

代碼實(shí)用性差,幾乎不能用于實(shí)際,但是可以作為初學(xué)者的練手項(xiàng)目;
斑馬線檢測思路:獲取車前感興趣區(qū)域,判斷白色像素點(diǎn)比例即可實(shí)現(xiàn);
行人檢測思路:opencv有內(nèi)置行人檢測函數(shù),基于內(nèi)置的訓(xùn)練好的數(shù)據(jù)集;

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • python基于OpenCV模板匹配識別圖片中的數(shù)字
  • Python OpenCV高斯金字塔與拉普拉斯金字塔的實(shí)現(xiàn)
  • Python OpenCV 基于圖像邊緣提取的輪廓發(fā)現(xiàn)函數(shù)
  • Python opencv操作深入詳解
  • Python+Opencv實(shí)現(xiàn)數(shù)字識別的示例代碼
  • python中的opencv和PIL(pillow)轉(zhuǎn)化操作
  • OpenCV+Python幾何變換的實(shí)現(xiàn)示例
  • python利用opencv實(shí)現(xiàn)顏色檢測
  • python opencv實(shí)現(xiàn)圖像配準(zhǔn)與比較
  • python OpenCV學(xué)習(xí)筆記

標(biāo)簽:郴州 哈爾濱 烏蘭察布 烏蘭察布 大慶 合肥 海南 平頂山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python+opencv實(shí)現(xiàn)車道線檢測》,本文關(guān)鍵詞  python+opencv,實(shí)現(xiàn),車道,線,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python+opencv實(shí)現(xiàn)車道線檢測》相關(guān)的同類信息!
  • 本頁收集關(guān)于python+opencv實(shí)現(xiàn)車道線檢測的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    缙云县| 樟树市| 郑州市| 天等县| 怀化市| 青河县| 辽源市| 建平县| 嵊泗县| 天津市| 嵩明县| 永福县| 五河县| 定日县| 洪湖市| 通化县| 漳浦县| 营口市| 电白县| 阳原县| 中阳县| 义乌市| 马尔康县| 高尔夫| 龙口市| 元谋县| 大理市| 巴彦县| 固安县| 昆明市| 洛阳市| 天镇县| 本溪| 昭通市| 清原| 铁力市| 沽源县| 河南省| 瑞金市| 洪雅县| 罗定市|