這篇文章將為大家詳細講解有關(guān)怎么在python中利用opencv實現(xiàn)一個車道線檢測功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:域名與空間、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、淇濱網(wǎng)站維護、網(wǎng)站推廣。實現(xiàn)思路:
1、canny邊緣檢測獲取圖中的邊緣信息;
2、霍夫變換尋找圖中直線;
3、繪制梯形感興趣區(qū)域獲得車前范圍;
4、得到并繪制車道線;
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)
代碼實現(xiàn):
主要增加了根據(jù)斜率作線性擬合過濾無用點后連線的操作;
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): # 過濾斜率差別較大的點 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) # 計算平均斜率 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其實是近似找了一個 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) # 處理后的點 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]) # 要畫左右車道線的端點 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)
關(guān)于怎么在python中利用opencv實現(xiàn)一個車道線檢測功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
當前文章:怎么在python中利用opencv實現(xiàn)一個車道線檢測功能-創(chuàng)新互聯(lián)
分享路徑:http://www.rwnh.cn/article40/cseheo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、域名注冊、定制開發(fā)、做網(wǎng)站、微信公眾號、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容