无人驾驶工程师学习笔记(三)——Canny边缘检测

Throughout this Nanodegree Program, we will be using Python with OpenCV for computer vision work. OpenCV stands for Open-Source Computer Vision.
主页:https://opencv.org/

canny算法

边缘检测:识别图像中物体的边界,是opencv中的一种算法。

  1. 首先转成灰度图像
  2. 计算图像的梯度值,每一个点的亮度对应着该点的梯度值,通过寻找最大的梯度值就可以找到边缘
  3. 然后根据边缘形状检测对象
    canny函数:edges=cv2,Canny(gray,low_threshold,high_threshold)
    gray图像名,edges输出的边缘图像名,其余两个参数为上下阈值,决定了边缘被检测到需要的强度(相邻像素间值的差异程度即梯度)。
    在这里插入图片描述在这里插入图片描述导数越大,梯度越大,因为是二维图像所以需要对xy都求导。即梯度计算,通过这个计算可以得到一些较粗的边缘线。然后通过canny算法可以仅保留梯度值最大的像素点,并细化边缘线。然后通过包含梯度强度更低的一些像素点,我们会再次扩展这些高强度边缘线的宽度。这些低强度像素的阈值是我们在调用canny时设定的。

The canny algorithm will first detect strong edge (strong gradient) pixels above the high_threshold, and reject pixels below the low_threshold. Next, pixels with values between the low_threshold and high_threshold will be included as long as they are connected to strong edges. The output edges is a binary image with white pixels tracing out the detected edges and black everywhere else. See the OpenCV Canny Docs for more details.

在进行边缘检测之前,我们需要进行高斯滤波,which is essentially a way of suppressing noise and spurious gradients by averaging (check out the OpenCV docs for GaussianBlur).

霍夫变换

可以在下面的网站中理解Python中使用霍夫变换(https://alyssaq.github.io/2014/understanding-hough-transform/
图像空间的一条直线及时霍夫空间的一个点。

在这里插入图片描述而对于图像空间中的点则对应霍夫空间中的直线(A选项)
在这里插入图片描述那么在图像空间中由两个点组成的直线则对应霍夫空间中由两条直线相交得到的点(c选项)。
在这里插入图片描述但是对于垂直和水平直线,m或b的值为无穷大,无法用这种方式表示,但是可以在图像空间中使用极坐标,图像空间中的每个点在霍夫空间中变换为一条正弦曲线,如下:
在这里插入图片描述在这里插入图片描述

HoughLinesP

lines = cv2.HoughLinesP(masked_edges, rho, theta, threshold, np.array([]),min_line_length, max_line_gap)

masked_edges:the output from canny

lines: the outputfrom HoughLinesP, be an array containing the endpoints (x1, y1, x2, y2) of all line segments detected by the transform operation

rho and theta : are the distance and angular resolution of our grid in Hough space. Remember that, in Hough space, we have a grid laid out along the (Θ, ρ) axis. You need to specify rho in units of pixels and theta in units of radians.
rho>=1; theta>=1 degree (pi/180 in radians)

The threshold parameter specifies the minimum number of votes a candidate line needs to have to make it into the output.

The empty np.array([]) is just a placeholder, no need to change it.

min_line_length is the minimum length of a line (in pixels) that you will accept in the output.

max_line_gap is the maximum distance (again, in pixels) between segments that you will allow to be connected into a single line.

eg. I chose parameters for my Hough space grid to be a rho of 2 pixels and theta of 1 degree (pi/180 radians). I chose a threshold of 15, meaning at least 15 points in image space need to be associated with each line segment. I imposed a min_line_length of 40 pixels, and max_line_gap of 20 pixels.

参数取值is a big hill ,you can get help in this blog:Finding the right parameters for your Computer Vision algorithm

numpy.zeros_like

numpy.zeros_like(a, dtype=None, order=‘K’, subok=True)[source]

Return an array of zeros with the same shape and type as a given array.

eg. mask = np.zeros_like(edges)

cv2.fillPoly()

cv2.fillPoly()函数可以用来填充任意形状的图型.可以用来绘制多边形,工作中也经常使用非常多个边来近似的画一条曲线.cv2.fillPoly()函数可以一次填充多个图型.

img = np.zeros((1080, 1920, 3), np.uint8)
area1 = np.array([[250, 200], [300, 100], [750, 800], [100, 1000]])
area2 = np.array([[1000, 200], [1500, 200], [1500, 400], [1000, 400]])
 
cv2.fillPoly(img, [area1, area2], (255, 255, 255))
 
plt.imshow(img)
plt.show()

在这里插入图片描述

cv2.bitwise_and() 函数

cv2.bitwise_and()是对二进制数据进行“与”操作,即对图像(灰度图像或彩色图像均可)每个像素值进行二进制“与”操作,1&1=1,1&0=0,0&1=0,0&0=0

OutputArray dst = cv2.bitwise_and(InputArray src1, InputArray src2, InputArray mask=noArray());//dst = src1 & src2

利用掩码(mask)进行“与”操作,即掩码图像白色区域是对需要处理图像像素的保留,黑色区域是对需要处理图像像素的剔除,其余按位操作原理类似只是效果不同而已。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值