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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
/********************************************************
Author : Deepshikha
[key_value] : blobAnalysis(source_image)
// 1 :image
Optional arguments :-
// 2 : filterByArea
// 3 : filterByThreshold
// 4 : filterByCircularity
// 5 : filterByConvexity
********************************************************/
#include <numeric>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/features2d/features2d.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"
int opencv_blobAnalysis(char *fname, unsigned long fname_len){
// Error management variable
SciErr sciErr;
//Variables
int i, j;
int intErr;
int iRows1 = 0;
int iCols1 = 0;
int iRows = 0;
int iCols = 0;
int *piLen = NULL;
int *piAddr = NULL;
char **pstData = NULL;
char *currentArg = NULL;
bool *providedArgs = NULL;
double *area = NULL;
double *threshold = NULL;
double *circularity = NULL;
double *convexity = NULL;
double *key_value = NULL;
// checking input argument
// 1 : image
// 2 : filterByArea
// 3 : filterByThreshold
// 4 : filterByCircularity
// 5 : filterByConvexity
CheckInputArgument(pvApiCtx, 1, 9);
CheckOutputArgument(pvApiCtx, 1, 1);
// retrieve image
Mat image;
retrieveImage(image, 1);
// For the optional arguments
int nbInputArguments = *getNbInputArgument(pvApiCtx);
//sciprint("%d\n",nbInputArguments);
if((nbInputArguments%2) == 0)
{
Scierror(999, "%d: The number of arguments must be odd\n");
return 0;
}
providedArgs = (bool*) malloc(sizeof(bool) * 4);
memset(providedArgs, 0, 4);
for(int iter = 2; iter <= nbInputArguments; ++iter)
{
sciErr = getVarAddressFromPosition(pvApiCtx, iter, &piAddr);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
/// Three calls to getMatrixOfString
//First Call
sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, NULL, NULL);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
// Second call to get length of string
piLen = (int*) malloc(sizeof(int) * iRows1 * iCols1);
sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows1, &iCols1, piLen, NULL);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
// third call
pstData = (char**) malloc(sizeof(char*) * iRows1 * iCols1);
for(int k = 0; k < iRows1 * iCols1; ++k)
{
pstData[k] = (char*) malloc(sizeof(char) * piLen[k] + 1);
}
sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows1, &iCols1, piLen, pstData);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
currentArg = pstData[0];
// getting filterbyArea
if(strcmp(currentArg, "filterByArea") == 0)
{
if(iter + 1 <= nbInputArguments and !providedArgs[0])
{
sciErr = getVarAddressFromPosition(pvApiCtx, ++iter, &piAddr);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &iRows1, &iCols1, &area);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
if(iRows1 != 1 or iCols1 != 2)
{
Scierror(999, "Incorrect dimension of matrix for argument .\n");
return 0;
}
providedArgs[0] = 1;
}
else if(providedArgs[0]) // Send an error message if an argument is provided more than once. Same for all optional arguments.
{
Scierror(999, "Please provide optional arguments only once.\n");
return 0;
}
else // Send an error message if name of argument is given but type is incorrect. Same for all optional arguments.
{
Scierror(999, "Incorrect number of arguments provided. Please check the documentation for more information.\n");
return 0;
}
}
/// filterbyThreshold
else if(strcmp(currentArg, "filterByThreshold") == 0)
{
if(iter+1 <= nbInputArguments && !providedArgs[1])
{
sciErr = getVarAddressFromPosition(pvApiCtx, ++iter, &piAddr);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &iRows1, &iCols1, &threshold);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
if(iRows1 != 1 or iCols1 != 2)
{
Scierror(999, "Incorrect dimension of matrix for argument .\n");
return 0;
}
// Checking if values are in proper range. Same for all optional arguments
providedArgs[1] = 1;
}
else if(providedArgs[1]) // Send an error message if an argument is provided more than once. Same for all optional arguments.
{
Scierror(999, "Please provide optional arguments only once.\n");
return 0;
}
else // Send an error message if name of argument is given but type is incorrect. Same for all optional arguments.
{
Scierror(999, "Incorrect number of arguments provided. Please check the documentation for more information.\n");
return 0;
}
}
/// filterbycircularity
else if(strcmp(currentArg, "filterByCircularity") == 0)
{
if(iter+1 <= nbInputArguments && !providedArgs[2])
{
sciErr = getVarAddressFromPosition(pvApiCtx, ++iter, &piAddr);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &iRows1, &iCols1, &circularity);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
if(iRows1 != 1 or iCols1 != 2)
{
Scierror(999, "Incorrect dimension of matrix for argument .\n");
return 0;
}
// Checking if values are in proper range. Same for all optional arguments
providedArgs[2] = 1;
}
else if(providedArgs[2]) // Send an error message if an argument is provided more than once. Same for all optional arguments.
{
Scierror(999, "Please provide optional arguments only once.\n");
return 0;
}
else // Send an error message if name of argument is given but type is incorrect. Same for all optional arguments.
{
Scierror(999, "Incorrect number of arguments provided. Please check the documentation for more information.\n");
return 0;
}
}
/// filterbyConvexity
else if(strcmp(currentArg, "filterByConvexity") == 0)
{
if(iter + 1 <= nbInputArguments and !providedArgs[3])
{
sciErr = getVarAddressFromPosition(pvApiCtx, ++iter, &piAddr);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &iRows1, &iCols1, &convexity);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
if(iRows1 != 1 or iCols1 != 2)
{
Scierror(999, "Incorrect dimension of matrix for argument .\n");
return 0;
}
providedArgs[3] = 1;
}
else if(providedArgs[3]) // Send an error message if an argument is provided more than once. Same for all optional arguments.
{
Scierror(999, "Please provide optional arguments only once.\n");
return 0;
}
else // Send an error message if name of argument is given but type is incorrect. Same for all optional arguments.
{
Scierror(999, "Incorrect number of arguments provided. Please check the documentation for more information.\n");
return 0;
}
}
else
{
Scierror(9,"Invalid argument passed");
return 0;
}
}
/// End of error check and input get;
/// params for holding the parameters values;
SimpleBlobDetector::Params params;
/// if area is provided
if(providedArgs[0] == 1){
params.filterByArea = true;
params.minArea = area[0];
params.maxArea = area[1];
}
/// if threshold is provided
if(providedArgs[1] == 1){
params.minThreshold = threshold[0];
params.maxThreshold = threshold[1];
}
/// if Circularity is provided
if(providedArgs[2] == 1){
params.filterByCircularity = true;
params.minCircularity = circularity[0];
params.maxCircularity = circularity[1];
}
/// if convexity is provided
if(providedArgs[3] == 1){
params.filterByConvexity = true;
params.minConvexity = convexity[0];
params.maxConvexity = convexity[1];
}
SimpleBlobDetector detector( params );
/// to store the keypoints of blobs detected
vector<KeyPoint> keyPoints;
/// fuction to detect blob in the image
detector.detect(image, keyPoints);
//sciprint("%d", keyPoints.size());
key_value = (double*)malloc(sizeof(double) * (int)keyPoints.size() * 2);
double *size_value = NULL;
size_value = (double*)malloc(sizeof(double) * (int)keyPoints.size() * 1);
int total_keypoints = (int)keyPoints.size();
vector< KeyPoint >::iterator it;
for(it = keyPoints.begin(); it != keyPoints.end(); ++it)
{
KeyPoint temp = keyPoints[i];
// x coordinate
key_value[i + 0 * total_keypoints] = it->pt.x;
// y coordinate
key_value[i + 1 * total_keypoints] = it->pt.y;
// size
size_value[i + 0 * total_keypoints] = it->size;
//area
//key_value[i + 3 * total_keypoints] = it->Area;
}
sciErr = createList(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 2, &piAddr);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
sciErr = createMatrixOfDoubleInList(pvApiCtx, nbInputArgument(pvApiCtx) + 1, piAddr, 1, total_keypoints, 2, key_value);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
sciErr = createMatrixOfDoubleInList(pvApiCtx, nbInputArgument(pvApiCtx) + 1, piAddr, 2, total_keypoints, 1, size_value);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
//Assigning the list as the Output Variable
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
//Returning the Output Variables as arguments to the Scilab environment
ReturnArguments(pvApiCtx);
return 0;
}
/* ==================================================================== */
}
|