Matrix QR Decomposition using OpenCV
Matrix QR decomposition is very useful in least square fitting model. But there is no function available to do it in OpenCV directly. So i write a function to do it myself.
It is based on the House Holder Algorithm. The defailed algorithm can be found in https://en.wikipedia.org/wiki/QR_decomposition.
The function and test code is in below.
void HouseHolderQR(const cv::Mat &A, cv::Mat &Q, cv::Mat &R)
{
assert ( A.channels() == );
assert ( A.rows >= A.cols );
auto sign = [](float value) { return value >= ? : -; };
const auto totalRows = A.rows;
const auto totalCols = A.cols;
R = A.clone();
Q = cv::Mat::eye ( totalRows, totalRows, A.type() );
for ( int col = ; col < A.cols; ++ col )
{
cv::Mat matAROI = cv::Mat ( R, cv::Range ( col, totalRows ), cv::Range ( col, totalCols ) );
cv::Mat y = matAROI.col ( );
auto yNorm = norm ( y );
cv::Mat e1 = cv::Mat::eye ( y.rows, , A.type() );
cv::Mat w = y + sign(y.at<float>(,)) * yNorm * e1;
cv::Mat v = w / norm( w );
cv::Mat vT; cv::transpose(v, vT );
cv::Mat I = cv::Mat::eye( matAROI.rows, matAROI.rows, A.type() );
cv::Mat I_2VVT = I - * v * vT;
cv::Mat matH = cv::Mat::eye ( totalRows, totalRows, A.type() );
cv::Mat matHROI = cv::Mat(matH, cv::Range ( col, totalRows ), cv::Range ( col, totalRows ) );
I_2VVT.copyTo ( matHROI );
R = matH * R;
Q = Q * matH;
}
} void TestQRDecomposition()
{
cv::Mat A, Q, R; //Test case 1
{
//A = cv::Mat ( 4, 3, CV_32FC1 );
//A.at<float>(0,0) = -1.f;
//A.at<float>(0,1) = -1.f;
//A.at<float>(0,2) = 1.f; //A.at<float>(1,0) = 1.f;
//A.at<float>(1,1) = 3.f;
//A.at<float>(1,2) = 3.f; //A.at<float>(2,0) = -1.f;
//A.at<float>(2,1) = -1.f;
//A.at<float>(2,2) = 5.f; //A.at<float>(3,0) = 1.f;
//A.at<float>(3,1) = 3.f;
//A.at<float>(3,2) = 7.f;
} {
A = cv::Mat(, , CV_32FC1);
A.at<float>(, ) = .f;
A.at<float>(, ) = -.f;
A.at<float>(, ) = .f; A.at<float>(, ) = .f;
A.at<float>(, ) = .f;
A.at<float>(, ) = -.f; A.at<float>(, ) = -.f;
A.at<float>(, ) = .f;
A.at<float>(, ) = -.f; A.at<float>(, ) = -.f;
A.at<float>(, ) = .f;
A.at<float>(, ) = .f; A.at<float>(, ) = .f;
A.at<float>(, ) = .f;
A.at<float>(, ) = .f;
} std::cout << "A: " << std::endl;
printfMat<float>(A); HouseHolderQR(A, Q, R); std::cout << "Q: " << std::endl;
printfMat<float>(Q); std::cout << "R: " << std::endl;
printfMat<float>(R); cv::Mat AVerify = Q * R;
std::cout << "AVerify: " << std::endl;
printfMat<float>(AVerify);
}
Matrix QR Decomposition using OpenCV的更多相关文章
- Mahout源码分析之 -- QR矩阵分解
一.算法原理 请参考我在大学时写的<QR方法求矩阵全部特征值>,其包含原理.实例及C语言实现:http://www.docin.com/p-114587383.html 二.源码分析 这里 ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]Contents
I find it may cost me so much time in doing such solutions to exercises and problems....I am sorry t ...
- QR分解
从矩阵分解的角度来看,LU和Cholesky分解目标在于将矩阵转化为三角矩阵的乘积,所以在LAPACK种对应的名称是trf(Triangular Factorization).QR分解的目的在 ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.2
Show that the following statements are equivalent: (1). $A$ is positive. (2). $A=B^*B$ for some $B$. ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.1.3
Use the QR decomposition to prove Hadamard's inequality: if $X=(x_1,\cdots,x_n)$, then $$\bex |\det ...
- OpenCV(5)-图像掩码操作(卷积)-锐化
锐化概念 图像平滑过程是去除噪声的过程.图像的主要能量在低频部分,而噪声主要集中在高频部分.图像的边缘信息主要也在高频部分,在平滑处理后,将会丢不部分边缘信息.因此需要使用锐化技术来增强边缘. 平滑处 ...
- C++ & OpenCV 零散学习总结
OpenCV中Mat基本用法: Mat类 (Matrix的缩写) 是OpenCV用于处理图像而引入的一个封装类.从功能上讲,Mat类在IplImage结构的基础上进一步增强,并且,由于引入C++高级编 ...
- 【Python | opencv+PIL】常见操作(创建、添加帧、绘图、读取等)的效率对比及其优化
一.背景 本人准备用python做图像和视频编辑的操作,却发现opencv和PIL的效率并不是很理想,并且同样的需求有多种不同的写法并有着不同的效率.见全网并无较完整的效率对比文档,遂决定自己丰衣足食 ...
- 【原创】开源Math.NET基础数学类库使用(06)直接求解线性方程组
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...
随机推荐
- spring 中容器 map、set、list、property 的 bean 实例化
参考:http://www.kaifajie.cn/spring/9966.html <bean id="fieldMap" class="org.springfr ...
- RT-Thread的线程(任务)处理【RT-Thread学习笔记 2】
RT-Thread中使用线程这个概念,而不是任务.两者相似,我在这里把他的线程当作任务来理解了 1.任务处理: 动态任务相关API 创建任务:rt_thread_create函数,创建任务之后会返回r ...
- exp导出做成批处理注意事项
不能叫exp.bat,会一直显示导出这句话. 出现EXP-00106: 数据库链接口令无效:是因为http://blog.csdn.net/hzfu007/article/details/189823 ...
- SQL Server 树形表非循环递归查询
很多人可能想要查询整个树形表关联的内容都会通过循环递归来查...事实上在微软在SQL2005或以上版本就能用别的语法进行查询,下面是示例. --通过子节点查询父节点WITH TREE AS( ...
- 升级到VS2013.Update.4的问题
升级到VS2013.Update.4后,编译VS2010的解决方案出错,提示AxImp.exe找不到,到网上搜索后,没有找到能用的法子: 修复VS2013后也无法解决: 折腾2个小时后终于找到问题了: ...
- 在VS中添加lib的简单方法
1.工程---属性---配置属性---VC++ Directories---General---Include Directories:加上头文件存放目录 2.VC中,切换到"解决方案视图& ...
- Model View Controller
On the iPhone or iPod touch, a modal view controller takes over the entire screen. This is the defau ...
- 《JS高程》中的正则的复杂模式的总结
复杂模式: *分组: var reDogDog=/dogdog/g;---------------var reDogDog=/(dog){2}/g; *引用:(注意带括号和不带括号) var sMat ...
- Entity framework code first
EF Code First 不便之处,数据库结构改变时,需要利用程序包管理器控制台生成代码,再用update-database -Verbose更新数据库,这样的做法在很多时候不容易部署.下面介绍一种 ...
- web开发过程中经常用到的一些公共方法及操作
进化成为程序猿也有段岁月了,所谓的经验,广度还是依旧,只不过是对于某种功能有了多种实现方式的想法.每天依旧不厌其烦的敲打着代码,每一行代码的回车似乎都有一种似曾相识的感觉.于是乎:粘贴复制,再粘贴再复 ...