博客專欄

        EEPW首頁(yè) > 博客 > python實(shí)現(xiàn)簡(jiǎn)單的車道線檢測(cè)

        python實(shí)現(xiàn)簡(jiǎn)單的車道線檢測(cè)

        發(fā)布人:計(jì)算機(jī)視覺(jué)工坊 時(shí)間:2023-02-21 來(lái)源:工程師 發(fā)布文章

        python實(shí)現(xiàn)簡(jiǎn)單的車道線檢測(cè),本文章將介紹兩種簡(jiǎn)單的方法


        1.顏色閾值+區(qū)域掩模

        2.canny邊緣檢測(cè)+霍夫變換


        這兩種方法都能實(shí)現(xiàn)簡(jiǎn)單的車道線檢測(cè)demo,注意僅僅是demo


        下面的圖片是用到的測(cè)試圖片


        圖片




        1.顏色閾值+ 區(qū)域掩模



        我們可以僅僅通過(guò)設(shè)置一些RGB通道閾值,來(lái)提取車道線。


        以下的代碼設(shè)置了RGB通道閾值為220,大于220的像素將設(shè)置為黑色,這樣可以將測(cè)試圖片中的車道線提取出來(lái)


        效果如下


        圖片


        我們發(fā)現(xiàn)符合閾值的像素既包括了車道線,也包含了其他非車道線部分。


        顯然,一個(gè)成熟的自動(dòng)駕駛感知算法,是不可能使用這種方法的。僅僅依靠顏色,既不科學(xué)也不魯棒。


        有一種改進(jìn)思路是利用圖像掩模的方法


        假設(shè)拍攝圖像的前置攝像頭安裝在汽車上的固定位置,這樣車道線將始終出現(xiàn)在圖像的相同區(qū)域中。我們將設(shè)置了一個(gè)區(qū)域,認(rèn)為車道線處于該區(qū)域內(nèi)。


        我們?cè)O(shè)置了一個(gè)三角形的區(qū)域,原則上你可以使用其他形狀


        圖片



        python代碼如下






















































        import matplotlib.pyplot as pltimport matplotlib.image as mpimgimport numpy as np
        # Read in the imageimage = mpimg.imread('test.jpg')
        # Grab the x and y sizes and make two copies of the image# With one copy we'll extract only the pixels that meet our selection,# then we'll paint those pixels red in the original image to see our selection# overlaid on the original.ysize = image.shape[0]xsize = image.shape[1]color_select= np.copy(image)line_image = np.copy(image)
        # Define our color criteriared_threshold = 220green_threshold = 220blue_threshold = 220rgb_threshold = [red_threshold, green_threshold, blue_threshold]
        # Define a triangle region of interest (Note: if you run this code,left_bottom = [0, ysize-1]right_bottom = [xsize-1, ysize-1]apex = [650, 400]
        fit_left = np.polyfit((left_bottom[0], apex[0]), (left_bottom[1], apex[1]), 1)fit_right = np.polyfit((right_bottom[0], apex[0]), (right_bottom[1], apex[1]), 1)fit_bottom = np.polyfit((left_bottom[0], right_bottom[0]), (left_bottom[1], right_bottom[1]), 1)
        # Mask pixels below the thresholdcolor_thresholds = (image[:,:,0] < rgb_threshold[0]) | \                    (image[:,:,1] < rgb_threshold[1]) | \                    (image[:,:,2] < rgb_threshold[2])
        # Find the region inside the linesXX, YY = np.meshgrid(np.arange(0, xsize), np.arange(0, ysize))region_thresholds = (YY > (XX*fit_left[0] + fit_left[1])) & \                    (YY > (XX*fit_right[0] + fit_right[1])) & \                    (YY < (XX*fit_bottom[0] + fit_bottom[1]))# Mask color selectioncolor_select[color_thresholds] = [0,0,0]# Find where image is both colored right and in the regionline_image[~color_thresholds & region_thresholds] = [255,0,0]
        # Display our two output imagesplt.imshow(color_select)plt.imshow(line_image)
        # uncomment if plot does not displayplt.show()


        圖片



        2.Canny邊緣檢測(cè)+霍夫變換



        顏色閾值+圖像掩模的方法雖然簡(jiǎn)單,但是只能應(yīng)對(duì)一些固定顏色車道線的場(chǎng)景。圖像像素受光照影響將是一個(gè)極其常見的問(wèn)題。


        canny邊緣檢測(cè)+霍夫變換是另外一種簡(jiǎn)單提取車道線的方法。首先依靠canny提取到原圖像的邊緣信息,再依靠霍夫變換提取滿足要求的直線


























































        import matplotlib.pyplot as pltimport matplotlib.image as mpimgimport numpy as npimport cv2

        # Read in and grayscale the imageimage = mpimg.imread('test.jpg')gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
        # Define a kernel size and apply Gaussian smoothingkernel_size = 5blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)
        # Define our parameters for Canny and applylow_threshold = 50high_threshold = 150edges = cv2.Canny(blur_gray, low_threshold, high_threshold)
        # Next we'll create a masked edges image using cv2.fillPoly()mask = np.zeros_like(edges)ignore_mask_color = 255
        # This time we are defining a four sided polygon to maskimshape = image.shapevertices = np.array([[(0,imshape[0]),(0, 0), (imshape[1], 0), (imshape[1],imshape[0])]], dtype=np.int32)  # all image# vertices = np.array([[(0,imshape[0]),(554, 460), (700, 446), (imshape[1],imshape[0])]], dtype=np.int32)  # defining a quadrilateral regioncv2.fillPoly(mask, vertices, ignore_mask_color)masked_edges = cv2.bitwise_and(edges, mask)
        # Define the Hough transform parameters# Make a blank the same size as our image to draw onrho = 1 # distance resolution in pixels of the Hough gridtheta = np.pi/180 # angular resolution in radians of the Hough gridthreshold = 1     # minimum number of votes (intersections in Hough grid cell)min_line_length = 5 #minimum number of pixels making up a linemax_line_gap = 1    # maximum gap in pixels between connectable line segmentsline_image = np.copy(image)*0 # creating a blank to draw lines on
        # Run Hough on edge detected image# Output "lines" is an array containing endpoints of detected line segmentslines = cv2.HoughLinesP(masked_edges, rho, theta, threshold, np.array([]),                            min_line_length, max_line_gap)
        # Iterate over the output "lines" and draw lines on a blank imagefor line in lines:    for x1,y1,x2,y2 in line:        cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),10)
        # Create a "color" binary image to combine with line imagecolor_edges = np.dstack((edges, edges, edges))
        # Draw the lines on the edge imagelines_edges = cv2.addWeighted(color_edges, 0.8, line_image, 1, 0)plt.imshow(lines_edges)plt.show()


        canny邊緣后,進(jìn)行霍夫直線檢測(cè)的結(jié)果


        圖片


        在此基礎(chǔ)上,增加一個(gè)四邊形的圖像掩模的結(jié)果


        四邊形的設(shè)定,寫在了代碼中,只是進(jìn)行了注釋


        圖片


        總結(jié):


        以上兩種方法只適合簡(jiǎn)單的demo,顯然并不能識(shí)別具備一定曲率的車道線,也無(wú)法適應(yīng)光照不同的情況。


        之后會(huì)介紹更多的識(shí)別車道線方法。


        *博客內(nèi)容為網(wǎng)友個(gè)人發(fā)布,僅代表博主個(gè)人觀點(diǎn),如有侵權(quán)請(qǐng)聯(lián)系工作人員刪除。



        關(guān)鍵詞: AI

        相關(guān)推薦

        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 龙游县| 平利县| 新乡市| 石渠县| 岗巴县| 淮南市| 开鲁县| 萝北县| 盐边县| 禄丰县| 台安县| 冷水江市| 黑龙江省| 孙吴县| 洞口县| 东莞市| 定南县| 新田县| 灌云县| 苏州市| 鄂托克旗| 若羌县| 临城县| 克拉玛依市| 电白县| 延寿县| 维西| 汤阴县| 紫云| 信阳市| 乌兰浩特市| 霍林郭勒市| 达拉特旗| 二连浩特市| 阳泉市| 文山县| 报价| 萍乡市| 呼玛县| 定陶县| 巫山县|