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的更多相关文章

  1. Mahout源码分析之 -- QR矩阵分解

    一.算法原理 请参考我在大学时写的<QR方法求矩阵全部特征值>,其包含原理.实例及C语言实现:http://www.docin.com/p-114587383.html 二.源码分析 这里 ...

  2. [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 ...

  3. QR分解

        从矩阵分解的角度来看,LU和Cholesky分解目标在于将矩阵转化为三角矩阵的乘积,所以在LAPACK种对应的名称是trf(Triangular Factorization).QR分解的目的在 ...

  4. [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$. ...

  5. [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 ...

  6. OpenCV(5)-图像掩码操作(卷积)-锐化

    锐化概念 图像平滑过程是去除噪声的过程.图像的主要能量在低频部分,而噪声主要集中在高频部分.图像的边缘信息主要也在高频部分,在平滑处理后,将会丢不部分边缘信息.因此需要使用锐化技术来增强边缘. 平滑处 ...

  7. C++ & OpenCV 零散学习总结

    OpenCV中Mat基本用法: Mat类 (Matrix的缩写) 是OpenCV用于处理图像而引入的一个封装类.从功能上讲,Mat类在IplImage结构的基础上进一步增强,并且,由于引入C++高级编 ...

  8. 【Python | opencv+PIL】常见操作(创建、添加帧、绘图、读取等)的效率对比及其优化

    一.背景 本人准备用python做图像和视频编辑的操作,却发现opencv和PIL的效率并不是很理想,并且同样的需求有多种不同的写法并有着不同的效率.见全网并无较完整的效率对比文档,遂决定自己丰衣足食 ...

  9. 【原创】开源Math.NET基础数学类库使用(06)直接求解线性方程组

                   本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新  开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...

随机推荐

  1. SQL Server 优化-执行计划

    对于SQL Server的优化来说,优化查询可能是很常见的事情.由于数据库的优化,本身也是一个涉及面比较的广的话题, 因此本文只谈优化查询时如何看懂SQL Server查询计划.毕竟我对SQL Ser ...

  2. eclipse svn插件地址

    http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID=2240

  3. [Nginx] - 负载均衡配置

    nginx.conf文件内容: user www www; worker_processes 2; error_log /usr/local/nginx/logs/serror.log crit; p ...

  4. Spring JDBC实现查询

    1 db.properties jdbc.user=root jdbc.password=920614 jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbc ...

  5. [LeetCode] 14. Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. public class ...

  6. 【学】React的学习之旅3 - 添加事件(onClick)

    button除了用<input type="button">之外,还可以直接用<button></button>来创建,而2个标签的中间的内容就 ...

  7. AJAX怎么用POST 传参数

    //注册回调函数.注意注册回调函数是不能加括号,加了会把函数的值返回给onreadystatechange xmlhttp.onreadystatechange = callback; //设置连接信 ...

  8. TEX学习笔记

    整理在这里, 方便以后容易查找. 毕竟每个tex的模板有些不一样. Beamer: Latex beamer 学习总结 http://blog.sina.com.cn/s/blog_6cf921f30 ...

  9. webUploader react 接口设计

    import React, {PropTypes} from 'react'; export default class Test extends React.Component { construc ...

  10. maven配置多模块项目事例

    limit-parent <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// ...