免费的行情软件网站不下载疫苗最新官方消息
2026/4/17 0:43:56 网站建设 项目流程
免费的行情软件网站不下载,疫苗最新官方消息,wordpress 个人 模板,网站建设关键字优化文章目录0 前言1 车道线检测2 目标3 检测思路4 代码实现4.1 视频图像加载4.2 车道线区域4.3 区域4.4 canny 边缘检测4.5 霍夫变换(Hough transform)4.6 HoughLinesP 检测原理4.6.1 定义显示车道线方法4.6.2 查看探测车道线数据结构4.6.3 探测车道线4.6.4 合成4.6.5 优化0 前言 …文章目录0 前言1 车道线检测2 目标3 检测思路4 代码实现4.1 视频图像加载4.2 车道线区域4.3 区域4.4 canny 边缘检测4.5 霍夫变换(Hough transform)4.6 HoughLinesP 检测原理4.6.1 定义显示车道线方法4.6.2 查看探测车道线数据结构4.6.3 探测车道线4.6.4 合成4.6.5 优化0 前言无人驾驶技术是机器学习为主的一门前沿领域在无人驾驶领域中机器学习的各种算法随处可见今天学长给大家介绍无人驾驶技术中的车道线检测。选题指导, 项目分享见文末1 车道线检测在无人驾驶领域每一个任务都是相当复杂看上去无从下手。那么面对这样极其复杂问题我们解决问题方式从先尝试简化问题然后由简入难一步一步尝试来一个一个地解决问题。车道线检测在无人驾驶中应该算是比较简单的任务依赖计算机视觉一些相关技术通过读取 camera 传入的图像数据进行分析识别出车道线位置我想这个对于 lidar 可能是无能为力。所以今天我们就从最简单任务说起看看有哪些技术可以帮助我们检出车道线。我们先把问题简化所谓简化问题就是用一些条件限制来缩小车道线检测的问题。我们先看数据也就是输入算法是车辆行驶的图像输出车道线位置。更多时候我们如何处理一件比较困难任务可能有时候我们拿到任务时还没有任何思路不要着急也不用想太多我们先开始一步一步地做从最简单的开始做起随着做就会有思路同样一些问题也会暴露出来。我们先找一段视频这段视频是我从网上一个关于车道线检测项目中拿到的也参考他的思路来做这件事。好现在就开始做这件事那么最简单的事就是先读取视频然后将其显示在屏幕以便于调试。2 目标检测图像中车道线位置将车道线信息提供路径规划。3 检测思路图像灰度处理图像高斯平滑处理canny 边缘检测区域 Mask霍夫变换绘制车道线4 代码实现4.1 视频图像加载importcv2importnumpyasnpimportsysimportpygamefrompygame.localsimport*classDisplay(object):def__init__(self,Width,Height):pygame.init()pygame.display.set_caption(Drive Video)self.screenpygame.display.set_mode((Width,Height),0,32)defpaint(self,draw):self.screen.fill([0,0,0])drawcv2.transpose(draw)drawpygame.surfarray.make_surface(draw)self.screen.blit(draw,(0,0))pygame.display.update()if__name____main__:solid_white_right_video_pathtest_videos/丹成学长车道线检测.mp4capcv2.VideoCapture(solid_white_right_video_path)Widthint(cap.get(cv2.CAP_PROP_FRAME_WIDTH))Heightint(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))displayDisplay(Width,Height)whileTrue:ret,drawcap.read()drawcv2.cvtColor(draw,cv2.COLOR_BGR2RGB)ifretFalse:breakdisplay.paint(draw)foreventinpygame.event.get():ifevent.typeQUIT:sys.exit()上面代码学长就不多说了默认大家对 python 是有所了解关于如何使用 opencv 读取图片网上代码示例也很多大家一看就懂。这里因为我用的是 mac 有时候显示视频图像可能会有些问题所以我们用 pygame 来显示 opencv 读取图像。这个大家根据自己实际情况而定吧。值得说一句的是 opencv 读取图像是 BGR 格式要想在 pygame 中正确显示图像就需要将 BGR 转换为 RGB 格式。4.2 车道线区域现在这个区域是我们根据观测图像绘制出来defcolor_select(img,red_threshold200,green_threshold200,blue_threshold200):ysize,xsizeimg.shape[:2]color_selectnp.copy(img)rgb_threshold[red_threshold,green_threshold,blue_threshold]thresholds(img[:,:,0]rgb_threshold[0])\|(img[:,:,1]rgb_threshold[1])\|(img[:,:,2]rgb_threshold[2])color_select[thresholds][0,0,0]returncolor_select效果如下4.3 区域我们要检测车道线位置相对比较固定通常出现车的前方所以我们通过绘制也就是仅检测我们关心区域。通过创建 mask 来过滤掉那些不关心的区域保留关心区域。4.4 canny 边缘检测有关边缘检测也是计算机视觉。首先利用梯度变化来检测图像中的边如何识别图像的梯度变化呢答案是卷积核。卷积核是就是不连续的像素上找到梯度变化较大位置。我们知道 sobal 核可以很好检测边缘那么 canny 就是 sobal 核检测上进行优化。# 示例代码作者丹成学长Q746876041defcanny_edge_detect(img):graycv2.cvtColor(img,cv2.COLOR_RGB2GRAY)kernel_size5blur_graycv2.GaussianBlur(gray,(kernel_size,kernel_size),0)low_threshold180high_threshold240edgescv2.Canny(blur_gray,low_threshold,high_threshold)returnedges4.5 霍夫变换(Hough transform)霍夫变换是将 x 和 y 坐标系中的线映射表示在霍夫空间的点(m,b)。所以霍夫变换实际上一种由繁到简(类似降维)的操作。当使用 canny 进行边缘检测后图像可以交给霍夫变换进行简单图形(线、圆)等的识别。这里用霍夫变换在 canny 边缘检测结果中寻找直线。# 示例代码作者丹成学长Q746876041masknp.zeros_like(edges)ignore_mask_color255# 获取图片尺寸imshapeimg.shape# 定义 mask 顶点verticesnp.array([[(0,imshape[0]),(450,290),(490,290),(imshape[1],imshape[0])]],dtypenp.int32)# 使用 fillpoly 来绘制 maskcv2.fillPoly(mask,vertices,ignore_mask_color)masked_edgescv2.bitwise_and(edges,mask)# 定义Hough 变换的参数rho1thetanp.pi/180threshold2min_line_length4# 组成一条线的最小像素数max_line_gap5# 可连接线段之间的最大像素间距# 创建一个用于绘制车道线的图片line_imagenp.copy(img)*0# 对于 canny 边缘检测结果应用 Hough 变换# 输出“线”是一个数组其中包含检测到的线段的端点linescv2.HoughLinesP(masked_edges,rho,theta,threshold,np.array([]),min_line_length,max_line_gap)# 遍历“线”的数组来在 line_image 上绘制forlineinlines:forx1,y1,x2,y2inline:cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),10)color_edgesnp.dstack((edges,edges,edges))importmathimportcv2importnumpyasnp Gray Scale Gaussian Smoothing Canny Edge Detection Region Masking Hough Transform Draw Lines [Mark Lane Lines with different Color] classSimpleLaneLineDetector(object):def__init__(self):passdefdetect(self,img):# 图像灰度处理gray_imgself.grayscale(img)print(gray_img)#图像高斯平滑处理smoothed_imgself.gaussian_blur(imggray_img,kernel_size5)#canny 边缘检测canny_imgself.canny(imgsmoothed_img,low_threshold180,high_threshold240)#区域 Maskmasked_imgself.region_of_interest(imgcanny_img,verticesself.get_vertices(img))#霍夫变换houghed_linesself.hough_lines(imgmasked_img,rho1,thetanp.pi/180,threshold20,min_line_len20,max_line_gap180)# 绘制车道线outputself.weighted_img(imghoughed_lines,initial_imgimg,alpha0.8,beta1.,gamma0.)returnoutputdefgrayscale(self,img):returncv2.cvtColor(img,cv2.COLOR_RGB2GRAY)defcanny(self,img,low_threshold,high_threshold):returncv2.Canny(img,low_threshold,high_threshold)defgaussian_blur(self,img,kernel_size):returncv2.GaussianBlur(img,(kernel_size,kernel_size),0)defregion_of_interest(self,img,vertices):masknp.zeros_like(img)iflen(img.shape)2:channel_countimg.shape[2]ignore_mask_color(255,)*channel_countelse:ignore_mask_color255cv2.fillPoly(mask,vertices,ignore_mask_color)masked_imagecv2.bitwise_and(img,mask)returnmasked_imagedefdraw_lines(self,img,lines,color[255,0,0],thickness10):forlineinlines:forx1,y1,x2,y2inline:cv2.line(img,(x1,y1),(x2,y2),color,thickness)defslope_lines(self,image,lines):imgimage.copy()poly_vertices[]order[0,1,3,2]left_lines[]right_lines[]forlineinlines:forx1,y1,x2,y2inline:ifx1x2:passelse:m(y2-y1)/(x2-x1)cy1-m*x1ifm0:left_lines.append((m,c))elifm0:right_lines.append((m,c))left_linenp.mean(left_lines,axis0)right_linenp.mean(right_lines,axis0)forslope,interceptin[left_line,right_line]:rows,colsimage.shape[:2]y1int(rows)y2int(rows*0.6)x1int((y1-intercept)/slope)x2int((y2-intercept)/slope)poly_vertices.append((x1,y1))poly_vertices.append((x2,y2))self.draw_lines(img,np.array([[[x1,y1,x2,y2]]]))poly_vertices[poly_vertices[i]foriinorder]cv2.fillPoly(img,ptsnp.array([poly_vertices],int32),color(0,255,0))returncv2.addWeighted(image,0.7,img,0.4,0.)defhough_lines(self,img,rho,theta,threshold,min_line_len,max_line_gap):linescv2.HoughLinesP(img,rho,theta,threshold,np.array([]),minLineLengthmin_line_len,maxLineGapmax_line_gap)line_imgnp.zeros((img.shape[0],img.shape[1],3),dtypenp.uint8)line_imgself.slope_lines(line_img,lines)returnline_imgdefweighted_img(self,img,initial_img,alpha0.1,beta1.,gamma0.):lines_edgescv2.addWeighted(initial_img,alpha,img,beta,gamma)returnlines_edgesdefget_vertices(self,image):rows,colsimage.shape[:2]bottom_left[cols*0.15,rows]top_left[cols*0.45,rows*0.6]bottom_right[cols*0.95,rows]top_right[cols*0.55,rows*0.6]vernp.array([[bottom_left,top_left,top_right,bottom_right]],dtypenp.int32)returnver4.6 HoughLinesP 检测原理接下来进入代码环节学长详细给大家解释一下 HoughLinesP 参数的含义以及如何使用。linescv2.HoughLinesP(cropped_image,2,np.pi/180,100,np.array([]),minLineLength40,maxLineGap5)第一参数是我们要检查的图片 Hough accumulator 数组第二个和第三个参数用于定义我们 Hough 坐标如何划分 bin也就是小格的精度。我们通过曲线穿过 bin 格子来进行投票我们根据投票数量来决定 p 和 theta 的值。2 表示我们小格宽度以像素为单位 。我们可以通过下图划分小格只要曲线穿过就会对小格进行投票我们记录投票数量记录最多的作为参数如果定义尺寸过大也就失去精度如果定义格子尺寸过小虽然精度上来了这样也会打来增长计算时间。接下来参数 100 表示我们投票为 100 以上的线才是符合要求是我们要找的线。也就是在 bin 小格子需要有 100 以上线相交于此才是我们要找的参数。minLineLength 给 40 表示我们检查线长度不能小于 40 pixelmaxLineGap5 作为线间断不能大于 5 pixel4.6.1 定义显示车道线方法defdisply_lines(image,lines):pass通过定义函数将找到的车道线显示出来。line_imagedisply_lines(lane_image,lines)4.6.2 查看探测车道线数据结构defdisply_lines(image,lines):line_imagenp.zeros_like(image)iflinesisnotNone:forlineinlines:print(line)先定义一个尺寸大小和原图一样的矩阵用于绘制查找到车道线我们先判断一下是否已经找到车道线lines 返回值应该不为 None 是一个矩阵我们可以简单地打印一下看一下效果[[704418927641]][[704426791516]][[320703445494]][[585301663381]][[630341670383]]4.6.3 探测车道线看数据结构[[x1,y1,x2,y2]] 的二维数组这就需要我们转换一下为一维数据[x1,y1,x2,y2]defdisply_lines(image,lines):line_imagenp.zeros_like(image)iflinessisnotNone:forlineinlines:x1,y1,x2,y2line.reshape(4)cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),10)returnline_image line_imagedisply_lines(lane_image,lines)cv2.imshow(result,line_image)4.6.4 合成有关合成图片我们是将两张图片通过给一定权重进行叠加合成。4.6.5 优化探测到的车道线还是不够平滑我们需要优化基本思路就是对这些直线的斜率和截距取平均值然后将所有探测出点绘制到一条直线上。defaverage_slope_intercept(image,lines):left_fit[]right_fit[]forlineinlines:x1,y1,x2,y2line.reshape(4)parametersnp.polyfit((x1,x2),(y1,y2),1)print(parameters)这里学长定义两个数组 left_fit 和 right_fit 分别用于存放左右两侧车道线的点我们打印一下 lines 的斜率和截距通过 numpy 提供 polyfit 方法输入两个点我们就可以得到通过这些点的直线的斜率和截距。[1.-286.][1.03448276-302.27586207][-1.6721238.04][1.02564103-299.[1.02564103-299.defaverage_slope_intercept(image,lines):left_fit[]right_fit[]forlineinlines:x1,y1,x2,y2line.reshape(4)parametersnp.polyfit((x1,x2),(y1,y2),1)# print(parameters)slopeparameters[0]interceptparameters[1]ifslope0:left_fit.append((slope,intercept))else:right_fit.append((slope,intercept))print(left_fit)print(right_fit)我们输出一下图片大小我们图片是以其左上角作为原点 0 0 来开始计算的所以我们直线从图片底部 700 多向上绘制我们无需绘制全部可以截距一部分即可。defmake_coordinates(image,line_parameters):slope,interceptline_parameters y1image.shape[0]y2int(y1*(3/5))x1int((y1-intercept)/slope)x2int((y2-intercept)/slope)# print(image.shape)returnnp.array([x1,y1,x2,y2])所以直线开始和终止我们给定 y1,y2 然后通过方程的斜率和截距根据y 算出 x。averaged_linesaverage_slope_intercept(lane_image,lines);line_imagedisply_lines(lane_image,averaged_lines)combo_imagecv2.addWeighted(lane_image,0.8,line_image,1,1,1)cv2.imshow(result,combo_image) 项目分享:大家可自取用于参考学习获取方式见文末!

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询