电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Python道路車道線檢測的實現

瀏覽:100日期:2022-06-15 16:58:07

車道線檢測是自動駕駛汽車以及一般計算機視覺的關鍵組件。這個概念用于描述自動駕駛汽車的路徑并避免進入另一條車道的風險。

在本文中,我們將構建一個機器學習項目來實時檢測車道線。我們將使用 OpenCV 庫使用計算機視覺的概念來做到這一點。為了檢測車道,我們必須檢測車道兩側的白色標記。

Python道路車道線檢測的實現

使用 Python 和 OpenCV 進行道路車道線檢測使用 Python 中的計算機視覺技術,我們將識別自動駕駛汽車必須行駛的道路車道線。這將是自動駕駛汽車的關鍵部分,因為自動駕駛汽車不應該越過它的車道,也不應該進入對面車道以避免事故。

幀掩碼和霍夫線變換要檢測車道中的白色標記,首先,我們需要屏蔽幀的其余部分。我們使用幀屏蔽來做到這一點。該幀只不過是圖像像素值的 NumPy 數組。為了掩蓋幀中不必要的像素,我們只需將 NumPy 數組中的這些像素值更新為 0。

制作后我們需要檢測車道線。用于檢測此類數學形狀的技術稱為霍夫變換。霍夫變換可以檢測矩形、圓形、三角形和直線等形狀。

代碼下載源碼請下載:車道線檢測項目代碼

按照以下步驟在 Python 中進行車道線檢測:

1.導入包

import matplotlib.pyplot as pltimport numpy as npimport cv2import osimport matplotlib.image as mpimgfrom moviepy.editor import VideoFileClipimport math

2. 應用幀屏蔽并找到感興趣的區域:

def interested_region(img, vertices): if len(img.shape) > 2: mask_color_ignore = (255,) * img.shape[2] else:mask_color_ignore = 255 cv2.fillPoly(np.zeros_like(img), vertices, mask_color_ignore) return cv2.bitwise_and(img, np.zeros_like(img))

3.霍夫變換空間中像素到線的轉換:

def hough_lines(img, rho, theta, threshold, min_line_len, max_line_gap): lines = cv2.HoughLinesP(img, rho, theta, threshold, np.array([]), minLineLength=min_line_len, maxLineGap=max_line_gap) line_img = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8) lines_drawn(line_img,lines) return line_img

4. 霍夫變換后在每一幀中創建兩條線:

def lines_drawn(img, lines, color=[255, 0, 0], thickness=6): global cache global first_frame slope_l, slope_r = [],[] lane_l,lane_r = [],[] α =0.2 for line in lines:for x1,y1,x2,y2 in line: slope = (y2-y1)/(x2-x1) if slope > 0.4:slope_r.append(slope)lane_r.append(line) elif slope < -0.4:slope_l.append(slope)lane_l.append(line)img.shape[0] = min(y1,y2,img.shape[0]) if((len(lane_l) == 0) or (len(lane_r) == 0)):print (’no lane detected’)return 1 slope_mean_l = np.mean(slope_l,axis =0) slope_mean_r = np.mean(slope_r,axis =0) mean_l = np.mean(np.array(lane_l),axis=0) mean_r = np.mean(np.array(lane_r),axis=0)if ((slope_mean_r == 0) or (slope_mean_l == 0 )):print(’dividing by zero’)return 1x1_l = int((img.shape[0] - mean_l[0][1] - (slope_mean_l * mean_l[0][0]))/slope_mean_l) x2_l = int((img.shape[0] - mean_l[0][1] - (slope_mean_l * mean_l[0][0]))/slope_mean_l) x1_r = int((img.shape[0] - mean_r[0][1] - (slope_mean_r * mean_r[0][0]))/slope_mean_r) x2_r = int((img.shape[0] - mean_r[0][1] - (slope_mean_r * mean_r[0][0]))/slope_mean_r) if x1_l > x1_r:x1_l = int((x1_l+x1_r)/2)x1_r = x1_ly1_l = int((slope_mean_l * x1_l ) + mean_l[0][1] - (slope_mean_l * mean_l[0][0]))y1_r = int((slope_mean_r * x1_r ) + mean_r[0][1] - (slope_mean_r * mean_r[0][0]))y2_l = int((slope_mean_l * x2_l ) + mean_l[0][1] - (slope_mean_l * mean_l[0][0]))y2_r = int((slope_mean_r * x2_r ) + mean_r[0][1] - (slope_mean_r * mean_r[0][0])) else:y1_l = img.shape[0]y2_l = img.shape[0]y1_r = img.shape[0]y2_r = img.shape[0] present_frame = np.array([x1_l,y1_l,x2_l,y2_l,x1_r,y1_r,x2_r,y2_r],dtype ='float32')if first_frame == 1:next_frame = present_framefirst_frame = 0 else :prev_frame = cachenext_frame = (1-α)*prev_frame+α*present_frame cv2.line(img, (int(next_frame[0]), int(next_frame[1])), (int(next_frame[2]),int(next_frame[3])), color, thickness) cv2.line(img, (int(next_frame[4]), int(next_frame[5])), (int(next_frame[6]),int(next_frame[7])), color, thickness)cache = next_frame

