OpenCV QR分解

#include<cxcore.h>
#include<cv.h>
#include<highgui.h>
#include<stdio.h>
cv::Ptr<CvMat> householder(cv::Ptr<CvMat>& x, cv::Ptr<CvMat>& y);
cv::Ptr<CvMat> blkdiag(const cv::Ptr<CvMat>& Ht, const CvSize& rect);
cv::Ptr<CvMat> blkdiag(const cv::Ptr<CvMat>& Ht, const CvSize& rect);
int ypQR_householder(const cv::Ptr<CvMat>& A, cv::Ptr<CvMat>& Q, cv::Ptr<CvMat>& R)
{
    int width = A->cols;
    int height = A->rows;
    R = cvCloneMat(A);
    Q = cvCreateMat(height, height, A->type);
    cvSetIdentity(Q);
    int end = 0;
    if(width > height)
        end = height;
    else
    end = width;
    for(int i = 0 ; i < end; i++)
    {
        cv::Ptr<CvMat> x = cvCreateMatHeader( height - i, 1, CV_32F);//此处一定要注意
        cvGetSubRect(R, x, cvRect(i, i, 1, height - i));
        cv::Ptr<CvMat> y = cvCreateMat( height - i, 1, CV_32F);
        cvZero(y);
        cvmSet(y, 0, 0, 1);
        cv::Ptr<CvMat> Ht = householder(x, y);
        if(NULL == Ht) return -1;
        cv::Ptr<CvMat> H = blkdiag(Ht, cvSize(Q->cols, Q->rows));
        if(NULL == H) return -1;
        cvmMul(Q, H, Q);
        cvmMul(H, R, R);
    }
    return 0;
}

cv::Ptr<CvMat> householder(cv::Ptr<CvMat>& x, cv::Ptr<CvMat>& y)
{
    int sign = 1;
    if(cvmGet(x, 0, 0) < 0)
    sign = -1;
    float nx = cvNorm(x);
    float ny = cvNorm(y);
    float rho = -1 * sign * nx / ny;
    cvConvertScale(y, y, rho);
    cv::Ptr<CvMat> diff_xy = cvCreateMat(x->rows, x->cols, x->type);
    cvSub(x , y, diff_xy);

    float n_diff_xy = cvNorm(diff_xy);
    cvConvertScale(diff_xy, diff_xy, 1/n_diff_xy);

    cv::Ptr<CvMat> I = cvCreateMat( x->rows, x->rows, x->type);
    cvSetIdentity(I);

    cv::Ptr<CvMat> diff_xy_trans = cvCreateMat(diff_xy->cols, diff_xy->rows, diff_xy->type);
    cvTranspose(diff_xy, diff_xy_trans);

    cv::Ptr<CvMat> diff_xy_mul = cvCreateMat(diff_xy->rows, diff_xy_trans->cols, diff_xy->type);
    cvmMul(diff_xy, diff_xy_trans, diff_xy_mul);
    cvConvertScale( diff_xy_mul, diff_xy_mul, -2);
    cvAdd(I, diff_xy_mul, diff_xy_mul);

    return diff_xy_mul;
}

cv::Ptr<CvMat> blkdiag(const cv::Ptr<CvMat>& Ht, const CvSize& rect)
{
    int H_width = rect.width;
    int H_height = rect.height;

    int Ht_width = Ht->cols;
    int Ht_height = Ht->rows;
    if(H_width < Ht_width || H_height < Ht_height)
        return NULL;
    else if(H_width == Ht_width && H_height == Ht_height)
        return Ht;
    else
    {
        cv::Ptr<CvMat> H = cvCreateMat(rect.height, rect.width, CV_32F);
        cvSetIdentity(H);
        int start_i = H_height - Ht_height;
        int start_j = H_width - Ht_width;

        for(int i = start_i; i < H_height; i++)
        {
            for(int j = start_j; j < H_width; j++)
            {
                cvmSet(H, i, j, cvmGet(Ht, i- start_i, j - start_j));
            }
        }

        return H;
    }
}

/***********************************test*****************************************/
void test_qr()
{
    float a[] = {-5,7, 6,8,-20,26,5,-5,34,16,9,65,10,13,15};
    cv::Ptr<CvMat> A = cvCreateMatHeader(3, 5, CV_32FC1);
    cvInitMatHeader( A, 3, 5, CV_32FC1, a);
    cv::Ptr<CvMat> Q = NULL;
    cv::Ptr<CvMat> R = NULL;
    int re = ypQR_householder(A, Q, R);
    if(-1 == re || NULL == Q || NULL == R) return ;

    cv::Ptr<CvMat> Aq = cvCreateMat( 3, 5,CV_32FC1);
    cvmMul(Q, R, Aq);
    printf("A:\n");
    for(int i=0;i< A->rows;i++)
    {
        for(int j=0;j<A->cols;j++)
            printf("%f ",cvGetReal2D( A, i, j ));
        printf("\n");
    }
    printf("Q:\n");
    for(int i=0;i< Q->rows;i++)
    {
        for(int j=0;j<Q->cols;j++)
            printf("%f ",cvGetReal2D( Q, i, j ));
        printf("\n");
    }
    printf("R:\n");
    for(int i=0;i< R->rows;i++)
    {
        for(int j=0;j<R->cols;j++)
            printf("%f ",cvGetReal2D( R, i, j ));
        printf("\n");
    }
    printf("At:\n");
    for(int i=0;i<Aq->rows;i++)
    {
        printf("\n");
        for(int j=0;j<Aq->cols;j++)
            printf("%f ",cvGetReal2D( Aq, i, j ));
    }
    printf("\n");
    return ;
}
int main(){
    test_qr();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值