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/
边缘检测:识别图像中物体的边界,是opencv中的一种算法。
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的值为无穷大,无法用这种方式表示,但是可以在图像空间中使用极坐标,图像空间中的每个点在霍夫空间中变换为一条正弦曲线,如下:
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(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()函数可以一次填充多个图型.
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()是对二进制数据进行“与”操作,即对图像(灰度图像或彩色图像均可)每个像素值进行二进制“与”操作,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)进行“与”操作,即掩码图像白色区域是对需要处理图像像素的保留,黑色区域是对需要处理图像像素的剔除,其余按位操作原理类似只是效果不同而已。