本文根据opencv 轻松入门 面向python总结的,便于查找,每个函数都对应书里页码,可以看我语雀排版好的
https://www.yuque.com/nuanyanggaozhao/glvn9z/smagn3
retval = cv2.imread( filename [, flags])
None = cv2.imshow( 窗口名称,图像)
key = cv2.waitkey(0) #这个0,是表示一直停在界面无限等待
其中waitkey里的参数,不写就是键盘按什么返回什么
key == ord(’ A ') 可以用ord将键盘转换为ASCII码去跟返回的值比较
retval = cv2.imwrite( filename, img[, params])
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MsVEJMJK-1624751668875)(https://cdn.nlark.com/yuque/__latex/54c90174a75a910f64fbc38a15a1c01a.svg#card=math&code=a%20%2B%20b%20%3D%0A%5Cbegin%7Bcases%7D%0Aa%20%2B%20b%2C%20%20%26%20%5Ctext%7Ba%20%2B%20b%20%E2%89%A4%20255%7D%20%5C%5C%5B2ex%5D%0Amod%EF%BC%88a%20%2B%20b%2C%20256%EF%BC%89%2C%20%26%20%5Ctext%7Ba%20%0A%20%2B%20b%20%3E%20255%7D%0A%5Cend%7Bcases%7D&id=r7Bhg)]
两个加起来,超过255,那就是取255
dst = cv2.addweighted(src1, alpha, src2, beta, gamma)
图1,图1权重,图2,图2权重,混合图片亮度
cv2.bitwise_and() # 与
cv2.bitwise_or() #或
cv2.bitwise_xor() #按位异或
cv2.bitwise_not #按位取反
cv2.cvtColor( src, code [, dstCn])
dst = cv2.inRange( src, lowerb, upperb)
dst = cv2.resize( src, dsize[, fx[, fy[, interpolation]]])
# filpCode 翻转类型,x轴 0,/ y轴 正数 / xy同时,负数
dst = cv2.flip( src, filpCode)
dst = cv2.warpAffine( src, M, dsize[, flags[, borderMode[, borderValue]]])
可以用cv2.getRotationMatrix2D( center, angle, scale)去生成M
# M生成
retval = cv2.getAffineTransform( sec, dst)
dst = cv2.warpPerspective( src, M, dsize[, flags[, borderMode[, borderValue]]])
dst = cv2.remap( src, map1, map2, interpolation[, borderMode[, borderValue]])
此函数功能可以复制P119,翻转P121,xy轴互换P126
retval, dst = cv2.threshold( src, thresh, maxval, type)
dst = cv2.adaptiveThreshold( src, maxValue, adaptiveMethod, thresholdType, blockSize, c)
dst = cv2.blur( src, ksize, anchor, borderType)
dst = cv2.boxFilter( src, ddepth, ksize, anchor, normalize, borderType)
dst = cv2.GaussianBlur( src, ksize, sigmaX, sigmaY, borderType)
# 去噪效果很好
dst = cv2.medianBlur( src, ksize)
dst = cv2.bilateralFilter( src, d, sigmaColor, sigmaSpace, borderType)
dst = cv2.filter2D( src, ddepth, kernel, anchor, delta, borderType)
dst = cv2.erode( srrc, kernel[, anchor[, iterations[, borderType[, borderValue]]]])
dst = cv2.dilate( src, kernel[, anchor[, iterations[, borderType[, borderValue]]]])
dst = cv2.morphologyEx( src, op, kernel[, anchor[, iterations[, borderType[, borderValue]]]])
retval = cv2.getStructuringElement( shape, ksize[, anchor])
dst = cv2.Soble( src, ddepth, dx, dy[, ksize[, delta[, borderType]]])
dst = cv2.Scharr( src, ddepth, dx, dy[, scale[, delta[, borderType]]])
dst = cv2.Laplacian( src, ddepth, dx, dy[, ksize[, delta[, borderType]]]))
1)使用高斯滤波器,以平滑图像,滤除噪声。
2)计算图像中每个像素点的梯度强度和方向。
3)应用非极大值(Non-Maximum Suppression)抑制,以消除边缘检测带来的杂散响应。
4)应用双阈值(Double-Threshold)检测来确定真实的和潜在的边缘。
5)通过抑制孤立的弱边缘最终完成边缘检测。
![](https://img-blog.csdnimg.cn/img_convert/106983926a2ce17c4982275b26df3dbc.png#from=url&id=wQ3SI&margin=[object Object]&originHeight=238&originWidth=752&originalType=binary&ratio=1&status=done&style=none)
# 一层下一层,宽高为原来一半
dst = cv2.pyrDown( src[, dstsize[, borderType]])
# 一层上一层,宽高为原来2倍
dst = cv2.pyrDown( src[, dstsize[, borderType]])
为了恢复高分辨率的图像
image, contours, hierarchy = cv2.findContours( image, mode, method)
image = cv2.drawContours( image,contours, contourIdx, color[, thickness[, lineType]])
retval = cv2.moments( array[, binaryImage])
retval = cv2.contourArea( contour [, oriented])
retval = cv2.arcLength ( contour, closed)
hu = cv2.HuMoments( m )
retval = cv2.matchShapes( contour1, contour2, method, parameter)
retval = cv2.boundingRect( array )
retval = cv2.minAreaRect( points )
center, radius = cv2.minEnclosingCircle( points )
retval = cv2.fitEllipse( points )
line = cv2.fitLine( points, disType, param, reps, aeps)
retval, triangle = cv2.minEnclosingTriangle( points )
approxCurve = cv2.approxPolyDP( curve, epsilon, closed)
hull = cv2.convexHull( points[, clockwise[, returmnPoints]])
convexityDefects = cv2.convexityDefects( contour, convexhull)
retval = cv2.isContourConvex( contour )
retval = cv2.pointPolygonTest( contour, pt, measureDist)
leftmost = typle( cnt[cnt[:, :, 0].argmin()][0])
rightmost = typle( cnt[cnt[:, :, 0].argmax()][0])
topmost = typle( cnt[cnt[:, :, 1].argmin()][0])
bottommost = typle( cnt[cnt[:, :, 1].argmax()][0])