数据类型(版本4.6.0)
CV_8U:占8位的unsigned
CV_8UC(n):占8位的unsigned char
CV_8UC1:占8位的unsigned char 一通道
CV_8UC2:占8位的unsigned char 二通道
CV_8UC3:占8位的unsigned char 三通道
CV_8UC4:占8位的unsigned char 四通道
CV_8S:占8位的signed
CV_8SC(n):占8位的signed char
CV_8SC1:占8位的signed char 一通道
CV_8SC2:占8位的signed char 二通道
CV_8SC3:占8位的signed char 三通道
CV_8SC4:占8位的signed char 四通道
CV_16U:占16位的unsigned
CV_16UC(n):占16位的unsigned char
CV_16UC1:占16位的unsigned char 一通道
CV_16U2:占16位的unsigned char 二通道
CV_16U3:占16位的unsigned char 三通道
CV_16U4:占16位的unsigned char 四通道
CV_16S:占16位的signed
CV_16SC(n):占16位的signed char
CV_16SC1:占16位的signed char 一通道
CV_16SC2:占16位的signed char 二通道
CV_16SC3:占16位的signed char 三通道
CV_16SC4:占16位的signed char 四通道
CV_16F:占16位的float
CV_16FC(n):占16位的float char
CV_16FC1:占16位的float char 一通道
CV_16FC2:占16位的float char 二通道
CV_16FC3:占16位的float char 三通道
CV_16FC4:占16位的float char 四通道
CV_32S:占32位的signed
CV_32SC(n):占32位的signed char
CV_32SC1:占32位的signed char 一通道
CV_32SC2:占32位的signed char 二通道
CV_32SC3:占32位的signed char 三通道
CV_32SC4:占32位的signed char 四通道
CV_32F:占32位的float
CV_32FC(n):占32位的float char
CV_32FC1:占32位的float char 一通道
CV_32FC2:占32位的float char 二通道
CV_32FC3:占32位的float char 三通道
CV_32FC4:占23位的float char 四通道
CV_64F:占64位的float
CV_64FC(n):占64位的float char
CV_64FC1:占64位的float char 一通道
CV_64FC2:占64位的float char 二通道
CV_64FC3:占64位的float char 三通道
CV_64FC4:占64位的float char 四通道
矩阵基本操作
全零矩阵
rows:行数
cols:列数
type:数据类型(CV_16F)
size:Size(宽(列数),高(行数))
- Size与Mat中的成员函数.size()的返回值,有相同的数据类型,是[宽*高]。
- Mat中的成员变量.size,与以上二者不同,是 rows*cols
全一矩阵
rows:行数
cols:列数
type:数据类型(CV_16F)
size:Size(宽(列数),高(行数))
单位矩阵
rows:行数
cols:列数
type:数据类型(CV_16F)
size:Size(宽(列数),高(行数))
矩阵转置
求逆矩阵
逗号式分隔创建矩阵,常用于自定义卷积核
以下为使用实例
注意:给出的数据类型必须是基本数据类型,如int,double。不能是CV_16F等。
矩阵定义(只列出常用的)
rows:行数
cols:列数
type:数据类型(CV_16F)
size:Size(宽(列数),高(行数))
数据类型Scalar
- Scalar(gray)
- Scalar(blue,green,red)
通过ptr与at函数遍历矩阵
Vec类型
以下为实例
- 用ptr访问可以不加Vec类型
- 用at访问必须加Vec类型
通过迭代器遍历矩阵(recommended)
图像基本操作
图片读取
创建窗口
winname(window name):窗体名
图片显示
winname(window name):窗体名
若窗体未创建,会自动进行创建
控制图片的展示时间,如设置delay=0,则表示一直展示,按SPACE停止展示
如设置delay不为0,则表示停留delay毫秒
图片保存
filename:保存的文件名
视频输出
filename
影片档案名称(例如video.avi)
图片序列(例如img_%02d.jpg,将读取像这样的样本img_00.jpg, img_01.jpg, img_02.jpg, …)
视频流的网址(例如protocol://host:port/script_name?script_params|auth)。请注意,每个视频流或IP摄像机源均具有其自己的URL方案。请参考源流的文档以了解正确的URL。
index
要打开的视频捕获设备的ID。要使用默认后端打开默认摄像头,只需传递0。
当apiPreference为CAP_ANY时,使用camera_id + domain_offset(CAP_ *)向后兼容有效。
apiPreference(not important)
首选使用的Capture API后端。如果有多个可用的读取器实现,则可以用于实施特定的读取器实现。
设置读取的摄像头编号,默认CAP_ANY=0,自动检测摄像头。多个摄像头时,使用索引0,1,2,…进行编号调用摄像头。 apiPreference = -1时单独出现窗口,选取相应编号摄像头。
演示
通道分离与合并
分离
API(一)
src(source):输入图像。
mvbegin(mat vector begin):分离后的Mat数组。
API(二)
m(mat):输入图像。
mv(mat vector):分离后的的Mat数组,可以使用STL容器vector。
合并
API(一)
mv(mat vector):欲合并的图像数组。
count:欲合并的图像的个数
dst(destination):输出图片。
API(二)
mv(mat vector):欲合并的图像数组,可以使用STL容器vector。
dst(destination):输出图片。
图片色彩模式转换
code:转换码
转换类型和转换码
- RGB和BGR(opencv默认的彩色图像的颜色空间是BGR)颜色空间的转换
cv::COLOR_BGR2RGB
cv::COLOR_RGB2BGR
cv::COLOR_RGBA2BGRA
cv::COLOR_BGRA2RGBA
- 向RGB和BGR图像中增添alpha通道
cv::COLOR_RGB2RGBA
cv::COLOR_BGR2BGRA
- 从RGB和BGR图像中去除alpha通道
cv::COLOR_RGBA2RGB
cv::COLOR_BGRA2BGR
- 从RBG和BGR颜色空间转换到灰度空间
cv::COLOR_RGB2GRAY
cv::COLOR_BGR2GRAY
cv::COLOR_RGBA2GRAY
cv::COLOR_BGRA2GRAY
- 从灰度空间转换到RGB和BGR颜色空间
cv::COLOR_GRAY2RGB
cv::COLOR_GRAY2BGR
cv::COLOR_GRAY2RGBA
cv::COLOR_GRAY2BGRA
- RGB和BGR颜色空间与BGR565颜色空间之间的转换
cv::COLOR_RGB2BGR565
cv::COLOR_BGR2BGR565
cv::COLOR_BGR5652RGB
cv::COLOR_BGR5652BGR
cv::COLOR_RGBA2BGR565
cv::COLOR_BGRA2BGR565
cv::COLOR_BGR5652RGBA
cv::COLOR_BGR5652BGRA
- 灰度空间与BGR565之间的转换
cv::COLOR_GRAY2BGR555
cv::COLOR_BGR5552GRAY
- RGB和BGR颜色空间与CIE XYZ之间的转换
cv::COLOR_RGB2XYZ
cv::COLOR_BGR2XYZ
cv::COLOR_XYZ2RGB
cv::COLOR_XYZ2BGR
- RGB和BGR颜色空间与uma色度(YCrCb空间)之间的转换
cv::COLOR_RGB2YCrCb
cv::COLOR_BGR2YCrCb
cv::COLOR_YCrCb2RGB
cv::COLOR_YCrCb2BGR
- RGB和BGR颜色空间与HSV颜色空间之间的相互转换
cv::COLOR_RGB2HSV
cv::COLOR_BGR2HSV
cv::COLOR_HSV2RGB
cv::COLOR_HSV2BGR
- RGB和BGR颜色空间与HLS颜色空间之间的相互转换
cv::COLOR_RGB2HLS
cv::COLOR_BGR2HLS
cv::COLOR_HLS2RGB
cv::COLOR_HLS2BGR
- RGB和BGR颜色空间与CIE Lab颜色空间之间的相互转换
cv::COLOR_RGB2Lab
cv::COLOR_BGR2Lab
cv::COLOR_Lab2RGB
cv::COLOR_Lab2BGR
- RGB和BGR颜色空间与CIE Luv颜色空间之间的相互转换
cv::COLOR_RGB2Luv
cv::COLOR_BGR2Luv
cv::COLOR_Luv2RGB
cv::COLOR_Luv2BGR
- Bayer格式(raw data)向RGB或BGR颜色空间的转换
cv::COLOR_BayerBG2RGB
cv::COLOR_BayerGB2RGB
cv::COLOR_BayerRG2RGB
cv::COLOR_BayerGR2RGB
cv::COLOR_BayerBG2BGR
cv::COLOR_BayerGB2BGR
cv::COLOR_BayerRG2BGR
cv::COLOR_BayerGR2BGR
改变图片的对比度和亮度
概述
a:控制对比度增益
b:控制亮度增益
手动(使用saturate_cast函数确保输出值不溢出范围)
调用API:Mat::convertTo
可以看到效果是一样的
图片混合
src(source1):输入图片1
alpha:src1的权重
src2(source2):输入图片2
beta:src2的权重
gamma:额外的增量
dst(destination):输出图片
dtype(destination type):输出图片的数据类型,-1表示与输入图片一致
图片尺寸调整
src(source):输入图片
dst(destination):输出图片
dsize(destination size):输出图片的尺寸
fx:x方向(width方向)的缩放比例,如果它是0,那么它就会按照(double)dsize.width/src.cols来计算;
fy:y方向(height方向)的缩放比例,如果它是0,那么它就会按照(double)dsize.height/src.rows来计算;
interpolation:插值算法的选择
插值算法(not important)
注意事项
使用注意事项:
- dsize和fx/fy不能同时为0
- 指定dsize的值,让fx和fy空置直接使用默认值。
- 让dsize为0,指定好fx和fy的值,比如fx=fy=0.5,那么就相当于把原图两个方向缩小一倍。
图像金字塔(常用于神经网络的池化层,对图像进行成倍的放大或缩小)
src(source):输入图片
dst(destination):输出图片
dstsize(destination size):输出图片的尺寸,默认自动调整
borderType:边界填充方式,默认为黑边。如果没有设置dstsize,则不会出现黑边,因为已经进行了自动调整
src(source):输入图片
dst(destination):输出图片
dstsize(destination size):输出图片的尺寸,默认自动调整
borderType:边界填充方式,默认为黑边。如果没有设置dstsize,则不会出现黑边,因为已经进行了自动调整
二值化(一般只对灰度图使用)
src(source):输入图片
dst(destination):输出图片
thresh(threshold):阈值
maxval(max value):最大值
type:阈值类型
阈值类型
1. 阈值二值化(Threshold Binary)
首先指定像素的灰度值的阈值,遍历图像中像素值,如果像素的灰度值大于这个阈值,则将这个像素设置为最大像素值(8位灰度值最大为255);若像素的灰度值小于阈值,则将该像素点像素值赋值为0。公式以及示意图如下:
2. 阈值反二值化(Threshold Binary Inverted)
首先也要指定一个阈值,不同的是在对图像进行阈值化操作时与阈值二值化相反,当像素的灰度值超过这个阈值的时候为该像素点赋值为0;当该像素的灰度值低于该阈值时赋值为最大值。公式及示意图如下:
3. 截断(Truncate)
给定像素值阈值,在图像中像素的灰度值大于该阈值的像素点被设置为该阈值,而小于该阈值的像素值保持不变。公式以及示意图如下:
4. 阈值取零(Threshold To Zero)
与截断阈值化相反,像素点的灰度值如果大于该阈值则像素值不变,如果像素点的灰度值小于该阈值,则该像素值设置为0.公式以及示意图如下:
5. 阈值反取零(Threshold To Zero Inverted)
像素值大于阈值的像素赋值为0,而小于该阈值的像素值则保持不变,公式以及示意图如下:
图片裁剪
方式一
以下为实例
方式二
以下为实例
Rect类构造
基本变换
翻转
src(source):输入图片
dst(destination):输出图片
flipCode:翻转类型
效果
90°旋转
src(source):输入图片
dst(destination):输出图片
rotateCode:旋转类型
效果
仿射变换与透射变换
src(source):输入图片
dst(destination):输出图片
M:变换矩阵
dsize(destination size):输出图片的尺寸,若不对输出图片的尺寸进行调整,那么很可能会出现黑边
flags:插值算法
borderMode:边界外推法
borderValue:填充边界的值
平移
只需将变换矩阵M设置成如下形式:
delta_x:x方向上的偏移量
delta_y:y方向上的偏移量
M_values:必须是浮点类型的数组对象
M:必须是CV_32F,不能用逗号式分隔创建
效果
任意角度旋转
获得变换矩阵M
center:旋转中心点的坐标
angle:逆时针偏角
scale:生成图与原图之比
效果
仿射(不破坏几何关系)
获得变换矩阵M
src[](source[]):输入图片的坐标点集,含三个坐标点
dst[](destination[]):三个坐标点变换的目标位置
透射(破坏几何关系)
已知变换后图片,逆推变换矩阵M
src(source):输入图片
dst(destination):输出图片
获得变换矩阵M
src[](source[]):输入图片的坐标点集,含四个坐标点
dst[](destination[]):四个坐标点变换的目标位置
滤波及边缘检测
均值滤波
卷积核形状
API
src(source):输入图片
dst(destination):输出图片
ksize(kernal size):卷积核大小,必须是正奇数
anchor:滤波器中心像素位置,取(-1,-1)表示几何中心
borderType:边界填充方式,默认为黑边
效果
高斯滤波
卷积核形状
二维高斯函数表述为:
对应图形:
代码实现(不区分sigmaX与sigmaY)
API
src(source):输入图片
dst(destination):输出图片
ksize(kernal size):卷积核大小。如果这个值我们设其为非正数,那么OpenCV会从第五个参数sigmaSpace来计算出它来。
sigmaX:x方向上的标准差
sigmaY:y方向上的标准差。默认输入量为0,则将其设置为等于sigmaX,如果两个轴的标准差均为0,则根据输入的高斯滤波器尺寸计算标准偏差。
borderType:边界填充方式,默认为黑边
效果
中值滤波
原理
取滤波器内的中值作为输出,可以很好的抑制椒盐噪声
API
src(source):输入图片
dst(destination):输出图片
ksize(kernal size):卷积核边长,必须是正奇数
效果
高斯双边滤波
原理
双边滤波器的好处是可以做边缘保存(edge preserving),一般用高斯滤波去降噪,会较明显地模糊边缘,对于高频细节的保护效果并不明显。双边滤波器顾名思义比高斯滤波多了一个高斯方差sigma-d,它是基于空间分布的高斯滤波函数,所以在边缘附近,离的较远的像素不会太多影响到边缘上的像素值,这样就保证了边缘附近像素值的保存。但是由于保存了过多的高频信息,对于彩色图像里的高频噪声,双边滤波器不能够干净的滤掉,只能够对于低频信息进行较好的滤波。
API
src(source):输入图片
dst(destination):输出图片
d:卷积核边长。如果这个值我们设其为非正数,那么OpenCV会从第五个参数sigmaSpace来计算出它来。
sigmaColor:颜色空间滤波器的sigma值。这个参数的值越大,就表明该像素邻域内有更宽广的颜色会被混合到一起,产生较大的半相等颜色区域。
sigmaSpace:坐标空间中滤波器的sigma值,坐标空间的标注方差。他的数值越大,意味着越远的像素会相互影响,从而使更大的区域足够相似的颜色获取相同的颜色。当d>0,卷积核大小已被指定且与sigmaSpace无关。否则,d正比于sigmaSpace。
borderType:边界填充方式,默认为黑边
效果
获取用来形态学操作的滤波器
shape:滤波器形状
ksize(kernal size):滤波器大小
anchor:滤波器中心像素位置,取(-1,-1)表示几何中心
腐蚀和膨胀(一般对二值图)
原理
腐蚀:取滤波器内的最小值作为输出
膨胀:取滤波器内的最大值作为输出
腐蚀API
src(source):输入图片
dst(destination):输出图片
kernal:滤波器矩阵
anchor:滤波器中心像素位置,取(-1,-1)表示几何中心
iterations:执行erode函数的次数,默认执行一次
borderType:边界填充方式
borderValue:填充边界的值
效果
膨胀API
src(source):输入图片
dst(destination):输出图片
kernal:滤波器矩阵
anchor:滤波器中心像素位置,取(-1,-1)表示几何中心
iterations:执行erode函数的次数
borderType:边界填充方式
borderValue:填充边界的值
效果
形态学操作(一般对二值图)
API
src(source):输入图片
dst(destination):输出图片
op:变换类型
kernal:滤波器矩阵
anchor:滤波器中心像素位置,取(-1,-1)表示几何中心
iterations:执行erode函数的次数
borderType:边界填充方式
borderValue:填充边界的值
变换类型
开
原理
对输入图片先进行腐蚀,然后进行膨胀。可以用来屏蔽与滤波器大小相当的亮部。
效果
闭
原理
对输入图片先进行膨胀,然后进行腐蚀。可以用来屏蔽与滤波器大小相当的暗部。
效果
顶帽
原理
对输入图片先进行开操作,然后原图-开操作图。可以用来提取与滤波器大小相当的亮部。
效果
黑帽
原理
对输入图片先进行闭操作,然后闭操作图-原图。可以用来提取与滤波器大小相当的暗部。
效果
形态学梯度
原理
膨胀图与腐蚀图之差。可以用来提取边界轮廓,但提取效果比不上专业的边缘检测算法。
效果
击中击不中变换
原理
击中击不中变换由下面三步构成:
用结构元素B1来腐蚀输入图像
用结构元素B2来腐蚀输入图像的补集
前两步结果的与运算
结构元素B1和B2可以结合为一个元素B。例如:
结构元素:左B1(击中元素),中B2(击不中元素),右B(两者结合)
本例中,我们寻找这样一种结构模式,中间像素属于背景,其上下左右属于前景,其余领域像素忽略不计(背景为黑色,前景为白色)。然后用上面的核在输入图像中找这种结构。从下面的输出图像中可以看到,输入图像中只有一个位置满足要求。
输入二值图像
输出二值图像
选择合适的边缘检测输出图像的深度
参照以下表格
int sdepth | int ddepth |
CV_8U | CV_16S/CV_32F/CV_64F |
CV_16U/CV_16S | CV_32F/CV_64F |
CV_32F | CV_32F/CV_64F |
CV_64F | CV_64F |
convertScaleAbs恢复深度为CV_8U
src(source):输入图片
dst(destination):输出图片
sobel
卷积核形状(ksize=3)
API
src(source):输入图片
dst(destination):输出图片
ddepth(destination depth):输出图片的深度(CV_16F)
dx:x方向导数的阶数,一般取1
dy:y方向导数的阶数,一般取1
ksize:卷积核边长,默认为3
scale:生成图与原图的缩放比例,默认为1
delta:额外的增量,默认为0
borderType:边界填充方式,默认为黑边
流程
- 用cvtColor函数转灰度图
- 在x,y方向上分别各调用一次Sobel
- 用convertScaleAbs函数转换到原格式,否则无法显示
- 用addWeighted函数把两张输出图片加在一起
同时在x,y方向上调用Sobel和分开调用的效果对比
可以看到效果差了很多
scharr
卷积核形状(ksize恒定为3)
虽然Sobel算子可以有效的提取图像边缘,但是对图像中较弱的边缘提取效果较差。因此为了能够有效的提取出较弱的边缘,需要将像素值间的差距增大,因此引入Scharr算子。Scharr算子是对Sobel算子差异性的增强,因此两者之间的在检测图像边缘的原理和使用方式上相同。
API
src(source):输入图片
dst(destination):输出图片
ddepth(destination depth):输出图片的深度(CV_16F)
dx:x方向导数的阶数,一般取1
dy:y方向导数的阶数,一般取1
scale:生成图与原图的缩放比例,默认为1
delta:额外的增量,默认为0
borderType:边界填充方式,默认为黑边
流程
- 用cvtColor函数转灰度图
- 在x,y方向上分别各调用一次Scharr
- 用convertScaleAbs函数转换到原格式,否则无法显示
- 用addWeighted函数把两张输出图片加在一起
Laplacian
卷积核形状(ksize=3)
Laplacian算子的卷积核形状决定了它对噪声非常敏感,因此,通常需要通过滤波平滑处理。
API
src(source):输入图片
dst(destination):输出图片
ddepth(destination depth):输出图片的深度(CV_16F)
scale:生成图与原图的缩放比例,默认为1
delta:额外的增量,默认为0
borderType:边界填充方式,默认为黑边
流程
- 用中值滤波等操作平滑处理
- 用cvtColor函数转灰度图
- 用Laplacian函数处理
- 用convertScaleAbs函数转换到原格式,否则无法显示
Canny(recommended)
API
image:输入图片
edges:输出图片
threshold1:最小阈值
threshold2:最大阈值
高于threshold2被认为是真边界,低于threshold1被抛弃,介于二者之间,则取决于是否与真边界相连。
apertureSize:Sobel卷积核的大小,默认为3。核越大,对噪声越不敏感,但是边缘检测的错误也会随之增加。
L2gradient:计算图像梯度幅度的标识,默认为false,表示L1范数(直接将两个方向的导数的绝对值相加)。如果使用true,表示L2范数(两个方向的导数的平方和再开方)
流程
- 用中值滤波等操作平滑处理
- 用Canny函数处理
效果
画几何图形
直线
API
img(image):输入图片
pt1(point1):端点1
pt2(point2):端点2
color:颜色
thickness:粗细
lineType:连通类型
shift:坐标点小数点位数(not important)
连通类型
- LINE_4与LINE_8差别不大,而LINE_AA的抗锯齿效果显著
正矩形
API
img(image):输入图片
pt1(point1):左上角端点
pt2(point2):右下角端点
color:颜色
thickness:粗细
lineType:连通类型
shift:坐标点小数点位数(not important)
img(image):输入图片
rec(rect):一个矩形
color:颜色
thickness:粗细
lineType:连通类型
shift:坐标点小数点位数(not important)
圆形
API
img(image):输入图片
center:圆心坐标
radius:半径
color:颜色
thickness:粗细。若取负值,则表示进行填充
lineType:连通类型
shift:坐标点小数点位数(not important)
椭圆
API
img(image):输入图片
center:圆心坐标
axes:(x方向上半轴长,y方向上半轴长)
angle:顺时针偏角
startAngle:以x方向上的半轴为起点,偏移一定角度后的起点,从此起点开始画椭圆
endAngle:以x方向上的半轴为起点,偏移一定角度后的终点,到此为止结束画椭圆
确定起点和终点后,顺时针方向画
color:颜色
thickness:粗细。若取负值,则表示进行填充
lineType:连通类型
shift:坐标点小数点位数(not important)
效果
斜矩形
API(通过RotatedRect类和line函数实现)
下面是自定义的一个快捷画斜矩形的函数
Trackbar控件
createTrackbar创建滚动条
API
trackbarname:滚动条名字
winname(window name):窗体名字。要先用nameWindow创建好同名窗体,滚动条才会出现
value:欲控制的变量的地址
count:欲控制的变量的最大值(最小为0)
onChange:回调函数,默认为空。如果想要传入,那么其参数是固定的。
userdata:万能指针,默认为空。如果想要传入,通常用一个类的对象的地址。
getTrackbarPos获得滚动条当前的值
trackbarname:滚动条名字
winname(window name):窗体名字
使用方式一(recommended)
原理
不使用createTrackbar函数的参数value、onChange、userdata参数。通过while(1)的无限循环,在循环中不断地用getTrackbarPos函数动态地获取滚动条的值,然后在循环内部用这些值进行操作。
效果
使用方式二
原理
不使用getTrackbarPos函数,使用createTrackbar的全部参数,在onChange回调函数中完成所有操作,由于回调函数的参数表是固定的,因此需要userdata传入所需数据。在每次移动滚动条时,相当于调用了一次回调函数,就完成了操作。结尾没有waitKey(0)就显示不了多久。
效果
轮廓检测(一般对二值图)
查找轮廓
API
image:输入图片
contours:保存输出轮廓的点坐标。通常用vector<vector<Point>>数据类型担任,通过Point可以看出存储的是坐标。
hierarchy:可选参数,保存输出轮廓的层级关系。通常用vector<Vec4i>数据类型担任。
hierarchy[i][0]:第i个轮廓的同一层级后一个轮廓的索引编号。
hierarchy[i][1]:第i个轮廓的同一层级前一个轮廓的索引编号。
hierarchy[i][2]:第i个轮廓的子轮廓的索引编号。
hierarchy[i][3]:第i个轮廓的父轮廓的索引编号。
如果当前轮廓没有对应的后一个轮廓、前一个轮廓、父轮廓或内嵌轮廓的话,则hierarchy[i][0] ~hierarchy[i][3]的相应位被设置为默认值-1。
mode:轮廓层级的检测模式。
method:轮廓坐标点的储存方式
offset:额外偏移量,在每一个检测出的轮廓点上加上该偏移量,可以是负值。当所分析图像是另外一个图像的ROI的时候,通过加减这个偏移量,可以把ROI图像的检测结果投影到原始图像对应位置上。
轮廓层级检测模式:索引号(层级)
RETR_EXTERNAL(索引顺序:从右下到左上)
只检测最外围轮廓,包含在外围轮廓内的内围轮廓被忽略
RETR_LIST(recommended)(索引顺序:从右下到左上,由外到内)
检测所有的轮廓,包括内围、外围轮廓,但是检测到的轮廓不建立层级关系,这就意味着这个检索模式下不存在父轮廓或内嵌轮廓,所以hierarch[i]向量内所有元素的第3、第4个分量都会被置为-1。
RETR_CCOMP(not recommended)(索引顺序:由内到外,从右下到左上)
检测所有的轮廓,但所有轮廓只建立两个等级关系,外围为顶层,若外围内的内围轮廓还包含了其他的轮廓信息,则内围内的所有轮廓均归属于顶层
RETR_TREE(recommended)
检测所有轮廓,所有轮廓建立一个等级树结构。外层轮廓包含内层轮廓,内层轮廓还可以继续包含内嵌轮廓。
轮廓坐标点储存方式
CHAIN_APPROX_NONE:保存物体边界上所有连续的轮廓点到contours向量内
CHAIN_APPROX_SIMPLE(recommended):仅保存轮廓的拐点信息,把所有轮廓拐点处的点保存入contours向量内,拐点与拐点之间直线段上的信息点不予保留,效率比较高。
CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS:使用tehChinl chain 近似算法(not important)
绘制轮廓
API
image:输入图片
contours:输入轮廓数组
contourIdx(contour index):欲绘制的轮廓的索引值,输入-1可以绘制所有轮廓
color:绘制颜色
thickness:线条粗细,默认为1。输入-1则表示填充。
lineType:连通类型。
hierarchy:可选的层次结构信息。它仅在当你需要绘制一些轮廓线时被使用。(详见参数maxLevel)默认为noArray(),返回一个空数组。
maxLevel:绘制轮廓线的最高级别。此参数仅在参数hierarchy有效时被考虑。
- 如果为0,只有被指定的轮廓被绘制。
- 如果为1,绘制被指定的轮廓和其下一级轮廓。
- 如果为2,绘制被指定的轮廓和其所有子轮廓。
offset:额外偏移量。
轮廓面积和周长
面积(非原地算法)
contour:某一个轮廓,数据类型vector<Point>
oriented: 有方向的区域标志。(not important)
- true: 此函数依赖轮廓的方向(顺时针或逆时针)返回一个已标记区域的值。
- false: 默认值。意味着返回不带方向的绝对值。
此函数利用格林公式计算轮廓的面积。对于具有自交点的轮廓,该函数几乎肯定会给出错误的结果。
周长(非原地算法)
curve:某一个轮廓,数据类型vector<Point>
closed:轮廓是否是闭合的。
多边形逼近
curve:某一个轮廓,数据类型vector<Point>
approxCurve:输出多边形的点集,数据类型vector<Point>
epsilon:设置精度,越小则精度越高,多边形越趋近于曲线,拟合效果更好但效率低。
closed:轮廓是否是闭合的。
凸包
points:输入点集
hull:输出凸包。数据类型取决于returnPoints,vector<Point>或vector<int>
clockwise:拟合凸包的直线的转动方向,TRUE为顺时针,否则为逆时针。
returnPoints:若为true,则在hull中存储点的坐标。若为false,则在hull中存储点的索引,索引值根据参数points得到。默认为true
外接矩形
最小外接矩形(返回RotatedRect)
points:输入点集
最大外界矩形(返回Rect)
points:输入点集
由于换了markdown编辑器,后续内容请点击链接
后续不在此处更新了