从零开始 OpenCV (三) —— 矩阵/图像的基本操作
从零开始 OpenCV (三) —— 矩阵/图像的基本操作
基本操作
“加” 操作: cvAdd()
, cvAddS()
, cvAddWeighted()
cvAdd
将mask
指定的元素相加;
cvAddS
将mask
指定的元素与标量value
相加;
cvAddWeighted
将mask
指定区域的元素按权重相加, 并加上gamma
;
函数定义
// dst(mask) = src1(mask) + src2(mask)
// mask specifies elements of destination array to be changed.
void cvAdd(const CvArr* src1, const CvArr* src2, CvArr* dst, const CvArr* mask = NULL);
// dst(mask) = src(mask) + value
void cvAddS(const CvArr* src, const CvScalar value, CvArr* dst, const CvArr* mask = NULL);
// dst = src1 * alpha + src2 * beta + gamma
void cvAddWeighted(const CvArr* src1, double alpha, const CvArr* src2, double beta, double gamma, CvArr* dst);
举个例子: 选择两张图片的感兴趣区域, 按照一定的比例相叠加。
#include "opencv/highgui.h"
#include "opencv/cv.h"
int main()
{
// load image from file
IplImage* src1 = cvLoadImage("C:\\Users\\yanglion\\Pictures\\Img\\Ali.jpg");
IplImage* src2 = cvLoadImage("C:\\Users\\yanglion\\Pictures\\Img\\SpongeBob.JPG");
IplImage* dst = cvLoadImage("C:\\Users\\yanglion\\Pictures\\Img\\Ali.jpg");
// set image's ROI
cvSetImageROI(src1, cvRect(217, 116, 100, 80));
cvSetImageROI(src2, cvRect(106, 58, 100, 80));
cvSetImageROI(dst, cvRect(217, 116, 100, 80));
// dst = src1 * alpha + src2 * beta + gamma
double alpha = 0.4;
double beta = 1 - alpha;
double gamma = 0.0;
cvAddWeighted(src1, alpha, src2, beta, gamma, dst);
// Resets the image ROI to include the entire image and releases the ROI structure.
cvResetImageROI(src1);
cvResetImageROI(src2);
cvResetImageROI(dst);
// create window
cvNamedWindow("Ali");
cvNamedWindow("SpongeBob");
cvNamedWindow("AliWithSpongeBobFace");
// display image within window (highgui windows remember their content)
cvShowImage("Ali", src1);
cvShowImage("SpongeBob", src2);
cvShowImage("AliWithSpongeBobFace", dst);
// wait for key event infinitely (delay<=0) or for "delay" milliseconds
cvWaitKey();
return 0;
}
运行效果:
“减” 操作: cvSub()
, cvSubS()
, cvSubRS()
类似于操作,
cvSub
将src1
和src2
中对应的元素进行减法运算;
cvSubS
将src
中的每个元素依次减去标量value
的值;
cvSubRS
则是将标量value
的值减去src
中的每个元素的值;
// dst(mask) = src1(mask) - src2(mask)
void cvSub(const CvArr* src1, const CvArr* src2, CvArr* dst, const CvArr* mask = NULL);
// dst(mask) = src(mask) - value;
void cvSubS(const CvArr* src, CvScalar value, CvArr* dst, const CvArr* mask = NULL);
// dst(mask) = value - src(mask)
void cvSubRS(const CvArr* src, CvScalar value, CvArr* dst, const CvArr* mask = NULL);
其他基本操作
差值的绝对值: cvAbs()
, cvAbsDiff()
, cvAbsDiffS()
两个矩阵的元素依次求差: cvDiv()
两个矩阵的元素依次求积: cvMul()
按位与操作: cvAnd()
, cvAndS()
按位非: cvNot()
按位或: cvOr()
, cvOrS()
按位异或: cvXor()
, cvXorS()
统计操作
求和, 平均值, 标准差, 协方差: cvSum()
, cvAvg()
, cvAvgSdv()
, cvCalcCovarMatrix()
// Finds sum of array elements
CvScalar cvSum(const CvArr* arr);
// Calculates mean value of array elements
CvScalar cvAvg(const CvArr* arr, const CvArr*mask = NULL);
// Calculates mean and standard deviation of pixel values
void cvAvgSdv(const CvArr* arr, CvScalar* mean, CvScalar* std_dev, const CvArr* mask = NULL);
// Calculates covariation matrix for a set of vectors
void cvCalcCovarMatrix(const CvArr** vects, int count, CvArr* cov_mat, CvArr* avg, int flags);
统计非0的个数: cvCountNonZero()
// Calculates number of non-zero pixels
int cvCountNonZero(const CvArr* arr);
获取最大值和最小值: cvMinMaxLoc()
// Finds global minimum, maximum and their positions
void cvMinMaxLoc(const CvArr* arr, double* min_val, double* max_val,
CvPoint* min_loc = NULL, CvPoint* max_loc = NULL, const CvArr* mask = NULL);
矩阵操作
计算矩阵的特征值和特征向量: cvEigenVV()
// Finds eigen values and vectors of a symmetric matrix
void cvEigenVV(CvArr* mat, CvArr* evects, CvArr* evals, double eps = 0, int lowindex = -1, int highindex = -1);
计算矩阵的逆矩阵, 转置和迹: cvInvert()
, cvTranspose()
, cvTrace()
// Inverts matrix
double cvInvert(const CvArr* src, CvArr* dst, int method = 0);
// Tranposes matrix. Square matrices can be transposed in-place
void cvTranspose(const CvArr* src, CvArr* dst);
// Calculates trace of the matrix (sum of elements on the main diagonal)
CvScalar cvTrace (const CvArr *mat);
计算矩阵的行列式: cvDet()
// Calculates determinant of input matrix
double cvDet(const CvArr* mat);
求线性方程组的解: cvSolve()
// Solves linear system (src1)*(dst) = (src2) (returns 0 if src1 is a singular and CV_LU method is used)
int cvSolve(const CvArr* src1, const CvArr* src2, CvArr* dst, int method = 0);
图像操作
将多通道图像切割成多个单通道数组: cvSplit()
// Splits a multi-channel array into the set of single-channel arrays or extracts particular [color] plane void cvSplit(const CvArr* src, CvArr* dst0, CvArr* dst1, CvArr* dst2, CvArr* dst3);
将多个单通道图像合并为一个多通道图像: cvMerge()
// Merges a set of single-channel arrays into the single multi-channel array
// or inserts one particular [color] plane to the array
void cvMerge (const CvArr *src0, const CvArr *src1, const CvArr *src2, const CvArr *src3, CvArr *dst);