1、目录
cv::determinant() | 计算方形矩阵的行列式 |
cv::divide() | 两个数组逐元素相除 |
cv::eigen() | 计算方形矩阵的特征值和特征向量 |
cv::exp() | 逐元素求指数 |
cv::filp() | 翻转矩阵 |
cv::gemm() | 两个数组逐元素相乘 |
cv::filp() | 翻转矩阵 |
cv::gemm() | 两个数组逐元素相乘 |
2、例子代码
//包含OpenCV的头文件
#include <opencv2/opencv.hpp>
#include <opencv2/core/mat.hpp>
#include <iostream>
#include <time.h>
using namespace std;
//使用OpenCV的命名空间
using namespace cv;
//测试cvtColor
//转换空间为Y=0.299R + 0.587G + 0.114B
//测试determinant()
//计算矩阵的行列式
//需要主要的地方是:必须是方形矩阵,必须浮点型数据并且是单通道类型
/*
Matrix m is:
[1, 2;
3, 4]
determinant is: -2
*/
void test_determinant()
{
const int iRows = 2;
const int iCols = 2;
Mat m(iRows, iCols, CV_32FC1);
m.at<float>(0, 0) = 1;
m.at<float>(0, 1) = 2;
m.at<float>(1, 0) = 3;
m.at<float>(1, 1) = 4;
double value = determinant(m);
cout << "Matrix m is:\n" << m << endl;
cout << "determinant is:\t" << value << endl;
return;
}
//测试divide()
//两个矩阵逐像素相除
//CV_EXPORTS_W void divide(InputArray src1, InputArray src2, OutputArray dst,double scale = 1, int dtype = -1);
//公式 scale*src1/src2;
//src1 输入矩阵
//src2 第二个输入矩阵;第二个矩阵和第一个矩阵大小是相同的,并且类型也是相同的
//dst 输出矩阵
//scale 缩放因子
//dtype 输出影像的深度,注意的地方:如果传入-1的话,那么输出矩阵的深度和src2是相同的
//还有另外一个函数
//CV_EXPORTS_W void divide(double scale, InputArray src2,OutputArray dst, int dtype = -1);
//公式不同的地方在于
//scale/src2
/*
Matrix src1 is:
[1, 2, 3;
4, 5, 6;
7, 8, 9]
Matrix src2 is:
[9, 8, 7;
6, 5, 4;
3, 2, 1]
Result matrix is:
[0.11111111, 0.25, 0.42857143;
0.66666669, 1, 1.5;
2.3333333, 4, 9]
Result1 matrix is:
[1, 0.5, 0.33333334;
0.25, 0.2, 0.16666667;
0.14285715, 0.125, 0.11111111]
*/
void test_divide()
{
const int iRows = 3;
const int iCols = 3;
Mat src1(iRows, iCols, CV_32FC1);
Mat src2(iRows, iCols, CV_32FC1);
//测试第一个divide
int Index = 1;
for (int i=0;i<iRows;++i)
{
for (int j=0;j<iCols;++j)
{
src1.at<float>(i, j) = Index;
src2.at<float>(i, j) = 10 - Index;
++Index;
}
}
cout << "Matrix src1 is:\n" << src1 << endl;
cout << "Matrix src2 is:\n" << src2 << endl;
Mat result;
divide(src1, src2, result, 1, CV_32F);
cout << "Result matrix is:\n" << result << endl;
//测试第二个divide
Mat result1;
divide(1.0, src1,result1, 1, CV_32F);
cout << "Result1 matrix is:\n" << result1 << endl;
return;
}
//测试eigen()
//计算特征值
//需要注意的地方:矩阵必须为float类型,并且是对称矩阵,非对称矩阵的话,计算出来的是错误的
/*
Matrix m is:
[2, 1, 0;
1, 3, 1;
0, 1, 2]
eigen values is:
[3.9999998;
1.9999999;
1.0000001]
eigen vectors is:
[0.40824822, 0.81649661, 0.40824834;
-0.70710671, -4.2503757e-08, 0.70710689;
0.57735038, -0.57735038, 0.57735026]
*/
void test_eigen()
{
const int iRows = 3;
const int iCols = 3;
Mat m(iRows, iCols, CV_32FC1);
m.at<float>(0, 0) = 2;
m.at<float>(0, 1) = 1;
m.at<float>(0, 2) = 0;
m.at<float>(1, 0) = 1;
m.at<float>(1, 1) = 3;
m.at<float>(1, 2) = 1;
m.at<float>(2, 0) = 0;
m.at<float>(2, 1) = 1;
m.at<float>(2, 2) = 2;
//计算特征值和特性向量
Mat eigenValues, eigenVectors;
eigen(m, eigenValues, eigenVectors);
cout << "Matrix m is:\n" << m << endl;
cout << "eigen values is:\n" << eigenValues << endl;
cout << "eigen vectors is:\n" << eigenVectors << endl;
return;
}
//测试exp()
//计算指数值
/*
source matrix is:
[0, 0, 0;
1, 1, 1;
2, 2, 2]
Result matrix is:
[1, 1, 1;
2.718282, 2.718282, 2.718282;
7.3890557, 7.3890557, 7.3890557]
*/
void test_exp()
{
const int iRows = 3;
const int iCols = 3;
Mat m(iRows, iCols, CV_32FC1);
for (int i=0;i<iRows;++i)
{
m.at<float>(i, 0) = m.at<float>(i, 1) = m.at<float>(i, 2) = i;
}
Mat Result;
exp(m, Result);
cout << "source matrix is:\n" <<m<< endl << endl;
cout << "Result matrix is:\n" << Result << endl << endl;
return;
}
//测试flip()
//CV_EXPORTS_W void flip(InputArray src, OutputArray dst, int flipCode);
//src 输入矩阵
//dst 输出矩阵
//flipCode 如果大于0的话,沿Y轴进行翻转;如果等于0的话,沿X轴进行翻转;如果小于0的话,那么同时沿X,Y进行翻转
/*
source matrix is:
[ 91, 2, 79;
179, 52, 205;
236, 8, 181]
matrix flip Y axis is:
[ 79, 2, 91;
205, 52, 179;
181, 8, 236]
matrix flip X axis is:
[236, 8, 181;
179, 52, 205;
91, 2, 79]
matrix flip X Y axis is:
[181, 8, 236;
205, 52, 179;
79, 2, 91]
*/
void test_flip()
{
const int iRows = 3;
const int iCols = 3;
Mat src(iRows, iCols, CV_8UC1);
randu(src, 0, 255);
cout << "source matrix is:\n" << src << endl << endl;
Mat dst;
flip(src, dst, 1);
cout << "matrix flip Y axis is:\n" << dst << endl << endl;
flip(src, dst, 0);
cout << "matrix flip X axis is:\n" << dst << endl << endl;
flip(src, dst, -1);
cout << "matrix flip X Y axis is:\n" << dst << endl << endl;
return;
}
//测试inRange()
//CV_EXPORTS_W void inRange(InputArray src, InputArray lowerb,InputArray upperb, OutputArray dst);
//如果在范围之内的话,设置值为255,不然的话结果为0;并且输出矩阵的深度为CV_8U
//这个范围为[lower,upper]
/*
source matrix is:
[ 1, 2, 3;
4, 5, 6;
7, 8, 9]
inRange matrix is:
[ 0, 0, 255;
255, 255, 255;
0, 0, 0]
*/
void test_inRange()
{
const int iRows = 3;
const int iCols = 3;
Mat src(iRows, iCols, CV_8UC1);
int Index = 1;
for (int i = 0; i < iRows; ++i)
{
for (int j = 0; j < iCols; ++j)
{
src.at<unsigned char>(i, j) = Index;
++Index;
}
}
cout << "source matrix is:\n" << src << endl << endl;
Mat dst;
inRange(src, 3, 6, dst);
cout << "inRange matrix is:\n" << dst << endl << endl;
return;
}
//测试invert()
//CV_EXPORTS_W double invert(InputArray src, OutputArray dst, int flags = DECOMP_LU);
//src 输入矩阵,注意这个矩阵必须是浮点型的
//dst 输出矩阵
//三个标志 DECOMP_LU DECOMP_SVD DECOMP_CHOLESKY
//如果矩阵是n*m 的话,那么返回的结果为m*n
//在使用LU分解的话,返回值为src的行列式,如果行列式失败的话,结果为0
//SVD 分解返回出来的结果为最小特征值和最大特征值的比例
//注意的地方:LU和CHOLSKY 分解必须是方形矩阵,非奇异矩阵,并且是正定矩阵
/*
source matrix is:
[1, 2, 3;
1, 0, -1;
0, 1, 1]
invert matrix is:
[0.5, 0.5, -1;
-0.5, 0.5, 2;
0.5, -0.5, -1]
*/
void test_invert()
{
const int iRows = 3;
const int iCols = 3;
Mat src(iRows, iCols, CV_32FC1);
src.at<float>(0, 0) = 1;
src.at<float>(0, 1) = 2;
src.at<float>(0, 2) = 3;
src.at<float>(1, 0) = 1;
src.at<float>(1, 1) = 0;
src.at<float>(1, 2) = -1;
src.at<float>(2, 0) = 0;
src.at<float>(2, 1) = 1;
src.at<float>(2, 2) = 1;
cout << "source matrix is:\n" << src << endl << endl;
Mat dst;
invert(src, dst, DECOMP_LU);
cout << "invert matrix is:\n" << dst << endl << endl;
return;
}
//测试log
//注意的地方:log值应该是大于0的值
/*
source matrix is:
[-3, -2, -1;
0, 1, 2;
3, 4, 5]
log matrix is:
[-nan(ind), -nan(ind), -nan(ind);
-inf, 0, 0.69314718;
1.0986123, 1.3862944, 1.6094379]
*/
void test_log()
{
const int iRows = 3;
const int iCols = 3;
Mat src(iRows, iCols, CV_32FC1);
int Index = -3;
for (int i = 0; i < iRows; ++i)
{
for (int j = 0; j < iCols; ++j)
{
src.at<float>(i, j) = Index;
++Index;
}
}
cout << "source matrix is:\n" << src << endl << endl;
Mat dst;
log(src, dst);
cout << "log matrix is:\n" << dst << endl << endl;
return;
}
int main()
{
//修改默认的随机数
//RNG rng(time(NULL));
//theRNG() = rng;
test_log();
return 0;
}