forked from ugocapeto/thepainter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute_equivalent_rectangle.c
254 lines (204 loc) · 2.85 KB
/
compute_equivalent_rectangle.c
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
#include "header.h"
void compute_equivalent_rectangle(
double *image_arr,
int width,
int height,
double *pxc,
double *pyc,
double *ptheta,
double *prectw,
double *prectl,
int *perr_flag
)
/*
image_arr is a width*height grayscale image with values between 0.0 and 1.0
*/
/*
(xc,yc) is the center of the equivalent rectangle
w/r to origin of the image which is the top left
theta is the angle betwen the longer edge and the x-axis
w is the width of the equivalent rectangle
l is the length of the equivalent rectangle
*/
{
int l;
int m;
double moment;
double M00;
double M10;
double M01;
double M11;
double M20;
double M02;
double xc;
double yc;
double a;
double b;
double c;
double theta;
double temp_dbl;
double rectw;
double rectl;
double pi= acos(-1.0);
int err_flag;
xc= 0.0;
yc= 0.0;
theta= 0.0;
rectw= 0.0;
rectl= 0.0;
err_flag= 0;
/*
Compute M00
*/
l= 0;
m= 0;
compute_image_moment(
image_arr,
width,
height,
l,
m,
&moment
);
M00= moment;
/*
Compute M10
*/
l= 1;
m= 0;
compute_image_moment(
image_arr,
width,
height,
l,
m,
&moment
);
M10= moment;
/*
Compute M01
*/
l= 0;
m= 1;
compute_image_moment(
image_arr,
width,
height,
l,
m,
&moment
);
M01= moment;
/*
Compute M11
*/
l= 1;
m= 1;
compute_image_moment(
image_arr,
width,
height,
l,
m,
&moment
);
M11= moment;
/*
Compute M20
*/
l= 2;
m= 0;
compute_image_moment(
image_arr,
width,
height,
l,
m,
&moment
);
M20= moment;
/*
Compute M02
*/
l= 0;
m= 2;
compute_image_moment(
image_arr,
width,
height,
l,
m,
&moment
);
M02= moment;
/*
Compute center of equivalent rectangle (xc,yc)
*/
xc= M10/M00;
yc= M01/M00;
/*
Compute a
*/
a= M20/M00 - xc*xc;
/*
Compute b
*/
b= 2*(M11/M00 - xc*yc);
/*
Compute c
*/
c= M02/M00 - yc*yc;
/*
Compute theta (angle between longer edge and the x-axis)
*/
if ( (a-c) == 0.0 ) {
theta= 0.0;
}
else {
theta= atan(b / (a-c));
theta/= 2.0;
}
if ( (a-c) < 0.0 ) {
/*
Go from angle between longer edge and y-axis to
angle between longer edge and x-axis
See my sbr notes
*/
theta+= pi/2.0;
}
/*
Compute rectw (rectangle width)
*/
temp_dbl= b*b + (a-c)*(a-c);
temp_dbl= sqrt(temp_dbl);
rectw= 6.0*(a+c-temp_dbl);
if ( !(rectw >= 0.0) ) {
/*
error_handler((char *)"compute_equivalent_rectangle");
*/
err_flag= 1;
goto END;
}
rectw= sqrt(rectw);
/*
Compute rectl (rectangle length)
*/
temp_dbl= b*b + (a-c)*(a-c);
temp_dbl= sqrt(temp_dbl);
rectl= 6.0*(a+c+temp_dbl);
if ( !(rectl >= 0.0) ) {
/*
error_handler((char *)"compute_equivalent_rectangle");
*/
err_flag= 1;
goto END;
}
rectl= sqrt(rectl);
END:
(*pxc)= xc;
(*pyc)= yc;
(*ptheta)= theta;
(*prectw)= rectw;
(*prectl)= rectl;
(*perr_flag)= err_flag;
}