Related matrix operations in OpenCV

Source: Internet
Author: User

First, the matrix
Mat i,img,i1,i2,dst,a,b;
Double K,alpha;
Scalar s;
1. Addition
i=i1+i2;//equals Add (I1,i2,i);
Add (I1,i2,dst,mask,dtype);
Scaleadd (I1,SCALE,I2,DST);//dst=scale*i1+i2;
2. Subtraction
Absdiff (i1,i2,i);//i=| i1-i2|;
A-B; A-s;s-a;-a;
Subtract (I1,I2,DST);
3. Multiplication
I=i.mul (I);//Point multiplication, I.mul (i,3);-->i=3*i.^2
Mat C=a.mul (5/b);//==divide (a,b,c,5);
a*b; matrix multiplication
I=alpha*i;
Mat::cross (Mat);//cross-multiplication of a three-dimensional vector (or matrix), A.cross (B)
Double Mat::d ot (MAT),//2 the result of a vector (or matrix) point multiplication, A.dot (B)
Mul-------Multiply
Pow (src,double P,DST);//If P is an integer DST (i) =src (i) ^p; other |src (i) |^p
4. Division
Divide (I1,i2,dst,scale,int dtype=-1);//dst=saturate_cast (I1*SCALE/I2);
A/B;ALPHA/A, all of them.
5. Conversion
I.convertto (i1,cv_32f);//Type conversion
A.T ();//Transpose
Flip (I,dst,int flipcode);//flipcode=0 is flipped up and down, >0 when left and right, <0 together
sqrt (I,DST);
Cvtcolor (I,dst,int code,int dstcn=0);
Resize: Deformation of the image
--------------------------------------------------------------------------
6. Other
Scalar s=sum (I); sum of each channel
Norm,countnonzero,trace,determinant,repeat all back to mat or scalar.
Countnonzero: The number of vectors used to count non-zero. (Rows*cols)
Scalar M=mean (I);//averaging of each channel
Mat Rowclone=c.row (1). Clone ();//Copy Line 2nd
Addweight (I1,alpha,i2,beta,gamma,dst,int dtype=-1);//dst=saturate (Alpha*i1+beta*i2+gamma);d type is the depth of DST
----------------------------------------------------------------------------
7. Operators
LOG10 ()
exp (I,DST);//dst=exp (I); calculates the exponent of each array element
Log (I,DST);//if iij!=0; then Dstij=log (| iij|)
Randu (I,scalar::all (0), Scalar::all (255));
Mat::t () Transpose
MAT::INV (int method=decomp_lu) inversion. Method=decomp_cholesky (specifically for symmetry, twice times the speed of Lu), DECOMP_SVD//A.INV (); A.INV () *b;
Invert (i1,dst,int method=decomp_lu);//usage Ibid.
MATEXPR ABS (MAT)//Seek absolute value
A Cmpop B; A compop alpha;alpha cmpop A; here Cmpop means >,>=,==,!=,<=,< etc., the result is cv_8uc1 mask of 0 or 255
Bitwise operation: A LOGICOP B; A Logicop s;s Logicop a;~a; here Logicop stands for &,|,^
Bitwise_not (i,dst,mask);//inverts all queues
and Bitwise_and,bitwise_or,bitwise_xor,
Min (A, b), Min (A,alpha), Max (A, B), Max (A,alpha), all return matexpr, and the type of DST returned is the same as
Double determinant (Mat);//determinant
BOOL Eigen (I1,dst,int lowindex=-1,int highindex=-1);//
BOOL Eigen (I1,dst,i,int ...); /get eigenvectors for eigenvalues vector DST and corresponding eigenvalues
Minmaxloc (I1,&minval,&maxval,point *minloc=0,point* maxloc=0,mask);
Minloc is the smallest point of the 2D time-distance origin (not verified)
------------------------------------------------------------------------------
8. Initialize
Mat I (Img,rect (10,10,100,100));//initialize with a piece of place.
Mat i=img (Range:all (), Range (1,3));//all Rows, columns
Mat I=img.clone ();//Full copy
Img.copyto (I);//Transfer Matrix header
Mat I (2,2,cv_8uc3,scalar (0,0,255));//i=[0,0,255,0,0,255;0,0,255,0,0,255];
Mat E=mat::eye (4,4,cv_64f);//Diagonal matrix
Mat O=mat::ones (2,2,cv_32f);//Whole one matrix
Mat Z=mat::zeros (3,3,CV_8UC1);//Full 0 matrix
Mat c= (Mat_ (2,2) <<0,-1,2,3);//If the initialization of a simple matrix
Mat::row (i); Mat::row (j); Mat::rowrange (Start,end); Mat::colrange (start,end); just create a head.
Mat::d IAG (int d);d =0 is the main diagonal, d=1 is lower than the main diagonal, d=-1 ....
Static Mat Mat::d IAG (const mat& MATD)
Mat::setto (Scalar &s); initialize matrix with S
Mat::p Ush_back (MAT), add a few more lines after the last line of the original Mat
Mat::p op_back (size_t nelems=1);//move out the bottom lines
-------------------------------------------------------------------------------
9. Matrix Read and modify
(1) 1 channels:
for (int i=0;i
for (int j=0;j
i.at (i,j) =k;
(2) 3 channels://Here is particularly important, is the key to implement the relevant algorithm
Mat_ _i=i;//He has no 4 channels, only 3 channels!
for (int i=0;i
for (int j=0;j
{
_i (I,J) [0]=b;
_i (I,J) [1]=g;
_i (I,J) [2]=r;
}
I=_i;
------------------------------------------------------------
or directly with i.at (I,J) [0] ....
-------------------------------------------------
float *s;
for (i=0;i
{s=proimg.ptr (i);
for (j=0;j
{a1=s[3*j+1]-m1;
a2=s[3*j+2]-m2;}}
-------------------------------------------------------------------------
(3) Other mechanisms
i.rows (0). Setto (Scalar (0));//clear the first line 0
Saturate_cast (...); /can ensure that the content is 0~255 integer
mat::total (); Returns the total number of elements
size_t mat::elemsize (); Returns the size of the element: Cv_16sc3-->3*sizeof (short)-->6
size_t mat::elemsize1 (); Returns the element size of one channel cv_16sc3-->sizeof (short)-->2
int Mat::type () returns his type CV_16SC3 and the like
int Mat::d epth () return Depth: cv_16sc3-->cv_16s
int Mat::channels () returns the number of channels
size_t MAT:STEP1 () returns a step that is divided by elemSize1 ()
Size mat::size () returns a size (cols,rows), or returns ( -1,-1) if it is greater than 2 dimensions, and is the first width and height
BOOL Mat::empty () If no element returns 1, i.e. Mat::total () ==0 or Mat::d ata==null
Uchar *mat::p tr (int i=0) points to line I
Mat::at (int i) (int i,int j) (Point pt) (int i,int j,int k)
RNG Random class: Next,float rng::uniform (float a,float b);
Double Rng::gaussian (double sigma);
Rng::fill (I,int Disttype,mat low,mat up);//fill with random numbers
Randu (I,low,high);
Randn (I,mat mean,mat StdDev);
Reduce (i,dst,int dim,int reduceop,int dtype=-1);//You can count the maximum, minimum, average, and
Setidentity (Dst,scalar &value=scalar (1));//replace diagonal with value
Effect equivalent: Mat a=mat::eye (4,3,cv_32f);
--------------------------------------------------------------
10. More complex operations
Gemm (I1,i2,alpha,i3,beta,dst,int flags=0);//i1 is at least floating point, I2 with I1,flags for transpose
Gemm (i1,i2,alpha,i3,beta,dst,gemm_1_t,gemm_3_t);-->dst=alpha*i1.t () *i2+beta*i3.t (); Use this to completely replace this function
multransposed (I,dst,bool Ata,mat delta=noarray (), double scale=1,int rtype=-1);
I is 1 channels, and unlike Gemm, he can be used for any type.
If Ata=flase, dst=scale* (I-delta). T () * (I-delta);
If it is true,dst=scale* (I-delta) (I-delta). T ();
Calccovarmatrix (mat,int,mat,mat,int,int=); Calccovarmatrix (Mat i,mat covar,mat mean,int flags,int=);
carttopolar//go to polar coordinates
Compare (I1,i2,dst,cmpop); Cmpop=cmp_eq,cmp_gt,cmp_ge,cmp_lt,cmp_le,com_ne
Completesymm (M,bool lowertoupper=false); Mij=mji when Lowertoupper=true (IJ)
becomes a visible Image: Convertscaleabs (I,dst,alpha,beta);d st=saturate_cast (|alpha*i+beta|);
DCT (I,dst,int flags=0),//DCT transformation, 1-D, 2-D matrix; flags=dct_inverse,dct_rows
Idct,dft,idft
InRange (I1,I_LOW,I_UP,DST);//dst is CV_8UC1, between 2 is 255.
Mahalanobis (Vec1,vec2,covar);
merge (Vector,mat);//Combine multiple mats into one and split opposite
Double Norm (...) : When Src2 wood sometimes, norm can calculate the longest vector, vector distance, and vector distance and the square root of the arithmetic
Solvecubic solution of 3 times equation, solvepoly n-Th equation
Arrangement: Sort,sortidx
Mixchannels (); Perform various passes on a channel
-----------------------------------------------------------------
11. Functions that are not understood
Getconvertelem,extractimagecoi,lut
Magnitude (X,Y,DST);//i1,i2 are all 1-dimensional vectors, dst=sqrt (x (i) ^2+y (i) ^2);
Meanstddev,
Mulspectrums (I1,i2,dst,flags);
Normalize (I,dst,alpha,beta,int normtype=norm_l2,int rtype=-1,mask);//Normalization
Pca,svd,solve,transform,transpose
Second, other data structure
POINT2F P (5,1);
point3f p3f (2,6,7);
Vector V;v.push_back ((float) cv_pi), V.push_back (2); V.push_back (3.01f);//Continuous entry
Vector vpoints (20);//one-time definition of 20


Iii. Common methods
Mat mask=src<0; so soon build a mat

"" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "", "" "" "" "" "" "" "" "" "" "" "" "," "" "" "" "" "" "" "" "." ""

Note: This article reference: http://blog.sina.com.cn/s/blog_afe2af380101bqhz.html |

‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘

Related matrix operations in OpenCV

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.