-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHomAdapter.cpp
More file actions
145 lines (121 loc) · 3.88 KB
/
HomAdapter.cpp
File metadata and controls
145 lines (121 loc) · 3.88 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
// HomAdapter.cpp
// RANSAC_AR
//
// Created by Michael Hotan on 5/12/12.
// Copyright (c) 2012 University of Washington. All rights reserved.
//
#include <iostream>
#include <exception>
#include <string>
#include <cstdlib>
#include <vector>
#include <opencv2/opencv.hpp>
#include "HomAdapter.h"
#include "ImageDrawingTools.h"
#define RED CV_RGB(250,0,0)
#define GREEN CV_RGB(0,250,0)
#define BLUE CV_RGB(0,0,250)
#define WHITE CV_RGB(250,250,250)
#define BLACK CV_RGB(0,0,0)
using namespace cv;
Mat HomAdapter::GetHomography() const{
return Homography_;
}
Mat HomAdapter::GetInverseHomography() const{
Mat inverse;
invert(GetHomography(), inverse);
return inverse;
}
Mat HomAdapter::GetProjectedPoints(const vector<CvPoint2D32f> &ptList, int image_num) const{
vector<Point2f> points;
int i;
for (i = 0; i < ptList.size(); ++i){
Point2f temp;
temp.x = ptList[i].x;
temp.y = ptList[i].y;
points[i] = temp;
}
return GetProjectedPoints(ptList, image_num);
}
Mat HomAdapter::GetProjectedPoints(const vector<Point2f> &ptList, int image_num) const{
Mat proj;
switch(image_num){
case 1:
//pt List are points on the second image
//Map these points back to the first image
perspectiveTransform(Mat(ptList), proj, GetInverseHomography());
break;
case 2:
//pt List are points on the first image
//Map these points back to the second image
perspectiveTransform(Mat(ptList), proj, GetHomography());
break;
default:
throw "Illegal Image Number";
}
return proj;
}
void HomAdapter::PrintCorrespondingPoints(){
int i;
int size = matchedPts1_.size();
for(i=0; i<size; ++i){
cout << "Image 1 Point [ X: " << matchedPts1_[i].x << ", Y: " << matchedPts1_[i].y << " ]";
cout << " Image 2 Point [ X: " << matchedPts2_[i].x << ", Y: " << matchedPts2_[i].y << " ]";
cout << endl;
}
}
cv::Mat HomAdapter::GetProjectedWarpImage(const cv::Mat &img1, const cv::Mat &img2, int image_num){
//Make tmeporary copies of MAt
Mat src, dest;
Mat HH; //Homography holder
cv::Size size;
//Set parameters
switch (image_num){
case 1: //Project warped first image
src = img1;
size.width = img2.cols;
size.height = img2.rows;
HH = GetHomography();
break;
case 2: //Project second image
src = img2;
HH = GetInverseHomography();
size.width = img1.cols;
size.height = img1.rows;
break;
default:
throw "Illegal image number, Image number needs to be 1 or 2 ";
//Returns empty Mat
}
//Warp using Perspective
cv::warpPerspective(src, dest, HH, size);
return dest;
}
cv::Mat HomAdapter::GetProjectedPoints(const cv::Mat &img1, const cv::Mat &img2, int image_num){
Mat select_img, proj;
vector<Point2f> plist;
//Pending on the image number draw imaeg accordingly
switch (image_num){
case 1:
plist = GetMatchedPts1();
proj = GetProjectedPoints(GetMatchedPts2(), image_num);
select_img = img1;
break;
case 2:
plist = GetMatchedPts2();
proj = GetProjectedPoints(GetMatchedPts1(), image_num);
select_img = img2;
break;
default:
throw "Illegal image number, Image number needs to be 1 or 2 ";
//Returns empty Mat
}
//Label each circle
assert(plist.size() == proj.rows);
//Draw all the selections on the image
drawCircleOnSelected(plist, select_img, BLUE);
drawXOnSelected(proj, select_img, RED);
drawLinesOnSelected(plist, toPoint2fVector(proj), select_img, RED);
return select_img;
}