-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQR_Detection.cpp
87 lines (80 loc) · 2.51 KB
/
QR_Detection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "stdafx.h"
#include <cxcore.h>
#include <vector>
using namespace std;
#pragma comment(lib,"cxcore.lib")
#pragma comment(lib,"highgui.lib")
void cvQR(CvMat *inputA,CvMat *q,CvMat *r)
{
CvSize inputSize=cvGetSize(inputA);
int width=inputSize.width;
int height=inputSize.height;
cvSetIdentity(q);
cvCopy(inputA,r);
vector<CvMat **> matMem;
CvMat *tempH=cvCreateMat(height,height,CV_32FC1);
matMem.push_back(&tempH);
CvMat *v=cvCreateMat(height,1,CV_32FC1);
matMem.push_back(&v);
CvMat *tempCol=cvCreateMat(height,1,CV_32FC1);
matMem.push_back(&tempCol);
CvMat *tempCol2=cvCreateMat(height,1,CV_32FC1);
matMem.push_back(&tempCol2);
CvMat *iMat=cvCreateMat(height,height,CV_32FC1);
matMem.push_back(&iMat);
CvMat *transV=cvCreateMat(1,height,CV_32FC1);
matMem.push_back(&transV);
for (int i=0; i<width; i++)
{
float b[] = {0};
CvMat temp;
cvGetCol(r,&temp,i); //get the i col
cvCopy(&temp,tempCol); //copy the row,don't hurt the original column
cvGetSubRect(tempCol,&temp,cvRect(0,0,1,i)); //get the header
if (temp.rows == 0 || temp.cols == 0)
memcpy(temp.data.fl,b,sizeof(float));
else
cvZero(&temp); //make it zero
float colNorm=cvNorm(tempCol); //get the norm of the current column
cvZero(tempCol2); //zero the e vector
float tempval=cvGet2D(r,i,i).val[0];
cvSet2D(tempCol2,i,0,cvScalar(tempval>0?-1:1*colNorm,0,0)); //set the new value
cvSub(tempCol,tempCol2,v); //subtract the two vectors,get v
float val2=cvNorm(v);
if (val2==0)
{
continue;
}
val2*=val2;
val2=1/val2;
cvTranspose(v,transV);
cvMatMul(v,transV,tempH); //
cvScale(tempH,tempH,2*val2);
cvSetIdentity(iMat); //the identity matrix
cvSub(iMat,tempH,iMat);
cvCopy(iMat,tempH); //get the H matrix
cvTranspose(tempH,tempH); //transpose
cvMatMul(q,tempH,q); //q=q.h
cvGetCol(r,&temp,i); //get the col again
cvSet2D(&temp,i,0,cvScalar(colNorm,0,0)); //set the norm
cvGetSubRect(r,&temp,cvRect(i,i+1,1,height-i-1));
if (temp.rows == 0 || temp.cols == 0)
memcpy(temp.data.fl,b,sizeof(float));
else
cvZero(&temp); //zero the submatrix
}
for (int j=0; j<matMem.size(); j++)
{
cvReleaseMat(matMem[j]);
}
}
int main(int argc, char* argv[]){
float matData[]={1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7};
float rData[16];
float qData[16];
CvMat aMat=cvMat(4,4,CV_32FC1,matData);
CvMat rMat=cvMat(4,4,CV_32FC1,rData);
CvMat qMat=cvMat(4,4,CV_32FC1,qData);
cvQR(&aMat,&qMat,&rMat);
float det=cvDet(&qMat); return 0;
}