C++调用Opencv实现基本矩阵操作

1、生成常用矩阵

Mat类中的函数eye(),one()在mat.hpp文件中有申明:

/*@param rows Number of rows.
  @param cols Number of columns.
  @param type Created matrix type.*/
static MatExpr eye(int rows, int cols, int type);
static MatExpr ones(int rows, int cols, int type);

/*@param size Alternative matrix size specification as Size(cols, rows) .
  @param type Created matrix type.*/
static MatExpr eye(Size size, int type);
static MatExpr ones(Size size, int type);

/*@param ndims Array dimensionality.
  @param sz Array of integers specifying the array shape.
  @param type Created matrix type.*/
static MatExpr ones(int ndims, const int* sz, int type);

生成矩阵:

#include
#include 
using namespace std;
using namespace cv;
int main()
{
	Mat m1 = Mat::eye(4, 4, CV_32F);
	Mat m2 = Mat::eye(Size(5, 5), CV_32S);
	Mat m3 = Mat::ones(3, 4, CV_32F);
        Mat m4 = (Mat_ < float >(3, 3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
	cout << "m1="</*@param d index of the diagonal, with the following values:
    - `d=0` is the main diagonal.
    - `d<0` is a diagonal from the lower half. For example, d=-1 means the diagonal is set
      immediately below the main one.
    - `d>0` is a diagonal from the upper half. For example, d=1 means the diagonal is set
      immediately above the main one.*/
Mat diag(int d=0) const;

/* @brief creates a diagonal matrix
   The method creates a square diagonal matrix from specified main diagonal.
   @param d One-dimensional matrix that represents the main diagonal.*/
static Mat diag(const Mat& d);

代码调用实现:

#include
#include 
using namespace std;
using namespace cv;
int main()
{
	Mat m4 = (Mat_ < float >(3, 3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
	Mat m5= (Mat_ < float >(4, 1) << 1, 2, 3, 4);
	Mat m40=m4.diag(0);//提取主对角
	Mat m41=m4.diag(1);
	Mat m42=m4.diag(-1);
	Mat diag_m5 = Mat::diag(m5);//生成对角矩阵
	cout <<"m4=\n"<< m4<<"\nm40=\n"<< m40<<"\nm41=\n" << m41<<"\nm42=\n" << m42 <

输出为:                         

3、利用Opencv实现Matlab的点乘和点

实现2个矩阵元素与元素

/*Example:
    @code
        Mat C = A.mul(5/B); // equivalent to divide(A, B, C, 5)
    @endcode
    @param m Another array of the same type and the same size as \*this, or a matrix        
     expression.
    @param scale Optional scale factor.*/
MatExpr mul(InputArray m, double scale=1) const;

之前的相乘或相除,其输出结果如下:

Mat m1 = (Mat_ < float >(3, 3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
Mat m2 = (Mat_ < float >(3, 3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
cout<<"m1=\n"<

                                          

4、求矩阵的二范数(矩阵与矩阵之间,矩阵自己)

/*@param src1 first input array.
  @param normType type of the norm (see #NormTypes).
  @param mask optional operation mask; it must have the same size as src1 and CV_8UC1 type.*/
double norm(InputArray src1, int normType = NORM_L2, InputArray mask = noArray());

/*@param src1 first input array.
  @param src2 second input array of the same size and the same type as src1.
  @param normType type of the norm (see #NormTypes).
  @param mask optional operation mask; it must have the same size as src1 and CV_8UC1 type*/
double norm(InputArray src1, InputArray src2,int normType = NORM_L2, InputArray mask = noArray());

API函数无src2的时候的公式是 : 

另外一个有src2的时候的公式是:

double result = 0.0,result2=0.0;
Mat m1 = (Mat_ < float >(3, 3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
Mat m2 = Mat::ones(3,3,CV_32F);
Mat m3 = 3 * m2;
result = cv::norm(m1, NORM_L2);
result2 = cv::norm(m3,m2,NORM_L2);
cout <<"result="<< result << endl;
cout << "result2=" << result2 << endl;
getchar();

输出的结果为:result=16.8819,result2=6 

 

 

 

 

你可能感兴趣的:(Opencv学习)