forked from MasteringOpenCV/code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Common.cpp
executable file
·220 lines (182 loc) · 6.51 KB
/
Common.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*****************************************************************************
* ExploringSfMWithOpenCV
******************************************************************************
* by Roy Shilkrot, 5th Dec 2012
* http://www.morethantechnical.com/
******************************************************************************
* Ch4 of the book "Mastering OpenCV with Practical Computer Vision Projects"
* Copyright Packt Publishing 2012.
* http://www.packtpub.com/cool-projects-with-opencv/book
*****************************************************************************/
#include "Common.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#ifndef WIN32
#include <dirent.h>
#endif
using namespace std;
using namespace cv;
std::vector<cv::DMatch> FlipMatches(const std::vector<cv::DMatch>& matches) {
std::vector<cv::DMatch> flip;
for(int i=0;i<matches.size();i++) {
flip.push_back(matches[i]);
swap(flip.back().queryIdx,flip.back().trainIdx);
}
return flip;
}
std::vector<cv::Point3d> CloudPointsToPoints(const std::vector<CloudPoint> cpts) {
std::vector<cv::Point3d> out;
for (unsigned int i=0; i<cpts.size(); i++) {
out.push_back(cpts[i].pt);
}
return out;
}
void GetAlignedPointsFromMatch(const std::vector<cv::KeyPoint>& imgpts1,
const std::vector<cv::KeyPoint>& imgpts2,
const std::vector<cv::DMatch>& matches,
std::vector<cv::KeyPoint>& pt_set1,
std::vector<cv::KeyPoint>& pt_set2)
{
for (unsigned int i=0; i<matches.size(); i++) {
// cout << "matches[i].queryIdx " << matches[i].queryIdx << " matches[i].trainIdx " << matches[i].trainIdx << endl;
assert(matches[i].queryIdx < imgpts1.size());
pt_set1.push_back(imgpts1[matches[i].queryIdx]);
assert(matches[i].trainIdx < imgpts2.size());
pt_set2.push_back(imgpts2[matches[i].trainIdx]);
}
}
void KeyPointsToPoints(const vector<KeyPoint>& kps, vector<Point2f>& ps) {
ps.clear();
for (unsigned int i=0; i<kps.size(); i++) ps.push_back(kps[i].pt);
}
void PointsToKeyPoints(const vector<Point2f>& ps, vector<KeyPoint>& kps) {
kps.clear();
for (unsigned int i=0; i<ps.size(); i++) kps.push_back(KeyPoint(ps[i],1.0f));
}
#define intrpmnmx(val,min,max) (max==min ? 0.0 : ((val)-min)/(max-min))
void drawArrows(Mat& frame, const vector<Point2f>& prevPts, const vector<Point2f>& nextPts, const vector<uchar>& status, const vector<float>& verror, const Scalar& _line_color)
{
double minVal,maxVal; minMaxIdx(verror,&minVal,&maxVal,0,0,status);
int line_thickness = 1;
for (size_t i = 0; i < prevPts.size(); ++i)
{
if (status[i])
{
double alpha = intrpmnmx(verror[i],minVal,maxVal); alpha = 1.0 - alpha;
Scalar line_color(alpha*_line_color[0],
alpha*_line_color[1],
alpha*_line_color[2]);
Point p = prevPts[i];
Point q = nextPts[i];
double angle = atan2((double) p.y - q.y, (double) p.x - q.x);
double hypotenuse = sqrt( (double)(p.y - q.y)*(p.y - q.y) + (double)(p.x - q.x)*(p.x - q.x) );
if (hypotenuse < 1.0)
continue;
// Here we lengthen the arrow by a factor of three.
q.x = (int) (p.x - 3 * hypotenuse * cos(angle));
q.y = (int) (p.y - 3 * hypotenuse * sin(angle));
// Now we draw the main line of the arrow.
line(frame, p, q, line_color, line_thickness);
// Now draw the tips of the arrow. I do some scaling so that the
// tips look proportional to the main line of the arrow.
p.x = (int) (q.x + 9 * cos(angle + CV_PI / 4));
p.y = (int) (q.y + 9 * sin(angle + CV_PI / 4));
line(frame, p, q, line_color, line_thickness);
p.x = (int) (q.x + 9 * cos(angle - CV_PI / 4));
p.y = (int) (q.y + 9 * sin(angle - CV_PI / 4));
line(frame, p, q, line_color, line_thickness);
}
}
}
bool hasEnding (std::string const &fullString, std::string const &ending)
{
if (fullString.length() >= ending.length()) {
return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
} else {
return false;
}
}
bool hasEndingLower (string const &fullString_, string const &_ending)
{
string fullstring = fullString_, ending = _ending;
transform(fullString_.begin(),fullString_.end(),fullstring.begin(),::tolower); // to lower
return hasEnding(fullstring,ending);
}
void imshow_250x250(const string& name_, const Mat& patch) {
Mat bigpatch; cv::resize(patch,bigpatch,Size(250,250));
imshow(name_,bigpatch);
}
void open_imgs_dir(char* dir_name, std::vector<cv::Mat>& images, std::vector<std::string>& images_names, double downscale_factor) {
if (dir_name == NULL) {
return;
}
string dir_name_ = string(dir_name);
vector<string> files_;
#ifndef WIN32
//open a directory the POSIX way
DIR *dp;
struct dirent *ep;
dp = opendir (dir_name);
if (dp != NULL)
{
while (ep = readdir (dp)) {
if (ep->d_name[0] != '.')
files_.push_back(ep->d_name);
}
(void) closedir (dp);
}
else {
cerr << ("Couldn't open the directory");
return;
}
#else
//open a directory the WIN32 way
HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA fdata;
if(dir_name_[dir_name_.size()-1] == '\\' || dir_name_[dir_name_.size()-1] == '/') {
dir_name_ = dir_name_.substr(0,dir_name_.size()-1);
}
hFind = FindFirstFile(string(dir_name_).append("\\*").c_str(), &fdata);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
if (strcmp(fdata.cFileName, ".") != 0 &&
strcmp(fdata.cFileName, "..") != 0)
{
if (fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
continue; // a diretory
}
else
{
files_.push_back(fdata.cFileName);
}
}
}
while (FindNextFile(hFind, &fdata) != 0);
} else {
cerr << "can't open directory\n";
return;
}
if (GetLastError() != ERROR_NO_MORE_FILES)
{
FindClose(hFind);
cerr << "some other error with opening directory: " << GetLastError() << endl;
return;
}
FindClose(hFind);
hFind = INVALID_HANDLE_VALUE;
#endif
for (unsigned int i=0; i<files_.size(); i++) {
if (files_[i][0] == '.' || !(hasEndingLower(files_[i],"jpg")||hasEndingLower(files_[i],"png"))) {
continue;
}
cv::Mat m_ = cv::imread(string(dir_name_).append("/").append(files_[i]));
if(downscale_factor != 1.0)
cv::resize(m_,m_,Size(),downscale_factor,downscale_factor);
images_names.push_back(files_[i]);
images.push_back(m_);
}
}