Python Opencv 计算几何相关 API
Opencv 中有很多常用的计算几何 API,比如轮廓,连通性,最小外接矩形,多边形填充,多边形内外判断等。这些函数很实用,但参数较多,使用时经常需要现在网上查找。因此,为提高效率,写了这篇文章进行总结。
连通性
对于一个 8-bit 单通道图片获取每个像素点的连通性
def connectedComponents(image, labels=None, connectivity=None, ltype=None): # real signature unknown; restored from __doc__
"""
connectedComponents(image[, labels[, connectivity[, ltype]]]) -> retval, labels
. @overload
.
. @param image the 8-bit single-channel image to be labeled
. @param labels destination labeled image
. @param connectivity 8 or 4 for 8-way or 4-way connectivity respectively
. @param ltype output image label type. Currently CV_32S and CV_16U are supported.
"""
pass
输入参数说明:
- image:传入的待确认连通性的单通道图像
- labels:返回的连通性标记矩阵,通常不传入,通过返回值获得
- connectivity:进行 4 领域判断还是 8 领域判断
- ltype:指定输出 labels 的数据类型,支持 CV_32S 和 CV_16U
输出参数说明:
- retval:存在的连通区域个数
- labels:连通性标记矩阵,从 0 开始标记,同一个 id 说明连通
def connectedComponentsWithStats(image, labels=None, stats=None, centroids=None, connectivity=None, ltype=None): # real signature unknown; restored from __doc__
"""
connectedComponentsWithStats(image[, labels[, stats[, centroids[, connectivity[, ltype]]]]]) -> retval, labels, stats, centroids
. @overload
. @param image the 8-bit single-channel image to be labeled
. @param labels destination labeled image
. @param stats statistics output for each label, including the background label, see below for
. available statistics. Statistics are accessed via stats(label, COLUMN) where COLUMN is one of
. #ConnectedComponentsTypes. The data type is CV_32S.
. @param centroids centroid output for each label, including the background label. Centroids are
. accessed via centroids(label, 0) for x and centroids(label, 1) for y. The data type CV_64F.
. @param connectivity 8 or 4 for 8-way or 4-way connectivity respectively
. @param ltype output image label type. Currently CV_32S and CV_16U are supported.
"""
pass
这个函数是对上面函数的扩充,增加了一些连通区域的特征输出,其中增加的部分
- stats:连通区域的 bounding box 特征和面积,为一个 retval * 5 的矩阵。第一行是 labels == 0 的连通域属性,前 4 列是连通域的 bounding box 的 (left, top, width, height),第 5 列是该连通域面积。其他行依次类推
- centroids:每个连通区域的几何中心店,为 retval * 2 的矩阵。第一行是 labels == 0 的连通区域的几何中心,其他行依次类推。
多边形填充
def fillPoly(img, pts, color, lineType=None, shift=None, offset=None): # real signature unknown; restored from __doc__
"""
fillPoly(img, pts, color[, lineType[, shift[, offset]]]) -> img
. @brief Fills the area bounded by one or more polygons.
.
. The function cv::fillPoly fills an area bounded by several polygonal contours. The function can fill
. complex areas, for example, areas with holes, contours with self-intersections (some of their
. parts), and so forth.
.
. @param img Image.
. @param pts Array of polygons where each polygon is represented as an array of points.
. @param color Polygon color.
. @param lineType Type of the polygon boundaries. See #LineTypes
. @param shift Number of fractional bits in the vertex coordinates.
. @param offset Optional offset of all points of the contours.
"""
pass
参数说明:
- img:原始输入图像,在此基础上进行绘制
- pts:依照顺时针或者逆时针的顺序依次排列的一个数组,比如 [p1, p2, p3, p4]
- color:填充多边形的颜色
- lineType:多边形边缘线型
- shift:顶点坐标的缩放系数
- offset:顶点坐标的偏移向量,比如 (x, y)
def fillConvexPoly(img, points, color, lineType=None, shift=None): # real signature unknown; restored from __doc__
"""
fillConvexPoly(img, points, color[, lineType[, shift]]) -> img
. @brief Fills a convex polygon.
.
. The function cv::fillConvexPoly draws a filled convex polygon. This function is much faster than the
. function #fillPoly . It can fill not only convex polygons but any monotonic polygon without
. self-intersections, that is, a polygon whose contour intersects every horizontal line (scan line)
. twice at the most (though, its top-most and/or the bottom edge could be horizontal).
.
. @param img Image.
. @param points Polygon vertices.
. @param color Polygon color.
. @param lineType Type of the polygon boundaries. See #LineTypes
. @param shift Number of fractional bits in the vertex coordinates.
"""
pass
这个 API 与 fillPoly() 类似,唯一的区别是这个 API 只能绘制凸多边形。
轮廓
Contours 相关的 API 可以参考这篇:https://blog.csdn.net/a40850273/article/details/88063478
TODO