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
|
/*********************************************************************************
*Author : Kevin George
*
*-> To execute, estimateGeometricTransform(I1,I2)
* where I1 & I2 are images
*
*This program only gives an affine transformation matrix
*********************************************************************************/
#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 "opencv2/features2d/features2d.hpp"
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include <iostream>
#include <math.h>
#include <vector>
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_estimateGeometricTransform(char *fname, unsigned long fname_len)
{
//-> Error Management variables
SciErr sciErr;
int intErr=0;
//-> Mat containers for images
Mat image_1;
Mat image_2;
//-> Address of Various Arguments
int *piAddr = NULL;
//-> Local variables
int minHessian = 400; //-> For SURF algorithm
int num_InputArgs;
//-> KeyPoint is data structure to store a point feature
// detected by a keypoint detector(OpenCV)
vector<KeyPoint> keypoints_1;
vector<KeyPoint> keypoints_2;
Mat descriptors_1, descriptors_2;
Mat img_matches;
vector< DMatch > good_matches;
vector< DMatch > matches;
vector<Point2f> a,b;
double max_dist = 0; double min_dist = 100;
/*double *indexPairs = NULL;
double *matchMetric = NULL;
double size=1;
int iRows, iCols; */
//KeyPoint *X = new KeyPoint; //-> Remember to free this Kevin
//-> Checks the number of arguments
//-> pvApiCtx is a Scilab environment pointer
CheckInputArgument(pvApiCtx, 1, 5); //Check on Number of Input Arguments
CheckOutputArgument(pvApiCtx, 1, 5); //Check on Number of Output Arguments
//-> Read Image
retrieveImage( image_1, 1);
retrieveImage(image_2, 2);
//-> Count number of input arguments
//num_InputArgs = *getNbInputArgument(pvApiCtx);
//-> Based on number of input arguments
//***************************************************** Actual Processing *************************************************************
//-- Step 1: Calculate keypoints
SurfFeatureDetector detector( minHessian );
detector.detect( image_1, keypoints_1 );
detector.detect( image_2, keypoints_2 );
//-- Step 2: Calculate descriptors (feature vectors)
SurfDescriptorExtractor extractor;
extractor.compute( image_1, keypoints_1, descriptors_1 );
extractor.compute( image_2, keypoints_2, descriptors_2 );
//-- Step 3: Matching descriptor vectors using FLANN matcher
FlannBasedMatcher matcher;
matcher.match( descriptors_1, descriptors_2, matches );
//-- Quick calculation of max and min distances between keypoints
for( int i = 0; i < descriptors_1.rows; i++ )
{
double dist = matches[i].distance;
if( dist < min_dist ) min_dist = dist;
if( dist > max_dist ) max_dist = dist;
}
for( int i = 0; i < descriptors_1.rows; i++ )
{
if( matches[i].distance <= max(2*min_dist, 0.02) )
{ good_matches.push_back( matches[i]); }
}
//queryidx- keypoints1
//traindidx- keypoints2
for(int i=0;i<3;i++)
{
a.push_back( keypoints_1[good_matches[i].queryIdx].pt );
b.push_back( keypoints_2[good_matches[i].trainIdx].pt );
}
Mat M = getAffineTransform( Mat(a), Mat(b) );
/* double output[M.rows][M.cols];
for(int i=0; i<M.rows; ++i)
{
const double* Mi = M.ptr<double>(i);
for (int j = 0; j < M.cols; j++)
{
output[i][j] = Mi[j];
}
} */
double *output = NULL;
output = (double *)malloc(sizeof(double)*M.rows*M.cols);
//-> Accessing elements of Mat object M
for(int i=0; i<M.rows; ++i)
{
const double* Mi = M.ptr<double>(i);
for (int j = 0; j < M.cols; j++)
{
output[i+j] = Mi[j];
}
}
sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx)+1, M.rows, M.cols, output);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
//-> Returning Output
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx)+1;
ReturnArguments(pvApiCtx);
return 0;
}
}
|