一、矩阵
Mat I,img,I1,I2,dst,A,B;
double k,alpha;
Scalar s;
1.加法
I=I1+I2;
add(I1,I2,dst,mask,dtype);
scaleAdd(I1,scale,I2,dst);
2.减法
absdiff(I1,I2,I);
A-B;A-s;s-A;-A;
subtract(I1,I2,dst);
3.乘法
I=I.mul(I);
Mat C=A.mul(5/B);
A*B;矩阵相乘
I=alpha*I;
Mat::cross(Mat);
double Mat::dot(Mat);
mul-------multiply
pow(src,double p,dst);
4.除法
divide(I1,I2,dst,scale,int dtype=-1);
A/B;alpha/A;都是点除
5.转换
I.convertTo(I1,CV_32F);
A.t();
flip(I,dst,int flipCode);
sqrt(I,dst);
cvtColor(I,dst,int code,int dstCn=0);
resize:对图像进行形变
--------------------------------------------------------------------------
6.其他
Scalar s=sum(I);各通道求和
norm,countNonZero,trace,determinant,repeat都是返回Mat或者Scalar
countNonZero:用来统计非零的向量个数.(rows*cols个)
Scalar m=mean(I);
Mat RowClone=C.row(1).clone();
addWeight(I1,alpha,I2,beta,gamma,dst,int dtype=-1);
----------------------------------------------------------------------------
7.运算符
log10()
exp(I,dst);
log(I,dst);
randu(I,Scalar::all(0),Scalar::all(255));
Mat::t()转置
Mat::inv(int method=DECOMP_LU)求逆。method=DECOMP_CHOLESKY(专门用于对称,速度是LU的2倍),DECOMP_SVD
invert(I1,dst,int method=DECOMP_LU);
MatExpr abs(Mat)
A cmpop B;A compop alpha;alpha cmpop A;这里cmpop表示>,>=,==,!=,<=,<等,结果是CV_8UC1的mask的0或255
按位运算:A logicop B;A logicop s;s logicop A;~A;这里logicop代表&,|,^
bitwise_not(I,dst,mask);
还有bitwise_and,bitwise_or,bitwise_xor,
min(A,B);min(A,alpha);max(A,B);max(A,alpha);都返回MatExpr,返回的dst和A的类型一样
double determinant(Mat);
bool eigen(I1,dst,int lowindex=-1,int highindex=-1);
bool eigen(I1,dst,I,int...);
minMaxLoc(I1,&minVal,&maxVal,Point *minLoc=0,Point* MaxLoc=0,mask);
-----------------------------------------------------------
-------------------
8.初始化
Mat I(img,Rect(10,10,100,100));
Mat I=img(Range:all(),Range(1,3));
Mat I=img.clone();
img.copyTo(I);
Mat I(2,2,CV_8UC3,Scalar(0,0,255));
Mat E=Mat::eye(4,4,CV_64F);
Mat O=Mat::ones(2,2,CV_32F);
Mat Z=Mat::zeros(3,3,CV_8UC1);
Mat C=(Mat_<double>(2,2)<<0,-1,2,3);
Mat::row(i);Mat::row(j);Mat::rowRange(start,end);Mat::colRange(start,end);都只是创建个头
Mat::diag(int d);d=0是是主对角线,d=1是比主低的对角线,d=-1....
static Mat Mat::diag(const Mat& matD)
Mat::setTo(Scalar &s);以s初始化矩阵
Mat::push_back(Mat);在原来的Mat的最后一行后再加几行
Mat::pop_back(size_t nelems=1);
-------------------------------------------------------------------------------
9.矩阵读取和修改
(1)1个通道:
for(int i=0;i<I.rows;++i)
for(int j=0;j<I.cols;++j)
I.at<uchar>(i,j)=k;
(2)3个通道:
Mat_<Vec3b> _I=I;
for(int i=0;i<I.rows;++i)
for(int j=0;j<I.cols;++j)
{
_I(i,j)[0]=b;
_I(i,j)[1]=g;
_I(i,j)[2]=r;
}
I=_I;
------------------------------------------------------------
或者直接用I.at<Vec3b>(i,j)[0]....
-------------------------------------------------
float *s;
for(i=0;i<dealImg.rows;i++)
{s=proImg.ptr<float>(i);
for(j=0;j<dealImg.cols;j++)
{a1=s[3*j+1]-m1;
a2=s[3*j+2]-m2;}}
-------------------------------------------------------------------------
(3)其他机制
I.rows(0).setTo(Scalar(0));
saturate_cast<uchar>(...);
Mat::total();返回一共的元素数量
size_t Mat::elemSize();返回元素的大小:CV_16SC3-->3*sizeof(short)-->6
size_t Mat::elemSize1();返回元素一个通道的大小CV_16SC3-->sizeof(short)-->2
int Mat::type()返回他的类型CV_16SC3之类
int Mat::depth()返回深度:CV_16SC3-->CV_16S
int Mat::channels()返回通道数
size_t Mat:step1()返回一个被elemSize1()除以过的step
Size Mat::size()返回Size(cols,rows);如果大于2维,则返回(-1,-1),都是先宽再高的
bool Mat::empty()如果没有元素返回1,即Mat::total()==0或者Mat::data==NULL
uchar *Mat::ptr(int i=0)指向第i行
Mat::at(int i)(int i,int j)(Point pt)(int i,int j,int k)
RNG随机类:next,float RNG::uniform(float a,float b);..
double RNG::gaussian(double sigma);
RNG::fill(I,int distType,Mat low,Mat up);
randu(I,low,high);
randn(I,Mat mean,Mat stddev);
reduce(I,dst,int dim,int reduceOp,int dtype=-1);
setIdentity(dst,Scalar &value=Scalar(1));
--------------------------------------------------------------
10.较复杂运算
gemm(I1,I2,alpha,I3,beta,dst,int flags=0);
mulTransposed(I,dst,bool aTa,Mat delta=noArray(),double scale=1,int rtype=-1);
calcCovarMatrix(Mat,int,Mat,Mat,int,int=);calcCovarMatrix(Mat I,Mat covar,Mat mean,int flags,int=);
cartToPolar
compare(I1,I2,dst,cmpop);cmpop=CMP_EQ,CMP_GT,CMP_GE,CMP_LT,CMP_LE,COM_NE
completeSymm(M,bool lowerToUpper=false);当lowerToUpper=true时Mij=Mji(i<j);当为flase时,Mij=Mji(i>j)
变成可显示图像:convertScaleAbs(I,dst,alpha,beta);dst=saturate_cast<uchar>(|alpha*I+beta|);
dct(I,dst,int flags=0);
idct,dft,idft
inRange(I1,I_low,I_up,dst);
Mahalanobis(vec1,vec2,covar);
merge(vector<Mat>,Mat);
double norm(...):当src2木有时,norm可以计算出最长向量、向量距离和、向量距离和的算术平方根
solveCubic解3次方程,solvePoly解n次方程
排列:sort,sortIdx
mixChannels();对某个通道进行各种传递
-----------------------------------------------------------------
11.未懂的函数
getConvertElem,extractImageCOI,LUT
magnitude(x,y,dst);
meanStdDev,
MulSpectrums(I1,I2,dst,flags);傅里叶
normalize(I,dst,alpha,beta,int normType=NORM_L2,int rtype=-1,mask);
PCA,SVD,solve,transform,transpose
二、其他数据结构
Point2f P(5,1);
Point3f P3f(2,6,7);
vector<float> v;v.push_back((float)CV_PI);v.push_back(2);v.push_back(3.01f);
vector<Point2f> vPoints(20);
三、常用方法
Mat mask=src<0;这样很快建立一个mask了
四、以后可能用到的函数
randShuffle,repeat