-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAffine.cpp
83 lines (69 loc) · 1.96 KB
/
Affine.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
//////////////////////////////////////////////////////////////////////
// Polynomial.cpp: implementation of the CPolynomial class.
// Author: Miller Zhang
// Email : [email protected]
//////////////////////////////////////////////////////////////////////
#include <vector>
#include <string>
#include <iostream>
#include "Affine.h"
#include "math.h"
#include "stdafx.h"
#include "opencv/cv.h"
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using cv::Mat;
CAffine::CAffine()
{
}
CAffine::~CAffine()
{
}
bool CAffine::Compmuterpara(vector<Point2d_csu> &m_Arraylimage,vector<Point2d_csu> &m_Arrayrimage)
{
if (m_Arraylimage.size() != m_Arrayrimage.size())
{
std::cout<<"error : size of points doesn't equal"<<std::endl;
return false;
}
int m_nNumofPoint = m_Arraylimage.size();
if (m_nNumofPoint < 3)
{
std::cout<<"too few points can be used, at least 3 point is requared"<<std::endl;
return false;
}
Mat A = Mat::zeros(2*m_nNumofPoint,6,CV_32F);
Mat l = Mat::zeros(2*m_nNumofPoint,1,CV_32F);
for (int i = 0; i < m_nNumofPoint; i++)
{
A.at<float>(2*i,0) = 1;
A.at<float>(2*i,1) = m_Arraylimage.at(i).x;
A.at<float>(2*i,2) = m_Arraylimage.at(i).y;
l.at<float>(2*i,0) = m_Arrayrimage.at(i).x;
A.at<float>(2*i+1,3) = 1;
A.at<float>(2*i+1,4) = m_Arraylimage.at(i).x;
A.at<float>(2*i+1,5) = m_Arraylimage.at(i).y;
l.at<float>(2*i+1,0) = m_Arrayrimage.at(i).y;
}
//cout<<"A: "<<A<<endl;
//cout<<"l: "<<l<<endl;
Mat params = (A.t()*A).inv()*A.t()*l;
a0 = params.at<float>(0,0);
a1 = params.at<float>(1,0);
a2 = params.at<float>(2,0);
b0 = params.at<float>(3,0);
b1 = params.at<float>(4,0);
b2 = params.at<float>(5,0);
//cout<<"params: "<<params<<endl;
return true;
}
//compute the source image pos (x,y) using the params which obtained by "Compmuterpara" function
Point2d_csu CAffine::positiveMS(Point2d_csu ptsorce)
{
Point2d_csu pt;
int xs = ptsorce.x;
int ys = ptsorce.y;
pt.x = a0 + a1*xs+a2*ys;
pt.y = b0 + b1*xs+b2*ys;
return pt;
}