-
Notifications
You must be signed in to change notification settings - Fork 0
/
PRJ4.cpp
324 lines (244 loc) · 8.28 KB
/
PRJ4.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*Project 4 is designed for analyzing the time from a clock.
It works better with clocks that dont have a second hand because it works with k-means algortihm*/
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <dirent.h>
using namespace cv;
using namespace std;
bool checkIfInTheRange(Vec4i l,int thresh,int centerX,int centerY);
Point2f getIntersectionOfLines(Vec4i line1, Vec4i line2);
Point getDominantCordinate(Vec4i line, Point p);
double getLength(Vec4i l);
void addPointTo(Vec4i l, Mat *matPoint);
int findMaxLengthIndex(vector<Vec4i> lines);
bool segmentIntersectRectangle(double a_p1x, double a_p1y, double a_p2x, double a_p2y, Mat src);
string getTimeFromClock(Mat src);
#define RAD 57.295
int main(int argc, char** argv)
{
const char* PATH = "DATASET";
DIR *firstLevel = opendir(PATH);
struct dirent *entry = readdir(firstLevel);
string result;
while (entry != NULL)
{
if (entry->d_type != DT_DIR && entry->d_name[0] != '.'){
string path= "";
path.append(PATH);
path += "/";
path += entry->d_name;
Mat src = imread(path);
try
{
result = getTimeFromClock(src);
}
catch (exception& e)
{
result = "Error";
}
src.~Mat();
cout << entry->d_name << " " << result << endl;
}
entry = readdir(firstLevel);
}
closedir(firstLevel);
return 0;
}
string getTimeFromClock(Mat src){
Mat src_gray;
Mat matPoint;
cvtColor(src, src_gray, CV_BGR2GRAY);
//Eliminate minor details
cv::threshold(src_gray, src_gray, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
//Invert colors
bitwise_not ( src_gray, src_gray );
//Try to eliminate second hand if present
erode(src_gray, src_gray, Mat(),Point(-1,-1));
vector<Vec4i> lines;
vector<Vec4i> validLines;
HoughLinesP(src_gray, lines, 1, CV_PI/180, 50, 50, 10
);
//Get the lines around the center
for( size_t i = 0; i < lines.size(); i++ )
{
Vec4i l = lines[i];
if(segmentIntersectRectangle(l[0], l[1], l[2], l[3],src)){
//Add line's points to the data to prepare for kmeans
addPointTo(l, &matPoint);
validLines.push_back(l);
}
}
cv::Mat labels;
cv::Mat centers(2, 1, CV_32FC2);
//Find 2 centers
cv::kmeans(matPoint, 2, labels,
cv::TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 1000, 0.01),
3, cv::KMEANS_PP_CENTERS, centers);
vector<Vec4i> firstLines;
vector<Vec4i> secondLines;
//Distribute lines according to centers
Vec2f firstCenter = centers.at<Vec2f>(0,0);
for( size_t i = 0; i < validLines.size(); i++ )
{
Vec4i l = validLines[i];
if (checkIfInTheRange(l, 5, firstCenter[0],firstCenter[1])) {
firstLines.push_back(l);
}else{
secondLines.push_back(l);
}
}
int longestLineInFirst = findMaxLengthIndex(firstLines);
int longestLineInSecond = findMaxLengthIndex(secondLines);
Vec4i firstLine = firstLines.at(longestLineInFirst);
Vec4i secondLine = secondLines.at(longestLineInSecond);
Vec4i minuteHand,hourHand;
if (getLength(firstLine)>getLength(secondLine)) {
minuteHand = firstLine;
hourHand = secondLine;
}else{
minuteHand = secondLine;
hourHand = firstLine;
}
//Find the intersection of lines
Point center = getIntersectionOfLines(minuteHand, hourHand);
//Find the dominant sides of those lines
Point minutePoint = getDominantCordinate(minuteHand, center);
Point hourPoint = getDominantCordinate(hourHand, center);
//Calculate time
float angleMinute = fmod(360 + RAD*atan2(minutePoint.x-center.x, center.y - minutePoint.y),360);
float angleHour = fmod(360 + RAD*atan2(hourPoint.x-center.x, center.y - hourPoint.y),360);
int minute = round(angleMinute/6);
int hour = angleHour/30;
string result = "It is " + to_string(hour) + " : " + to_string(minute);
return result;
}
bool checkIfInTheRange(Vec4i l,int thresh,int centerX,int centerY){
//Point to line distance claculation
double normalLength = hypot(l[2] - l[0], l[3] - l[1]);
double distance = (double)((centerX - l[0]) * (l[3] - l[1]) - (centerY - l[1]) * (l[2] - l[0])) / normalLength;
return thresh > abs(distance);
}
void addPointTo(Vec4i l, Mat *matPoint){
//Adding points to line
int startX = l[0] < l[2] ? l[0]: l[2];
int endX = l[0] > l[2] ? l[0]: l[2];
int startY = l[1] < l[3] ? l[1]: l[3];
int endY = l[1] > l[3] ? l[1]: l[3];
double slope = ((double)(l[1] - l[3])/(double)(l[0]-l[2]));
int intercept = l[1]-slope*l[0];
if(slope ==INFINITY){
for (int y = startY; y <= endY ; y++) {
matPoint->push_back(Vec2f(startX,y));
}
}
if (endX-startX > endY-startY) {
for (int x = startX; x <= endX ; x++) {
matPoint->push_back(Vec2f(x,(slope*x+intercept)));
}
}else{
for (int y = startY; y <= endY ; y++) {
matPoint->push_back(Vec2f(((y-intercept)/slope),y));
}
}
}
int findMaxLengthIndex(vector<Vec4i> lines){
double max = 0;
int index = 0;
for (int i = 0; i<lines.size(); i++) {
Vec4i l =lines.at(i);
double length = getLength(l);
if(length>max){
max = length;
index = i;
}
}
return index;
}
double getLength(Vec4i l){
return sqrt(pow(l[0]-l[2],2.0)+ pow(l[1]-l[3],2.0));
}
Point2f getIntersectionOfLines(Vec4i line1, Vec4i line2)
{
double a1 = (line1[1] - line1[3]) / (double)(line1[0]- line1[2]);
double b1 = line1[1] - a1 * line1[0];
double a2 = (line2[1] - line2[3]) / (double)(line2[0]- line2[2]);
double b2 = line2[1] - a2 * line2[0];
if (abs(a1 - a2) < 1e-8)
cout << "error";
double x = (b2 - b1) / (a1 - a2);
double y = a1 * x + b1;
return *new Point2f(x, y);
}
Point getDominantCordinate(Vec4i line, Point p)
{
double power1 = hypot(line[0]-p.x, line[1] - p.y);
double power2 =hypot(line[2]-p.x, line[3] - p.y);
if(power1>power2)
return Point(line[0],line[1]);
else
return Point(line[2],line[3]);
}
bool segmentIntersectRectangle(
double a_p1x,
double a_p1y,
double a_p2x,
double a_p2y, Mat src)
{
// Find min and max X for the segment
double a_rectangleMinX = src.cols/2 -20;
double a_rectangleMinY =src.rows/2 -20;
double a_rectangleMaxX = src.cols/2 +20;
double a_rectangleMaxY=src.rows/2 + 20;
double minX = a_p1x;
double maxX = a_p2x;
if(a_p1x > a_p2x)
{
minX = a_p2x;
maxX = a_p1x;
}
// Find the intersection of the segment's and rectangle's x-projections
if(maxX > a_rectangleMaxX)
{
maxX = a_rectangleMaxX;
}
if(minX < a_rectangleMinX)
{
minX = a_rectangleMinX;
}
if(minX > maxX) // If their projections do not intersect return false
{
return false;
}
// Find corresponding min and max Y for min and max X we found before
double minY = a_p1y;
double maxY = a_p2y;
double dx = a_p2x - a_p1x;
if(abs(dx) > 0.0000001)
{
double a = (a_p2y - a_p1y) / dx;
double b = a_p1y - a * a_p1x;
minY = a * minX + b;
maxY = a * maxX + b;
}
if(minY > maxY)
{
double tmp = maxY;
maxY = minY;
minY = tmp;
}
// Find the intersection of the segment's and rectangle's y-projections
if(maxY > a_rectangleMaxY)
{
maxY = a_rectangleMaxY;
}
if(minY < a_rectangleMinY)
{
minY = a_rectangleMinY;
}
if(minY > maxY) // If Y-projections do not intersect return false
{
return false;
}
return true;
}