-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
153 lines (127 loc) · 4.84 KB
/
main.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
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <stdio.h>
using namespace std;
using namespace cv;
#define circle_width 3
#define line_width 2
#define X_size 1400
#define Y_size 1050
#define offset 700
#define pi 3.1415926
Mat image, img2;
bool mouse_event = 0;
void CallBackFunc(int event, int x, int y, int flags, void *ptr){
Point*p = (Point*)ptr;
if(event == EVENT_LBUTTONDOWN){
p->x = x;
p->y = y;
circle(img2, Point(x,y), circle_width, Scalar(0,0,255),FILLED);
mouse_event = 1;
}
}
double gradient(Point pt1, Point pt2){
/*Calculate the slope of two points*/
double m;
m = double(pt2.y - pt1.y)/double(pt2.x - pt1.x);
return m;
}
double rad2deg(double rad){
/*Convert radians to degrees*/
double deg = 0;
deg = rad * (180.0/pi);
return deg;
}
int main(){
bool end = 0, status;
char wait_char;
double m1 = 0, l_x, l_y, angR, angD;
uint8_t i = 0;
String source = "img/source/",
result = "img/result/",
filename = "P070",
filetype = ".JPG",
path = source+filename+filetype;
Point pt, pnt[6];
image = imread(path,IMREAD_COLOR);
if(!image.data){
printf("Could not open file\n");
return -1;
}
resize(image, img2, Size(X_size,Y_size), INTER_LINEAR);
while(!end){
namedWindow(filename+filetype, WINDOW_NORMAL);
imshow(filename+filetype, img2);
setMouseCallback(filename+filetype,CallBackFunc, &pt);
if(mouse_event){
if(i<6){
if(i == 0){
pnt[0].x = pt.x; pnt[0].y = pt.y;
pnt[1].x = pt.x; pnt[1].y = pt.y - offset;
line(img2, pnt[0],pnt[1], Scalar(0,0,255),line_width);
circle(img2, pnt[i+1], circle_width, Scalar(0,0,255),FILLED);
printf("Point %d: %d %d \n",0,pnt[0].x,pnt[0].y);
printf("Point %d: %d %d \n",1,pnt[1].x,pnt[1].y);
i += 2;
}
else if(i == 2){
pnt[2].x = pt.x; pnt[2].y = pt.y;
printf("Point %d: %d %d \n",2,pnt[2].x,pnt[2].y);
i++;
}
else if(i == 3){
pnt[3].x = pt.x; pnt[3].y = pt.y;
pnt[4].x = round((pnt[3].x + pnt[2].x)/2);
pnt[4].y = round((pnt[3].y + pnt[2].y)/2);
m1 = -1/gradient(pnt[3],pnt[2]);
pnt[5].y = round(((pnt[0].x - pnt[4].x)*m1) + pnt[4].y);
pnt[5].x = pnt[0].x;
line(img2, pnt[2],pnt[3], Scalar(0,0,255),line_width);
circle(img2, pnt[4], circle_width, Scalar(0,0,255),FILLED);
line(img2, pnt[5],pnt[4], Scalar(0,0,255),line_width);
circle(img2, pnt[5], circle_width, Scalar(0,0,255),FILLED);
printf("Point %d: %d %d \n",3,pnt[3].x,pnt[3].y);
printf("Point %d: %d %d \n",4,pnt[4].x,pnt[4].y);
printf("Point %d: %d %d \n",5,pnt[5].x,pnt[5].y);
/*Calculate angle of the tip*/
l_x = abs(double(pnt[0].x - pnt[4].x));
l_y = abs(double(pnt[5].y - pnt[4].y));
angR = atan(l_x/l_y);
angD = rad2deg(angR);
/*Quadrant correction measured from the neutral axis of the robot*/
if(pnt[4].x < pnt[0].x){
if(m1<0) angD = 180 - angD;
}
else if (pnt[4].x > pnt[0].x){
if(m1>0) angD = 180 + angD;
else angD = 360 - angD;
}
printf("Angle: %lf\n",angD);
angD = int(round(angD)) % 360;
/*Put the text to the window*/
putText(img2, to_string(int(angD)), Point(pnt[4].x+75,pnt[4].y-10), FONT_HERSHEY_COMPLEX, 1.5, Scalar(0,0,0),2);
i+=3;
}
}
mouse_event = 0;
}
wait_char = waitKey(10);
if(wait_char == 's'){
/*Save the result*/
status = imwrite(result+filename+"_final"+filetype, img2);
printf("Image written to file-system : %d\n",status);
}
else if(wait_char == 'a'){
/*Clear the window*/
i = 0;
image = imread(path,cv::IMREAD_COLOR);
resize(image, img2, Size(X_size,Y_size), INTER_LINEAR);
}
else if(wait_char == 'q'){
/*Exit*/
destroyAllWindows();
end = 1;
}
}
return 0;
}