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
|
/***************************************************
Author : Tanmay Chaudhari
***************************************************/
#include <numeric>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
extern "C"
{
#include "api_scilab.h"
#include "Scierror.h"
#include "BOOL.h"
#include <localization.h>
#include "sciprint.h"
// #include "../common.h"
// #include "../common.cpp"
int opencv_bboxOverlapRatio(char *fname, unsigned long fname_len)
{
//Error management variable
SciErr sciErr;
//Variable declaration
int i, j;
int nbInputArguments = 0;
int iComplex = 0;
int iType = 0;
int iRows = 0;
int iCols = 0;
int rowsOfBboxA = 0;
int colsOfBboxA = 0;
int rowsOfBboxB = 0;
int colsOfBboxB = 0;
int unionOrMin = 0; //if 0 then union else min
int *piAddr = NULL;
int *piLen = NULL;
char **ratioType = NULL;
double x1 = 0;
double x2 = 0;
double y1 = 0;
double y2 = 0;
double w = 0;
double h = 0;
double intersectAB = 0;
double *overlapRatio = 0;
double *bboxA = NULL;
double *bboxB = NULL;
double *x1BboxA = NULL;
double *x2BboxA = NULL;
double *y1BboxA = NULL;
double *y2BboxA = NULL;
double *x1BboxB = NULL;
double *x2BboxB = NULL;
double *y1BboxB = NULL;
double *y2BboxB = NULL;
double *areaA = NULL;
double *areaB = NULL;
//Check input output arguments
checkInputArgument(pvApiCtx, 2, 3);
checkOutputArgument(pvApiCtx, 1, 1);
//Get number of input arguments
nbInputArguments = *getNbInputArgument(pvApiCtx);
for( i = 1; i <= nbInputArguments; i++)
{
//Get variable address of the input arguent
sciErr = getVarAddressFromPosition(pvApiCtx, i, &piAddr);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
if(i != 3)
{
//Check type
sciErr = getVarType(pvApiCtx, piAddr, &iType);
if(sciErr.iErr || iType != sci_matrix)
{
printError(&sciErr, 0);
return 0;
}
//Get complexity
iComplex = isVarComplex(pvApiCtx, piAddr);
//Check complexity
if(iComplex)
{
Scierror(999, "%s: Wrong type for input argument: A complex number is not expected.\n");
return 0;
}
if(i == 1)
sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &rowsOfBboxA, &colsOfBboxA, &bboxA);
else
sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &rowsOfBboxB, &colsOfBboxB, &bboxB);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
if((i == 1) && ((rowsOfBboxA == 0) || (colsOfBboxA !=4)))
{
printError(&sciErr, 0);
return 0;
}
if((i == 2) && ((rowsOfBboxB == 0) || (colsOfBboxB != 4)))
{
printError(&sciErr, 0);
return 0;
}
}
else
{
//first call to retrieve dimensions
sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, NULL, NULL);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
piLen = (int*)malloc(sizeof(int) * iRows * iCols);
//second call to retrieve the length of the string
sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, NULL);
if(sciErr.iErr || iRows != 1 || iCols != 1)
{
printError(&sciErr, 0);
return 0;
}
ratioType = (char**)malloc(sizeof(char*) * iRows * iCols);
for(int i=0;i< iRows * iCols; i++)
{
ratioType[i] = (char*)malloc(sizeof(char) * (piLen[i] + 1)); //+1 for NULL character
}
//third call to retrieve data
sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, ratioType);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
if(piLen[0] != 5 && piLen[0] != 3)
{
printError(&sciErr, 0);
return 0;
}
if(piLen[0] == 5 && ratioType[0][0] == 'U' && ratioType[0][1] == 'n' && ratioType[0][2] == 'i' && ratioType[0][3] == 'o' && ratioType[0][4] == 'n')
unionOrMin = 0;
else if(piLen[0] == 3 && ratioType[0][0] == 'M' && ratioType[0][1] == 'i' && ratioType[0][2] == 'n')
unionOrMin = 1;
else
{
Scierror(999, "%s: Wrong type for input argument: Union or Min is expected.\n");
return 0;
}
free(piLen);
free(ratioType);
}
}
x1BboxA = (double*)malloc(sizeof(double) * rowsOfBboxA);
x2BboxA = (double*)malloc(sizeof(double) * rowsOfBboxA);
y1BboxA = (double*)malloc(sizeof(double) * rowsOfBboxA);
y2BboxA = (double*)malloc(sizeof(double) * rowsOfBboxA);
x1BboxB = (double*)malloc(sizeof(double) * rowsOfBboxB);
x2BboxB = (double*)malloc(sizeof(double) * rowsOfBboxB);
y1BboxB = (double*)malloc(sizeof(double) * rowsOfBboxB);
y2BboxB = (double*)malloc(sizeof(double) * rowsOfBboxB);
areaA = (double*)malloc(sizeof(double) * rowsOfBboxA);
areaB = (double*)malloc(sizeof(double) * rowsOfBboxB);
overlapRatio = (double*)malloc(sizeof(double) * rowsOfBboxA * rowsOfBboxB);
for( i = 0; i < rowsOfBboxA; i++)
{
//Left top corner
x1BboxA[i] = bboxA[i];
y1BboxA[i] = bboxA[rowsOfBboxA + i];
//right bottom corner
x2BboxA[i] = x1BboxA[i] + bboxA[2 * rowsOfBboxA + i];
y2BboxA[i] = y1BboxA[i] + bboxA[3 * rowsOfBboxA + i];
}
for( i = 0; i < rowsOfBboxB; i++)
{
//Left top corner
x1BboxB[i] = bboxB[i];
y1BboxB[i] = bboxB[rowsOfBboxB + i];
//right bottom corner
x2BboxB[i] = x1BboxB[i] + bboxB[2 * rowsOfBboxB + i];
y2BboxB[i] = y1BboxB[i] + bboxB[3 * rowsOfBboxB + i];
}
//Computing area for each rectangle in bboxA
for( i = 0; i < rowsOfBboxA; i++)
areaA[i] = bboxA[2 * rowsOfBboxA + i] * bboxA[3 * rowsOfBboxA + i];
//Computing area for each rectangle in bboxB
for( i = 0 ; i < rowsOfBboxB; i++)
areaB[i] = bboxB[2 * rowsOfBboxB + i] * bboxB[3 * rowsOfBboxB + i];
for( i = 0; i < rowsOfBboxA * rowsOfBboxB; i++)
overlapRatio[i] = 0;
for( i = 0; i < rowsOfBboxA; i++)
{
for( j = 0; j < rowsOfBboxB; j++)
{
//Computing the corners of the intersect
x1 = (x1BboxA[i] > x1BboxB[j])?x1BboxA[i]:x1BboxB[j];
y1 = (y1BboxA[i] > y1BboxB[j])?y1BboxA[i]:y1BboxB[j];
x2 = (x2BboxA[i] < x2BboxB[j])?x2BboxA[i]:x2BboxB[j];
y2 = (y2BboxA[i] < y2BboxB[j])?y2BboxA[i]:y2BboxB[j];
w = x2 - x1;
if(w <= 0) //Skip if no intersection
continue;
h = y2 - y1;
if(h <= 0) //Skip if no intersection
continue;
intersectAB = w * h;
if(nbInputArguments == 2)
overlapRatio[j * rowsOfBboxA + i] = intersectAB/(areaA[i] + areaB[j] - intersectAB);
else
{
if(unionOrMin == 0)
overlapRatio[j * rowsOfBboxA + i] = intersectAB/(areaA[i] + areaB[j] - intersectAB);
else
overlapRatio[j * rowsOfBboxA + i] = intersectAB/((areaA[i] < areaB[j])?areaA[i]:areaB[j]);
}
}
}
sciErr = createList(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, &piAddr);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
sciErr = createMatrixOfDoubleInList(pvApiCtx, nbInputArgument(pvApiCtx) + 1, piAddr, 1, rowsOfBboxA, rowsOfBboxB, overlapRatio);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
//Return Output Argument
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
ReturnArguments(pvApiCtx);
return 0;
}
/* ==================================================================== */
}
|