首页 技术 正文
技术 2022年11月15日
0 收藏 565 点赞 2,769 浏览 1973 个字

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);
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898