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
|
/***************************************************
Author : Rohit Suri
***************************************************/
#include <numeric>
#include <string.h>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <math.h>
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"
/* Calling syntax: detectMinEigenFeatures(I) or detectMinEigenFeatures(I, Name, Value)
which uses additional options specified by one or more Name, Value pair arguments.
Arguments Allowed: MinQuality, FilterSize, ROI
MinQuality : The minimum accepted quality of corners represents a fraction of the
maximum corner metric value in the image. Larger values can be used
to remove erroneous corners.
FilterSize : Gaussian filter dimension
ROI : Rectangular region for corner detection */
int opencv_detectMinEigenFeatures(char *fname, unsigned long fname_len)
{
// Error management variables
SciErr sciErr;
int intErr;
//------Local variables------//
int *location = NULL;
double *metric = NULL;
int *piAddr = NULL;
int *piLen = NULL;
int nbInputArguments;
char **pstData = NULL;
char *currentArg = NULL;
bool *providedArgs = NULL;
double *matrixOfRoi; // ROI[xStart, yStart, width, height]
int iRows, iCols;
Mat sourceImage, dst, dstThresholded, ucharDstThresholded, extended;
vector<vector<Point> > contours;
double filterSize = 5, minQuality = 0.01, blockSize = 2, maxDst, localMax;
bool *included = NULL;
int pointCount = 0, localMaxPos;
double tempForSwapping;
int coordinateMin, coordinatePos;
//------Check number of parameters------//
CheckInputArgument(pvApiCtx, 1, 7);
CheckOutputArgument(pvApiCtx, 1, 3);
//------Get input arguments------//
retrieveImage(sourceImage, 1);
if(sourceImage.channels() != 1)
{
Scierror(999, "The input image is not a grayscale image.");
return 0;
}
matrixOfRoi = (double*) malloc(sizeof(double) * 4);
providedArgs = (bool*) malloc(sizeof(bool) * 3);
memset(providedArgs, 0, 3);
matrixOfRoi[0] = 0;
matrixOfRoi[1] = 0;
matrixOfRoi[2] = sourceImage.cols;
matrixOfRoi[3] = sourceImage.rows;
nbInputArguments = *getNbInputArgument(pvApiCtx);
// The following loop is for checking if optional arguments have been provided
for(int iter=2; iter <= nbInputArguments; iter++)
{
// Getting address of next argument
sciErr = getVarAddressFromPosition(pvApiCtx, iter, &piAddr);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
// Extracting name of next argument takes three calls to getMatrixOfString
sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, NULL, NULL);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
piLen = (int*) malloc(sizeof(int) * iRows * iCols);
sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, NULL);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
pstData = (char**) malloc(sizeof(char*) * iRows * iCols);
for(int iterPstData = 0; iterPstData < iRows * iCols; iterPstData++)
{
pstData[iterPstData] = (char*) malloc(sizeof(char) * piLen[iterPstData] + 1);
}
sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, pstData);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
currentArg = pstData[0];
// providedArgs[] makes sure that no optional argument is provided more than once
if(strcmp(currentArg, "MinQuality")==0)
{
if(iter+1<=nbInputArguments && !providedArgs[0])
{
sciErr = getVarAddressFromPosition(pvApiCtx, ++iter, &piAddr);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
intErr = getScalarDouble(pvApiCtx, piAddr, &minQuality);
if(intErr)
{
return intErr;
}
// Checking if values are in proper range. Same for all optional arguments
if(minQuality < 0 || minQuality > 1)
{
Scierror(999, "Error: Please provide proper value for \"%s\". Permissible range is [0, 1].\n", currentArg);
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;
}
}
else if(strcmp(currentArg, "FilterSize")==0)
{
if(iter+1 <= nbInputArguments && !providedArgs[1])
{
sciErr = getVarAddressFromPosition(pvApiCtx, ++iter, &piAddr);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
intErr = getScalarDouble(pvApiCtx, piAddr, &filterSize);
if(intErr)
{
return intErr;
}
providedArgs[1] = 1;
if(filterSize!=1 && filterSize!=3 && filterSize!=5 && filterSize!=7)
{
Scierror(999, "Error: Please provide proper value for \"%s\". Permissible values are {1, 3, 5, 7}.\n", currentArg);
return 0;
}
}
else if(providedArgs[1])
{
Scierror(999, "Please provide optional arguments only once.\n");
return 0;
}
else
{
Scierror(999, "Incorrect number of arguments provided. Please check the documentation for more information.\n");
return 0;
}
}
else if(strcmp(currentArg, "ROI")==0)
{
if(iter+1 <= nbInputArguments && !providedArgs[2])
{
sciErr = getVarAddressFromPosition(pvApiCtx, ++iter, &piAddr);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
if(!isDoubleType(pvApiCtx, piAddr) || isVarComplex(pvApiCtx, piAddr))
{
Scierror(999, "%s: Wrong type for input argument #%d: A real matrix expected.\n", fname, iter);
return 0;
}
sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &iRows, &iCols, &matrixOfRoi);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
if(iRows!=1 || iCols!=4)
{
Scierror(999, "Incorrect dimension of matrix for argument ROI.\n");
return 0;
}
providedArgs[2] = 1;
if(matrixOfRoi[0] < 0 || matrixOfRoi[0] > sourceImage.cols || matrixOfRoi[1] < 0 ||
matrixOfRoi[1] > sourceImage.cols || matrixOfRoi[2] < 0 || matrixOfRoi[0] + matrixOfRoi[2] > sourceImage.cols
|| matrixOfRoi[3] < 0 || matrixOfRoi[1] + matrixOfRoi[3] > sourceImage.rows)
{
Scierror(999, "Error: Please provide proper values for \"%s\".\n", currentArg);
return 0;
}
}
else if(providedArgs[2])
{
Scierror(999, "Please provide optional arguments only once.\n");
return 0;
}
else
{
Scierror(999, "Incorrect number of arguments provided. Please check the documentation for more information.\n");
return 0;
}
}
else
{
Scierror(999, "Error: \"%s\" name for input argument is not valid.\n", currentArg);
return 0;
}
}
//------Actual processing------//
cornerMinEigenVal(sourceImage(Rect(matrixOfRoi[0], matrixOfRoi[1], matrixOfRoi[2], matrixOfRoi[3])), dst, blockSize, filterSize, BORDER_DEFAULT);
// Finding the maximum value at a pixel in dst
maxDst = 0;
for(int rowIter=0; rowIter < dst.rows; rowIter++)
{
for(int colIter=0; colIter < dst.cols; colIter++)
{
if(dst.at<float>(rowIter, colIter) > maxDst)
{
maxDst = dst.at<float>(rowIter, colIter);
}
}
}
/* First, we threshold dst according to the minQuality expected by the user.
We then find contours in the thresholded image and check each pixel's value
to compute the local maxima. A extended mat is created because contours
having edges on the boundaries of the images are not considered by
findContours */
threshold(dst, dstThresholded, maxDst*minQuality, 255, THRESH_BINARY);
dstThresholded.convertTo(ucharDstThresholded, CV_8UC1);
extended = Mat(dst.rows+2, dst.cols+2, CV_8UC1);
for(int rowIter = 0; rowIter < extended.rows; rowIter++)
{
for(int colIter = 0; colIter < extended.cols; colIter++)
{
if(rowIter == 0 || rowIter == extended.rows - 1
|| colIter == 0 || colIter == extended.cols - 1)
{
extended.at<uchar>(rowIter, colIter) = 0;
}
else
{
extended.at<uchar>(rowIter, colIter) = ucharDstThresholded.at<uchar>(rowIter-1, colIter-1);
}
}
}
findContours(extended, contours, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);
pointCount = contours.size();
location = (int*) malloc(sizeof(int) * pointCount * 2);
metric = (double*) malloc(sizeof(double)*pointCount);
for(int rowIter = 0; rowIter < contours.size(); rowIter++)
{
localMax = 0;
localMaxPos = 0;
for(int colIter=0; colIter<contours[rowIter].size(); colIter++)
{
if(dst.at<float>(contours[rowIter][colIter].y-1, contours[rowIter][colIter].x-1)>localMax)
{
localMax = dst.at<float>(contours[rowIter][colIter].y-1, contours[rowIter][colIter].x-1);
localMaxPos = colIter;
}
}
if(localMax > maxDst*minQuality)
{
location[rowIter] = contours[rowIter][localMaxPos].x-1 + matrixOfRoi[0];
location[pointCount + rowIter] = contours[rowIter][localMaxPos].y-1 + matrixOfRoi[1];
metric[rowIter] = dst.at<float>(location[pointCount + rowIter], location[rowIter]);
}
}
// To return coordinates in order
for(int iter1 = 0; iter1 < pointCount - 1; iter1++)
{
coordinateMin = location[iter1];
coordinatePos = iter1;
for(int iter2 = iter1 + 1; iter2 < pointCount; iter2++)
{
if(location[iter2] < coordinateMin)
{
coordinateMin = location[iter2];
coordinatePos = iter2;
}
else if(location[iter2] == coordinateMin)
{
if(location[pointCount + iter2] < location[pointCount + coordinatePos])
{
coordinateMin = location[iter2];
coordinatePos = iter2;
}
}
}
// Swapping x coordinate
tempForSwapping = location[coordinatePos];
location[coordinatePos] = location[iter1];
location[iter1] = tempForSwapping;
// Swapping y coordinate
tempForSwapping = location[pointCount + coordinatePos];
location[pointCount + coordinatePos] = location[pointCount + iter1];
location[pointCount + iter1] = tempForSwapping;
// Swapping metric
tempForSwapping = metric[coordinatePos];
metric[coordinatePos] = metric[iter1];
metric[iter1] = tempForSwapping;
}
//------Create output arguments------//
sciErr = createMatrixOfInteger32(pvApiCtx, nbInputArgument(pvApiCtx)+1, pointCount, 2, location);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx)+2, pointCount, 1, metric);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
sciErr = createMatrixOfInteger32(pvApiCtx, nbInputArgument(pvApiCtx)+3, 1, 1, &pointCount);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
//------Return Arguments------//
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx)+1;
AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx)+2;
AssignOutputVariable(pvApiCtx, 3) = nbInputArgument(pvApiCtx)+3;
ReturnArguments(pvApiCtx);
return 0;
}
/* ==================================================================== */
}
|