下面是OPENCV用户手册之图像处理部分:梯度、边缘与角点(中文翻译),有错误欢迎指正,原文在:
http://www.assuredigit.com/incoming/sourcecode/opencv/chinese_docs/ref/opencvref_cv.htm
注意:
本章描述图像处理和分析的一些函数。大多数函数是针对二维数组的。所以我们用数组来描述“图像”,而图像不必是 IplImage,还可以是 CvMat's 或 CvMatND。
翻译:HUNNISH, 阿须数码
使用扩展 Sobel 算子计算一阶、二阶、三阶或混合图像差分
void cvSobel( const CvArr* src, CvArr* dst, int xorder, int yorder, int aperture_size=3 );
| -3 0 3| |-10 0 10| | -3 0 3|对 x-方向 以及转置矩阵对 y-方向。
函数 cvSobel 通过对图像用相应的内核进行卷积操作来计算图像差分:
dst(x,y) = dxorder+yodersrc/dxxorder•dyyorder |(x,y)
Sobel 算子结合 Gaussian 平滑和微分,以提高计算结果对噪声的抵抗能力。通常情况,函数调用采用如下参数 (xorder=1, yorder=0, aperture_size=3) 或 (xorder=0, yorder=1, aperture_size=3) 来计算一阶 x- 或 y- 方向的图像差分。第一种情况对应:
|-1 0 1| |-2 0 2| |-1 0 1|
核。第二种对应
|-1 -2 -1| | 0 0 0| | 1 2 1| or | 1 2 1| | 0 0 0| |-1 -2 -1|
核,它依赖于图像原点的定义 (origin
来自 IplImage
结构的定义)。不进行图像尺度变换。所以输出图像通常比输入图像大。为防止溢出,当输入图像是 8 位的,要求输出图像是 16 位的。产生的图像可以用函数 cvConvertScale 或 cvConvertScaleAbs 转换为 8 位的。除了 8-比特 图像,函数也接受 32-位 浮点数图像。所有输入和输出图像都必须是单通道,且图像大小或ROI尺寸一致。
计算图像的 Laplacian?
void cvLaplace( const CvArr* src, CvArr* dst, int aperture_size=3 );
函数 cvLaplace 计算输入图像的 Laplacian,方法是对用 sobel 算子计算的二阶 x- 和 y- 差分求和:
dst(x,y) = d2src/dx2 + d2src/dy2
对 aperture_size
=1 则给出最快计算结果,相当于对图像采用如下内核做卷积:
|0 1 0| |1 -4 1| |0 1 0|
类似于 cvSobel 函数,也不作图像的尺度变换,而且支持输入、输出图像类型一致。
采用 Canny 算法做边缘检测
void cvCanny( const CvArr* image, CvArr* edges, double threshold1, double threshold2, int aperture_size=3 );
函数 cvCanny 采用 CANNY 算法发现输入图像的边缘而且在输出图像中标识这些边缘。小的阈值 threshold1
用来控制边缘连接,大的阈值用来控制强边缘的初始分割。
计算特征图,用于角点检测
void cvPreCornerDetect( const CvArr* image, CvArr* corners, int aperture_size=3 );
函数 cvPreCornerDetect 计算函数 Dx2Dyy+Dy2Dxx - 2DxDyDxy 其中 D? 表示一阶图像差分,D?? 表示二阶图像差分。 角点被认为是函数的局部最大值:
// assuming that the image is 浮点数 IplImage* corners = cvCloneImage(image); IplImage* dilated_corners = cvCloneImage(image); IplImage* corner_mask = cvCreateImage( cvGetSize(image), 8, 1 ); cvPreCornerDetect( image, corners, 3 ); cvDilate( corners, dilated_corners, 0, 1 ); cvSubS( corners, dilated_corners, corners ); cvCmpS( corners, 0, corner_mask, CV_CMP_GE ); cvReleaseImage( &corners ); cvReleaseImage( &dilated_corners );
计算图像块的特征值和特征向量,用于角点检测
void cvCornerEigenValsAndVecs( const CvArr* image, CvArr* eigenvv, int block_size, int aperture_size=3 );
对每个象素,函数 cvCornerEigenValsAndVecs 考虑 block_size
× block_size
大小的邻域 S(p),然后在邻域上计算差分的相关矩阵:
| sumS(p)(dI/dx)2 sumS(p)(dI/dx•dI/dy)| M = | | | sumS(p)(dI/dx•dI/dy) sumS(p)(dI/dy)2 |
然后它计算矩阵的特征值和特征向量,并且按如下方式(λ1, λ2, x1, y1, x2, y2)存储这些值到输出图像中,其中
λ1, λ2 - M 的
特征值,没有排序
(x1, y1) - 特征向量,对 λ1
(x2, y2) - 特征向量,对 λ2
计算梯度矩阵的最小特征值,用于角点检测
void cvCornerMinEigenVal( const CvArr* image, CvArr* eigenval, int block_size, int aperture_size=3 );
函数 cvCornerMinEigenVal 与 cvCornerEigenValsAndVecs 类似,但是它仅仅计算和存储每个象素点差分相关矩阵的最小特征值,即前一个函数的 min(λ1, λ2)
精确角点位置
void cvFindCornerSubPix( const CvArr* image, CvPoint2D32f* corners, int count, CvSize win, CvSize zero_zone, CvTermCriteria criteria );
win
=(5,5) 那么使用 5*2+1 × 5*2+1 = 11 × 11 大小的搜索窗口
criteria
可以是最大迭代数目,也可以是精确度
函数 cvFindCornerSubPix 通过迭代来发现具有子象素精度的角点位置,或如图所示的放射鞍点(radial saddle points)。
Sub-pixel accurate corner locator is based on the observation that every vector from the center q
to a point p
located within a neighborhood of q
is orthogonal to the image gradient at p
subject to image and measurement noise. Consider the expression:
εi=DIpiT•(q-pi)
where DIpi
is the image gradient at the one of the points pi
in a neighborhood of q
. The value of q
is to be found such that εi
is minimized. A system of equations may be set up with εi
' set to zero:
sumi(DIpi•DIpiT)•q - sumi(DIpi•DIpiT•pi) = 0
where the gradients are summed within a neighborhood ("search window") of q
. Calling the first gradient term G
and the second gradient term b
gives:
q=G-1•b
The algorithm sets the center of the neighborhood window at this new center q
and then iterates until the center keeps within a set threshold.
确定图像的强角点
void cvGoodFeaturesToTrack( const CvArr* image, CvArr* eig_image, CvArr* temp_image, CvPoint2D32f* corners, int* corner_count, double quality_level, double min_distance, const CvArr* mask=NULL );
eig_image
一致
函数 cvGoodFeaturesToTrack 在图像中寻找具有大特征值的角点。该函数,首先用cvCornerMinEigenVal 计算输入图像的每一个象素点的最小特征值,并将结果存储到变量 eig_image 中。
然后进行非最大值压缩(仅保留3x3邻域中的局部最大值)。下一步将最小特征值小于 quality_level
•max(eig_image
(x,y)) 排除掉。最后,函数确保所有发现的角点之间具有足够的距离,(最强的角点第一个保留,然后检查新的角点与已有角点之间的距离大于 min_distance )。