-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cpp
195 lines (172 loc) · 6.08 KB
/
util.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
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\highgui\highgui.hpp"
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include "util.h"
using namespace cv;
using namespace std;
util::util(void)
{
}
util::~util(void)
{
}
/********************************************************************************/
/* Author: Viet Anh */
/* Method squareDouble */
/* parameter: double a */
/* return: a*a */
/********************************************************************************/
double util::squareDouble(double a){
return a * a;
}
/********************************************************************************/
/* Author: Viet Anh */
/* Method compare */
/* parameter: void *a, *b */
/* return: a*a */
/********************************************************************************/
int util::compare(const void *a, const void *b){
float fa = *(float *) a;
float fb = *(float *) b;
return (fa > fb) - (fa < fb);
}
/********************************************************************************/
/* Author: Viet Anh */
/* Method medianAngles */
/* parameter: float *angles, int n */
/* return: a*a */
/********************************************************************************/
float util::medianAngles(float *angles, int n){
float result;
qsort(angles, n, sizeof(float), util::compare);
if(n%2 == 0)
{
return (angles[n / 2] + angles[n / 2 - 1]) / 2;
}else
return angles[n / 2];
}
/********************************************************************************/
/* Author: Viet Anh */
/* Method distanceBetweenTwoPoint */
/* Describe: Tính khoảng các giữa 2 điểm */
/* parameter: cv::Point line_start, cv::Point line_end */
/* return: */
/********************************************************************************/
double util::distanceBetweenTwoPoint(cv::Point line_start, cv::Point line_end){
return sqrt(util::squareDouble(line_end.x - line_start.x) + util::squareDouble(line_end.y - line_start.y));;
}
/********************************************************************************/
/* Author: Viet Anh */
/* Method distanceBetweenTwoCVPoint */
/* Describe: Tính khoảng các giữa 2 điểm */
/* parameter: cv::Point line_start, cv::Point line_end */
/* return: */
/********************************************************************************/
double util::distanceBetweenTwoCVPoint(CvPoint &p, CvPoint &p1){
return sqrt(pow(p1.x - p.x,2) + pow(p1.y - p.y, 2));
}
/********************************************************************************/
/* Author: Viet Anh */
/* Method distancePointToLine */
/* Describe: Tính khoảng cách giữa 1 điểm tới 1 đường thẳng */
/* parameter: */
/* return: */
/********************************************************************************/
double util::distancePointToLine(cv::Point line_start, cv::Point line_end, cv::Point point){
// y = ax + b
// y' = -ax' + b'
float a = (line_end.y - line_start.y)/(line_end.x - line_start.x);
float b = line_start.y - a * line_start.x;
int b1 = point.y + a * point.x;
cv::Point temp;
temp.x = (b1 - b)/ (2*a);
temp.y = temp.x * a + b;
return util::distanceBetweenTwoPoint(point, temp);;
}
/********************************************************************************/
/* Author: Viet Anh */
/* Method cropImage */
/* Describe: Crop image in OpenCV */
/* parameter: src: image source */
/* dest:image taget */
/* rect: size of crop */
/* return: */
/********************************************************************************/
void util::cropImage(IplImage* src, IplImage* dest, CvRect rect){
cvSetImageROI(src, rect);
cvCopy(src, dest);
cvResetImageROI(src);
}
void util::copyImage(IplImage* small_image, IplImage* big_image){
cv::Mat small = cv::cvarrToMat(small_image);
cv::Mat big = cv::cvarrToMat(big_image);
small.copyTo(big(cv::Rect(0, big_image -> height * 3/4,small.cols, small.rows)));
}
/********************************************************************************/
/* Author: Viet Anh */
/* Method polyfit */
/* Describe: polyfit as matlab */
/* parameter: cv::Mat &src_x, cv::Mat &src_y, cv::Mat &dst, int order */
/* return: */
/********************************************************************************/
void util::writeFile(string data){
ofstream myfile;
myfile.open ("result.txt", fstream::app);
myfile << data;
myfile.close();
}
void util::writeFile(string data, string file_path){
ofstream myfile;
myfile.open (file_path, fstream::app);
myfile << data;
myfile.close();
}
string util::readFile(string file_path){
std::ifstream t(file_path);
std::string str((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
return str;
}
void util::overwriteFile(string data, string file_path){
ofstream myfile;
myfile.open (file_path);
myfile << data;
myfile.close();
}
vector<string> splitMy(string str, char delimiter) {
vector<string> internal;
stringstream ss(str); // Turn the string into a stream.
string tok;
while(getline(ss, tok, delimiter)) {
internal.push_back(tok);
}
return internal;
}
vector<CvPoint> util::readOutputClipFPT(string url){
string line;
ifstream file (url);
vector<CvPoint> result;
CvPoint temp;
int size = 0;
if(file.is_open()){
while (getline(file, line))
{
if(size == 0){
size = atoi(line.c_str());
}else{
vector<string> a = splitMy(line, ' ');
temp.x = atoi(a[1].c_str());
temp.y = atoi(a[2].c_str()) - 144 * 3;
result.push_back(temp);
}
}
}
return result;
}