5.處理每一幀視頻以檢測車道:

def weighted_img(img, initial_img, α=0.8, β=1., λ=0.): return cv2.addWeighted(initial_img, α, img, β, λ)def process_image(image): global first_frame gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) img_hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV) lower_yellow = np.array([20, 100, 100], dtype = 'uint8') upper_yellow = np.array([30, 255, 255], dtype='uint8') mask_yellow = cv2.inRange(img_hsv, lower_yellow, upper_yellow) mask_white = cv2.inRange(gray_image, 200, 255) mask_yw = cv2.bitwise_or(mask_white, mask_yellow) mask_yw_image = cv2.bitwise_and(gray_image, mask_yw) gauss_gray= cv2.GaussianBlur(mask_yw_image, (5, 5), 0) canny_edges=cv2.Canny(gauss_gray, 50, 150) imshape = image.shape lower_left = [imshape[1]/9,imshape[0]] lower_right = [imshape[1]-imshape[1]/9,imshape[0]] top_left = [imshape[1]/2-imshape[1]/8,imshape[0]/2+imshape[0]/10] top_right = [imshape[1]/2+imshape[1]/8,imshape[0]/2+imshape[0]/10] vertices = [np.array([lower_left,top_left,top_right,lower_right],dtype=np.int32)] roi_image = interested_region(canny_edges, vertices) theta = np.pi/180 line_image = hough_lines(roi_image, 4, theta, 30, 100, 180) result = weighted_img(line_image, image, α=0.8, β=1., λ=0.) return result

6. 將輸入視頻剪輯成幀并得到結果輸出視頻文件:

first_frame = 1white_output = ’__path_to_output_file__’clip1 = VideoFileClip('__path_to_input_file__')white_clip = clip1.fl_image(process_image)white_clip.write_videofile(white_output, audio=False)

車道線檢測項目 GUI 代碼:

Python道路車道線檢測的實現

import tkinter as tkfrom tkinter import *import cv2from PIL import Image, ImageTkimport osimport numpy as npglobal last_frame1 last_frame1 = np.zeros((480, 640, 3), dtype=np.uint8)global last_frame2 last_frame2 = np.zeros((480, 640, 3), dtype=np.uint8)global cap1global cap2cap1 = cv2.VideoCapture('path_to_input_test_video')cap2 = cv2.VideoCapture('path_to_resultant_lane_detected_video')def show_vid(): if not cap1.isOpened(): print('cant open the camera1') flag1, frame1 = cap1.read() frame1 = cv2.resize(frame1,(400,500)) if flag1 is None:print ('Major error!') elif flag1:global last_frame1last_frame1 = frame1.copy()pic = cv2.cvtColor(last_frame1, cv2.COLOR_BGR2RGB) img = Image.fromarray(pic)imgtk = ImageTk.PhotoImage(image=img)lmain.imgtk = imgtklmain.configure(image=imgtk)lmain.after(10, show_vid)def show_vid2(): if not cap2.isOpened(): print('cant open the camera2') flag2, frame2 = cap2.read() frame2 = cv2.resize(frame2,(400,500)) if flag2 is None:print ('Major error2!') elif flag2:global last_frame2last_frame2 = frame2.copy()pic2 = cv2.cvtColor(last_frame2, cv2.COLOR_BGR2RGB)img2 = Image.fromarray(pic2)img2tk = ImageTk.PhotoImage(image=img2)lmain2.img2tk = img2tklmain2.configure(image=img2tk)lmain2.after(10, show_vid2)if __name__ == ’__main__’: root=tk.Tk() lmain = tk.Label(master=root) lmain2 = tk.Label(master=root) lmain.pack(side = LEFT) lmain2.pack(side = RIGHT) root.title('Lane-line detection')root.geometry('900x700+100+10') exitbutton = Button(root, text=’Quit’,fg='red',command= root.destroy).pack(side = BOTTOM,) show_vid() show_vid2() root.mainloop() cap.release()

