欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python道路車道線檢測的實現(xiàn)

 更新時間:2021年06月27日 08:43:33   作者:初遇我ㄖ寸の熱情呢?  
在本文中,我們將構(gòu)建一個機器學(xué)習(xí)項目來實時檢測車道線。我們將使用 OpenCV 庫使用計算機視覺的概念來做到這一點,感興趣的可以了解一下

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

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

在這里插入圖片描述

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

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

制作后我們需要檢測車道線。用于檢測此類數(shù)學(xué)形狀的技術(shù)稱為霍夫變換?;舴蜃儞Q可以檢測矩形、圓形、三角形和直線等形狀。

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

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

1.導(dǎo)入包

import matplotlib.pyplot as plt

import numpy as np
import cv2
import os
import matplotlib.image as mpimg
from moviepy.editor import VideoFileClip
import math

2. 應(yīng)用幀屏蔽并找到感興趣的區(qū)域:

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.霍夫變換空間中像素到線的轉(zhuǎn)換:

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. 霍夫變換后在每一幀中創(chuàng)建兩條線:

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 1
    
    x1_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_l
        y1_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_frame        
        first_frame = 0        
    else :
        prev_frame = cache
        next_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. 將輸入視頻剪輯成幀并得到結(jié)果輸出視頻文件:

first_frame = 1
white_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 代碼:

在這里插入圖片描述

import tkinter as tk
from tkinter import *
import cv2
from PIL import Image, ImageTk
import os
import numpy as np


global 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 cap1
global cap2
cap1 = 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_frame1
        last_frame1 = frame1.copy()
        pic = cv2.cvtColor(last_frame1, cv2.COLOR_BGR2RGB)     
        img = Image.fromarray(pic)
        imgtk = ImageTk.PhotoImage(image=img)
        lmain.imgtk = imgtk
        lmain.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_frame2
        last_frame2 = frame2.copy()
        pic2 = cv2.cvtColor(last_frame2, cv2.COLOR_BGR2RGB)
        img2 = Image.fromarray(pic2)
        img2tk = ImageTk.PhotoImage(image=img2)
        lmain2.img2tk = img2tk
        lmain2.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()

到此這篇關(guān)于Python道路車道線檢測的實現(xiàn)的文章就介紹到這了,更多相關(guān)Python 道路車道線檢測內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:

相關(guān)文章

  • Python實現(xiàn)簡單遺傳算法(SGA)

    Python實現(xiàn)簡單遺傳算法(SGA)

    這篇文章主要為大家詳細介紹了Python實現(xiàn)簡單遺傳算法SGA,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 常用python爬蟲庫介紹與簡要說明

    常用python爬蟲庫介紹與簡要說明

    本文介紹了一些常用的python爬蟲庫其中包括python網(wǎng)絡(luò)庫,python網(wǎng)絡(luò)爬蟲框架,python HTML解析,python文本處理,python 自然語言處理,python 瀏覽器模擬等各種常用的python庫
    2020-01-01
  • Python中的圖形繪制簡單動畫實操

    Python中的圖形繪制簡單動畫實操

    這篇文章主要介紹了Python中的圖形繪制簡單動畫實操,?Matplotlib?是一個非常廣泛的庫,它也支持圖形動畫,動畫工具以?matplotlib.animation?基類為中心,它提供了一個框架,圍繞該框架構(gòu)建動畫,下面來看看具體的實現(xiàn)過程吧,需要的小伙伴可以參考一下
    2022-02-02
  • 探究Python中isalnum()方法的使用

    探究Python中isalnum()方法的使用

    這篇文章主要介紹了探究Python中isalnum()方法的使用,是Python入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-05-05
  • python scrapy腳本報錯問題及解決

    python scrapy腳本報錯問題及解決

    這篇文章主要介紹了python scrapy腳本報錯問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • python調(diào)用cmd命令行制作刷博器

    python調(diào)用cmd命令行制作刷博器

    這篇文章主要介紹了Python制作一個簡單的刷博器,可以學(xué)習(xí)Python線程、調(diào)用cmd命令行、打開網(wǎng)頁的知識點,大家參考使用吧
    2014-01-01
  • python3-flask-3將信息寫入日志的實操方法

    python3-flask-3將信息寫入日志的實操方法

    在本篇文章里小編給大家整理的是關(guān)于python3-flask-3將信息寫入日志的實操方法,有興趣的朋友們學(xué)習(xí)下。
    2019-11-11
  • python中ConfigParse模塊的用法

    python中ConfigParse模塊的用法

    這篇文章主要介紹了python中ConfigParse模塊的用法,以實例形式講述了配置文件模塊ConfigParse的使用步驟,非常具有實用價值,需要的朋友可以參考下
    2014-09-09
  • 使用Python實現(xiàn)NBA球員數(shù)據(jù)查詢小程序功能

    使用Python實現(xiàn)NBA球員數(shù)據(jù)查詢小程序功能

    這篇文章主要介紹了使用Python實現(xiàn)NBA球員數(shù)據(jù)查詢小程序功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • python變量前面加星(*)的含義及說明

    python變量前面加星(*)的含義及說明

    這篇文章主要介紹了python變量前面加星(*)的含義及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06

最新評論