詳解車(chē)道線檢測(cè)算法之傳統(tǒng)圖像處理
繪制車(chē)道線
# 定義左右車(chē)道線的點(diǎn)集合right_list = []left_list = []
# 根據(jù)霍夫變換得到的線段,繪制左右各一條車(chē)道線if lines is not None: for line in lines: for x1,y1,x2,y2 in line: if (y1 - y2) / (x1 - x2) > 0:# right side right_list.a(chǎn)ppend([x1,y1,x2,y2,(y2-y1)/(x2-x1),(x1*y2-x2*y1)/(x1-x2)]) else:# left side left_list.a(chǎn)ppend([x1,y1,x2,y2,(y2-y1)/(x2-x1),(x1*y2-x2*y1)/(x1-x2)])# 通過(guò)計(jì)算斜率,篩選得到最優(yōu)車(chē)道線 if len(right_list) != 0: [k_right_mean,b_right_mean,x_list,y_list] = filter_lines(right_list) y1_right = img.shape[0]-60 x1_right = (y1_right - b_right_mean) / k_right_mean y2_right = img.shape[0]*.65 #min(y_right) x2_right = (y2_right - b_right_mean) / k_right_mean cv2.line(img, (np.int32(x1_right), np.int32(y1_right)), (np.int32(x2_right), np.int32(y2_right)), [255,0,0], thickness) if len(left_list) != 0: [k_left_mean,b_left_mean,x_list,y_list] = filter_lines(left_list) y1_left = img.shape[0]-60 x1_left = (y1_left - b_left_mean) / k_left_mean y2_left = img.shape[0]*.65 #min(y_left) x2_left = (y2_left - b_left_mean) / k_left_mean cv2.line(img, (np.int32(x1_left), np.int32(y1_left)), (np.int32(x2_left), np.int32(y2_left)), color, thickness)
def filter_lines(xy_kb_list): n = 3 k_list = [xy_kb[4] for xy_kb in xy_kb_list] mean = np.mean(k_list) std = np.std(k_list) good_k_list = [] good_b_list = [] good_x_list = [] good_y_list = [] for [x1, y1, x2, y2, k, b] in xy_kb_list: if k < mean + n*std: good_k_list.a(chǎn)ppend(k) good_b_list.a(chǎn)ppend(b) good_x_list.a(chǎn)ppend(x1) good_x_list.a(chǎn)ppend(x2) good_y_list.a(chǎn)ppend(y1) good_y_list.a(chǎn)ppend(y2) if not good_x_list: good_k_list = k_list good_b_list = [xy_kb[5] for xy_kb in xy_kb_list] good_x_list = [xy_kb[0] for xy_kb in xy_kb_list] + [xy_kb[2] for xy_kb in xy_kb_list] good_y_list = [xy_kb[1] for xy_kb in xy_kb_list] + [xy_kb[3] for xy_kb in xy_kb_list] k_mean = np.median(good_k_list) b_mean = np.median(good_b_list) return k_mean,b_mean,good_x_list,good_y_list
將算法應(yīng)用于視頻
output = 'test_video_output.mp4'
from moviepy.editor import VideoFileClip
clip1 = VideoFileClip("test_video.mp4")white_clip = clip1.fl_image(process_image) %time white_clip.write_videofile(output , audio=False)
高級(jí)車(chē)道線檢測(cè)算法--曲線
計(jì)算相機(jī)校正矩陣和失真系數(shù)
# 使用提供的一組棋盤(pán)格圖片計(jì)算相機(jī)校正矩陣(camera calibration matrix)和失真系數(shù)(distortion coefficients).
objpoints = [] imgpoints = []
for ny in [5,6]: for nx in [6,7,8,9]: objp = np.zeros((ny*nx,3), np.float32) objp[:,:2] = np.mgrid[0:nx, 0:ny].T.reshape(-1,2)
for idx, fname in enumerate(images): img = cv2.imread(fname) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 使用cv2自動(dòng)尋找棋盤(pán)corners ret, corners = cv2.findChessboardCorners(gray, (nx,ny), None) if ret == True: objpoints.a(chǎn)ppend(objp) imgpoints.a(chǎn)ppend(corners) # 繪制corners cv2.drawChessboardCorners(img, (nx,ny), corners, ret) image_name=os.path.split(fname)[1] write_name = out_dir_cam+'corners_found_ny'+str(ny)+'_nx'+str(nx)+'_'+image_name cv2.imwrite(write_name, img) print(fname) plt.imshow(img)
# Test undistortion on an imageimg = cv2.imread('camera_cal/calibration.jpg')img_size = (img.shape[1], img.shape[0])
# Do camera calibration given object points and image pointsret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, img_size,None,None)dst = cv2.undistort(img, mtx, dist, None, mtx)
# 保存相機(jī)校正矩陣和失真系數(shù)結(jié)果dist_pickle = {}dist_pickle["mtx"] = mtxdist_pickle["dist"] = distpickle.dump(dist_pickle, open( "camera_dist_pickle.p", "wb" ) )
校正車(chē)道線圖片
使用計(jì)算好的相機(jī)校正矩陣(camera calibration matrix)和失真系數(shù)(distortion coefficients)校正車(chē)道線圖片。
# 打開(kāi)相機(jī)校正矩陣和失真系數(shù)結(jié)果with open('camera_dist_pickle.p', mode='rb') as f: dist_pickle = pickle.load(f) mtx = dist_pickle["mtx"] dist = dist_pickle["dist"]
# 應(yīng)用于車(chē)道線圖片images = glob.glob('test_images.jpg')
for idx, fname in enumerate(images): img = cv2.imread(fname) dst = cv2.undistort(img, mtx, dist, None, mtx) image_name=os.path.split(fname)[1] write_name = out_dir_img+'undistorted_'+image_name cv2.imwrite(write_name,dst) print(write_name)
組合閾值處理
# 轉(zhuǎn)換到HLS顏色空間(也可嘗試其他顏色空間)hls = cv2.cvtColor(img, cv2.COLOR_RGB2HLS).a(chǎn)stype(np.float)l_channel = hls[:,:,1]s_channel = hls[:,:,2]
# 使用Sobel算法在x方向進(jìn)行閾值處理sobelx = cv2.Sobel(l_channel, cv2.CV_64F, 1, 0)abs_sobelx = np.a(chǎn)bsolute(sobelx) scaled_sobel = np.uint8(255*abs_sobelx/np.max(abs_sobelx))
發(fā)表評(píng)論
請(qǐng)輸入評(píng)論內(nèi)容...
請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字
圖片新聞
最新活動(dòng)更多
-
11月19日立即報(bào)名>> 【線下論壇】華邦電子與恩智浦聯(lián)合技術(shù)論壇
-
12月19日立即報(bào)名>> 【線下會(huì)議】OFweek 2024(第九屆)物聯(lián)網(wǎng)產(chǎn)業(yè)大會(huì)
-
精彩回顧立即查看>> 蔡司新能源汽車(chē)三電質(zhì)量解決方案
-
精彩回顧立即查看>> 蔡司新能源汽車(chē)三電質(zhì)量解決方案
-
精彩回顧立即查看>> 2024(第五屆)全球數(shù)字經(jīng)濟(jì)產(chǎn)業(yè)大會(huì)暨展覽會(huì)
-
精彩回顧立即查看>> 【線下會(huì)議】全數(shù)會(huì)2024電子元器件展覽會(huì)
- 1 “作弊器”不靈了?智駕的激光雷達(dá),正在被攝像頭替代
- 2 被逼墻角的Mobileye,祭出 CAIS 大旗,挑戰(zhàn)端到端大模型智能駕駛
- 3 4 激光雷達(dá)與純視覺(jué)方案,哪個(gè)才是自動(dòng)駕駛最優(yōu)選?
- 5 激光雷達(dá)即將降價(jià),純視覺(jué)回到鄙視鏈底層?
- 6 如何跑贏汽車(chē)智能化下半場(chǎng)?
- 7 從小鵬、理想、蔚來(lái)布局看自動(dòng)駕駛發(fā)展趨勢(shì)
- 8 智能駕駛的百生相,你是該信呢?還是該信呢?
- 9 比亞迪技術(shù)魚(yú)池大戰(zhàn)豐田工具箱,誰(shuí)將主導(dǎo)未來(lái)汽車(chē)市場(chǎng)?
- 10 全網(wǎng)最詳解! 美國(guó)禁止中國(guó)智駕究竟包括哪些?
- IE工程師(汽車(chē)智聯(lián)) 惠州碩貝德無(wú)線科技股份有限公司
- 現(xiàn)場(chǎng)技術(shù)支持工程師(汽車(chē)) 易思維(杭州)科技股份有限公司
- 銷(xiāo)售經(jīng)理(汽車(chē)新能源行業(yè)) 廣州瑞松智能科技股份有限公司
- 結(jié)構(gòu)工程師-汽車(chē)電子事業(yè)部(J10116) 深圳奧尼電子股份有限公司
- 銷(xiāo)售總監(jiān)-汽車(chē)電子方向 深圳市智立方自動(dòng)化設(shè)備股份有限公司
- 項(xiàng)目經(jīng)理(汽車(chē)內(nèi)飾&汽車(chē)電子) 伯恩光學(xué)(惠州)有限公司
- 高級(jí)軟件工程師 廣東省/深圳市
- 自動(dòng)化高級(jí)工程師 廣東省/深圳市
- 光器件研發(fā)工程師 福建省/福州市
- 銷(xiāo)售總監(jiān)(光器件) 北京市/海淀區(qū)
- 激光器高級(jí)銷(xiāo)售經(jīng)理 上海市/虹口區(qū)
- 光器件物理工程師 北京市/海淀區(qū)
- 激光研發(fā)工程師 北京市/昌平區(qū)
- 技術(shù)專家 廣東省/江門(mén)市
- 封裝工程師 北京市/海淀區(qū)
- 結(jié)構(gòu)工程師 廣東省/深圳市