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
|
/****************************************************************************************************
estimateFundamentalMatrix(InputArray1,InputArray2,'method','RANSAC','param1',2,'param2',0.99);
possible methods are 7POINT,8POINT,RANSAC,LMEDS
param1- It indiacates maximum distance from a point to an epipolar line in pixels
param2- It specifies a desirable level of confidence
********************************************************* ***********************************************/
#include <numeric>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.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_estimateFundamentalMat(char *fname, unsigned long fname_len)
{
SciErr sciErr;
int intErr=0;
int iComplex = 0;
int iType = 0;
int *piAddr = NULL;
int *piLen = NULL;
int iRows,iCols;
int iA1Rows=0,iA1Cols=0,iA2Rows=0,iA2Cols=0;
int method=FM_RANSAC;
int nbInputArguments,iter,i,j;
char **pstData = NULL;
char *currentArg = NULL;
bool *providedArgs = NULL;
double* pdblReal = NULL;
double *iArray1=NULL,*iArray2=NULL;
double param1=3,param2=0.99;
vector<Point2f> point1,point2;
CheckInputArgument(pvApiCtx, 2, 8);
CheckOutputArgument(pvApiCtx, 1, 1) ;
nbInputArguments = *getNbInputArgument(pvApiCtx);
providedArgs = (bool*) malloc(sizeof(bool) * 3);
sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
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;
}
sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &iA1Rows, &iA1Cols, &iArray1);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
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;
}
sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &iA2Rows, &iA2Cols, &iArray2);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
if(iA1Rows != iA2Rows)
{
Scierror(999, "There must be the same number of points in both input arrays\n");
return 0;
}
for(i=0;i<iA1Rows;i++){
point1.push_back(Point2f(iArray1[i],iArray1[iA1Rows+i]));
point2.push_back(Point2f(iArray2[i],iArray2[iA2Rows+i]));
}
for(iter=3; iter<= nbInputArguments; iter++)
{
sciErr = getVarAddressFromPosition(pvApiCtx, iter, &piAddr);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
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];
if(strcmp(currentArg, "method")==0)
{
//sciprint("in if\tcurrentArg:%s\n",currentArg);
if(iter+1<=nbInputArguments && !providedArgs[0])
{
sciErr = getVarAddressFromPosition(pvApiCtx, ++iter, &piAddr);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
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];
// Checking if values are in proper range. Same for all optional arguments
//sciprint("in if\tcurrentArg:%s\n",currentArg);
if(strcmp(currentArg,"7POINT")==0)
method = FM_7POINT;
else if (strcmp(currentArg,"8POINT")==0)
method= FM_8POINT;
else if (strcmp(currentArg,"RANSAC")==0)
method= FM_RANSAC;
else if (strcmp(currentArg,"LMEDS")==0)
method= FM_LMEDS;
else
{
sciprint("Invalid method given. RANSAC was used instead");
method= FM_RANSAC;
}
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, "param1")==0)
{
if(iter+1 <= nbInputArguments && !providedArgs[1])
{
sciErr = getVarAddressFromPosition(pvApiCtx, ++iter, &piAddr);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
intErr = getScalarDouble(pvApiCtx, piAddr, ¶m1);
if(intErr)
{
return intErr;
}
providedArgs[1] = 1;
}
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, "param2")==0)
{
if(iter+1 <= nbInputArguments && !providedArgs[2])
{
sciErr = getVarAddressFromPosition(pvApiCtx, ++iter, &piAddr);
if (sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
intErr = getScalarDouble(pvApiCtx, piAddr, ¶m2);
if(intErr)
{
return intErr;
}
providedArgs[2] = 1;
}
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;
}
}
if (method==FM_7POINT && point1.size()!=7){
Scierror(999, "Error: For FM_7POINT method number of points in each array must be 7\n");
return 0;
}
if (method!=FM_7POINT && point1.size()<8 ){
Scierror(999, "Error: Number of points in each array must be greater than or equal to 8\n");
return 0;
}
Mat fm=cv::findFundamentalMat(point1, point2, method, param1, param2);
pdblReal = (double*) malloc(sizeof(double) * fm.rows *3);
for(i = 0 ; i < fm.rows ; i++)
{
for(j = 0 ; j < 3 ; j++)
{
pdblReal[i + fm.rows * j] = fm.at<double>(i,j);
}
}
sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, fm.rows , 3, pdblReal);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
ReturnArguments(pvApiCtx);
return 0;
}
}
|