到此這篇關于Python道路車道線檢測的實現的文章就介紹到這了,更多相關Python 道路車道線檢測內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 全自动端子机|刺破式端子压接机|全自动双头沾锡机|全自动插胶壳端子机-东莞市傅氏兄弟机械设备有限公司 | 分类168信息网 - 分类信息网 免费发布与查询 | 真空搅拌机-行星搅拌机-双行星动力混合机-广州市番禺区源创化工设备厂 | 宽带办理,电信宽带,移动宽带,联通宽带,电信宽带办理,移动宽带办理,联通宽带办理 | 电动卫生级调节阀,电动防爆球阀,电动软密封蝶阀,气动高压球阀,气动对夹蝶阀,气动V型调节球阀-上海川沪阀门有限公司 | 办公室装修_上海办公室设计装修_时尚办公新主张-后街印象 | 耐压仪-高压耐压仪|徐吉电气| 太平洋亲子网_健康育儿 品质生活 | 盘古网络技术有限公司| 香蕉筛|直线|等厚|弧形|振动筛|香蕉筛厂家-洛阳隆中重工 | 根系分析仪,大米外观品质检测仪,考种仪,藻类鉴定计数仪,叶面积仪,菌落计数仪,抑菌圈测量仪,抗生素效价测定仪,植物表型仪,冠层分析仪-杭州万深检测仪器网 | 氮化镓芯片-碳化硅二极管 - 华燊泰半导体 | B2B网站_B2B免费发布信息网站_B2B企业贸易平台 - 企资网 | 涿州网站建设_网站设计_网站制作_做网站_固安良言多米网络公司 | 北京办公室装修,办公室设计,写字楼装修-北京金视觉装饰工程公司 北京成考网-北京成人高考网 | 专业甜品培训学校_广东糖水培训_奶茶培训_特色小吃培训_广州烘趣甜品培训机构 | 杭州画室_十大画室_白墙画室_杭州美术培训_国美附中培训_附中考前培训_升学率高的画室_美术中考集训美术高考集训基地 | Honsberg流量计-Greisinger真空表-气压计-上海欧臻机电设备有限公司 | 美国查特CHART MVE液氮罐_查特杜瓦瓶_制造全球品质液氮罐 | 流变仪-热分析联用仪-热膨胀仪厂家-耐驰科学仪器商贸 | 非小号行情 - 专业的区块链、数字藏品行情APP、金色财经官网 | 铝合金线槽_铝型材加工_空调挡水板厂家-江阴炜福金属制品有限公司 | 天津次氯酸钠酸钙溶液-天津氢氧化钠厂家-天津市辅仁化工有限公司 | China plate rolling machine manufacturer,cone rolling machine-Saint Fighter | 飞行者联盟-飞机模拟机_无人机_低空经济_航空技术交流平台 | 直齿驱动-新型回转驱动和回转支承解决方案提供商-不二传动 | 恒湿机_除湿加湿一体机_恒湿净化消毒一体机厂家-杭州英腾电器有限公司 | 合肥宠物店装修_合肥宠物美容院装修_合肥宠物医院设计装修公司-安徽盛世和居装饰 | 液压油缸-液压站生产厂家-洛阳泰诺液压科技有限公司 | 二手注塑机回收_旧注塑机回收_二手注塑机买卖 - 大鑫二手注塑机 二手光谱仪维修-德国OBLF光谱仪|进口斯派克光谱仪-热电ARL光谱仪-意大利GNR光谱仪-永晖检测 | 加气混凝土砌块设备,轻质砖设备,蒸养砖设备,新型墙体设备-河南省杜甫机械制造有限公司 | 气象监测系统_气象传感器_微型气象仪_气象环境监测仪-山东风途物联网 | 冷凝水循环试验箱-冷凝水试验箱-可编程高低温试验箱厂家-上海巨为(www.juweigroup.com) | 假肢-假肢价格-假肢厂家-河南假肢-郑州市力康假肢矫形器有限公司 | 艾默生变频器,艾默生ct,变频器,ct驱动器,广州艾默生变频器,供水专用变频器,风机变频器,电梯变频器,艾默生变频器代理-广州市盟雄贸易有限公司官方网站-艾默生变频器应用解决方案服务商 | 设定时间记录电子秤-自动累计储存电子秤-昆山巨天仪器设备有限公司 | 北京银联移动POS机办理_收银POS机_智能pos机_刷卡机_收银系统_个人POS机-谷骐科技【官网】 | 深圳侦探联系方式_深圳小三调查取证公司_深圳小三分离机构 | 昆明网络公司|云南网络公司|昆明网站建设公司|昆明网页设计|云南网站制作|新媒体运营公司|APP开发|小程序研发|尽在昆明奥远科技有限公司 | 细沙回收机-尾矿干排脱水筛设备-泥石分离机-建筑垃圾分拣机厂家-青州冠诚重工机械有限公司 | 深圳希玛林顺潮眼科医院(官网)│深圳眼科医院│医保定点│香港希玛林顺潮眼科中心连锁品牌 |