Learning Opencv 3 —— 七章 Opencv 中的函子

Opencv 函数

Principal Component Analysis (cv::PCA)

主成分分析主要完成的功能是从高维空间中抽取其中的若干维,并使得保留的维度能够保留原始数据的绝大多数信息。

图中显示了 PCA 的完成过程,其找出现有分布中方差较大的多个维度,并将数据向这几个方向进行投影,从而完成数据的降维。

Opencv 针对 PCA 操作提供了一个功能类 cv::PCA

PCA::PCA();
PCA::PCA(
	cv::InputArray data, // Data, as rows or cols in 2d array
	cv::InputArray mean, // average, if known, 1-by-n or n-by-1
	int flags, // Are vectors rows or cols of 'data'
	int maxComponents = 0 // Max dimensions to retain
);

其提供了两个构造函数——一个默认构造函数,一个自定义构造函数

参数说明:

  • data:满足对应分布的所有样本,二维矩阵,每个样本按行或按列排列
  • mean:可选参数,指定数据每一维的均值,其可以为 1 * n 或 n * 1 的向量
  • flags:给出数据是按行(cv::PCA_DATA_AS_ROW)还是按列排列(cv::PCA_DATA_AS_COL)
  • maxComponents:PCA 保留的最大维度,默认所有维数都保留

一旦完成了 PCA 类的初始化,就可以使用对应函数进行相应操作。 

cv::PCA::project()
cv::Mat PCA::project( // Return results, as a 2d matrix
	cv::InputArray vec // points to project, rows or cols, 2d
) const;
void PCA::project(
	cv::InputArray vec // points to project, rows or cols, 2d
	cv::OutputArray result // Result of projection, reduced space
) const;

参数说明:

  • vec:需要投影的数据,其数据维数和排列方式需要跟构造器中给出的 data 相同
  • result:第一个直接将投影结果通过返回值返回,第二个参数 result 返回。计算结果与 data 具有相同的排列方式,维度又之前的 maxComponents 指定。

 在进行完投影之后,就是将压缩之后的数据进行反投影到原始空间中。这样就可以查看具体的压缩效果,通常反投影之后的结果与原始数据会有少量的差别,这些差别就是投影过程中丢弃的维度上的信息。

cv::PCA::backProject()
cv::Mat PCA::backProject( // Return results, as a 2d matrix
	cv::InputArray vec // Result of projection, reduced space
} const;
void PCA::backProject(
	cv::InputArray vec // Result of projection, reduced space
	cv::OutputArray result // "reconstructed" vectors, full dimension
) const;

其具体参数与 project 函数参数类似。

Singular Value Decomposition (cv::SVD)

SVD类的构造函数

cv::SVD()
SVD::SVD();
SVD::SVD(
	cv::InputArray A, // Linear system, array to be decomposed
	int flags = 0 // what to construct, can A can scratch
);

使用默认构造函数可以使用如下函数具体指定待分解的矩阵 

cv::SVD::operator()()
SVD::& SVD::operator() (
	cv::InputArray A, // Linear system, array to be decomposed
	int flags = 0 // what to construct, can A be scratch
	);

参数说明:

  • flags:cv::SVD::MODIFY_A(是否允许对 A 进行小幅修改从而提高计算速度并降低存储需求),cv::SVD::NO_UV,cv::SVD::FULL_UV 后两个相互排斥,标示是否需要计算 U 和 V 矩阵

除了上述方式将求得结果保存在内部,还可以使用如下函数用用户定义空间存储

cv::SVD::compute()
void SVD::compute(
	cv::InputArray A, // Linear system, array to be decomposed
	cv::OutputArray W, // Output array 'W', singular values
	cv::OutputArray U, // Output array 'U', left singular vectors
	cv::OutputArray Vt, // Output array 'Vt', right singular vectors
	int flags = 0 // what to construct, and if A can be scratch
);
cv::SVD::solveZ()
void SVD::solveZ(
	cv::InputArray A, // Linear system, array to be decomposed
	cv::OutputArray z // One possible solution (unit length)
);

其求解了如下公式

如果方程式有解,返回一个可行解;如果无解,则返回一个模值最小的解。

cv::SVD::backSubst()
void SVD::backSubst(
	cv::InputArray b, // Righthand side of linear system
	cv::OutputArray x // Found solution to linear system
);
void SVD::backSubst(
	cv::InputArray W, // Output array 'W', singular values
	cv::InputArray U, // Output array 'U', left singular vectors
	cv::InputArray Vt, // Output array 'Vt', right singular vectors
	cv::InputArray b, // Righthand side of linear system
	cv::OutputArray x // Found solution to linear system
);

其在指定矩阵 A 之后,求解如下公式

对于超定系统,其使用广义逆实现了最小均方误差意义下的最优解。不过通常情况下,会使用 cv::solve() 进行求解,并指定参数为 cv::DECOMP_SVD。

Random Number Generator (cv::RNG)

随机数生成器使用 Multiply with Carry (MWC) 算法产生均匀分布,使用 Ziggurat 算法产生高斯分布

cv::RNG()
cv::RNG& theRNG( void ); // Return a random number generator
cv::RNG::RNG(void);
cv::RNG::RNG(uint64 state); // create using the seed 'state'

其将产生一个默认的随机数,原理类似 cv::randu() 和 cv::randn()。其中 state 为一个无符号的64位整数,作为随机序列种子;传入 0 即调用默认种子。

cv::RNG::operator T(), where T is your favorite type
cv::RNG::operator uchar();
cv::RNG::operator schar();
cv::RNG::operator ushort();
cv::RNG::operator short int();
cv::RNG::operator int();
cv::RNG::operator unsigned();
cv::RNG::operator float();
cv::RNG::operator double();

范例

cv::RNG rng = cv::theRNG();
cout << "An integer: " << (int)rng << endl;
cout << "Another integer: " << int(rng) << endl;
cout << "A float: " << (float)rng << endl;
cout << "Another float: " << float(rng) << endl;

产生一个随机整数,服从 0 - N-1 上均匀分布

cv::RNG::operator()
unsigned int cv::RNG::operator()(); // Return random value from 0-UINT_MAX
unsigned int cv::RNG::operator()( unsigned int N ); // Return value from 0-(N-1)

产生某个范围内的均匀分布

cv::RNG::uniform()
int cv::RNG::uniform( int a, int b ); // Return value from a-(b-1)
float cv::RNG::uniform( float a, float b ); // Return value in range [a,b)
double cv::RNG::uniform( double a, double b ); // Return value in range [a,b)

零均值高斯分布

cv::RNG::gaussian()
double cv::RNG::gaussian( double sigma ); // Gaussian number, zero mean,
// std-dev='sigma'

均匀分布和高斯分布的通用函数

cv::RNG::fill()
void cv::RNG::fill(
	InputOutputArray mat, // Input array, values will be overwritten
	int distType, // Type of distribution (Gaussian or uniform)
	InputArray a, // min (uniform) or mean (Gaussian)
	InputArray b // max (uniform) or std-deviation (Gaussian)
);

参数说明:

  • distType:cv::RNG::UNIFORM 或者 cv::RNG::NORMAL
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值