summaryrefslogtreecommitdiff
path: root/sci_gateway1/cpp/opencv_undistortPoints.cpp
blob: dff80e41b2c7d37038be288cf8764456a7ac4389 (plain)
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
/***************************************************
Author : Deepshikha
***************************************************/

#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"
  
  int opencv_undistortPoints(char *fname, unsigned long fname_len)
  {
    //variables
    int i,j,k,n,m;
    int iRows=0,iCols=0;
    int *piAddr1  = NULL;
    int *piAddr2  = NULL;
    int *piAddr3  = NULL;
    int *piAddr4  = NULL;
    int *piAddr5  = NULL;
    
    double *pdblReal = NULL;

    SciErr sciErr;

    //function varible
    std::vector< Point2d > Src;
    std::vector< Point2d > dst;
    double cameraMatrix [3][3];
    double newCameraMatrix [3][3];
    double *distCoeffs = NULL;
    double RectificationMatrix[3][3];

    /*------------------------------------------- Checking Input Argument ----------------------------------------------------*/
    
    CheckInputArgument(pvApiCtx, 5, 5);
    CheckOutputArgument(pvApiCtx, 1, 1) ;

    /*------------------------------------- Source matrix :- First argument of the input -------------------------------------*/
    
    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }
    sciErr = getMatrixOfDouble(pvApiCtx, piAddr1, &iRows, &iCols, &pdblReal);
    if(sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }
    // For 1 X N single channel
    for(i = 0; i < iRows; ++i)
            Src.push_back( Point2d(pdblReal[(0 * iRows) + i], pdblReal[(1 * iRows) + i]));

    /* ------------------------------------ Camera matrix :- Second argument of the input -------------------------------------*/

    sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr2);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }
    sciErr = getMatrixOfDouble(pvApiCtx, piAddr2, &iRows, &iCols, &pdblReal);
    if(sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }
    Mat cameraMat(3, 3, CV_64F);
    for(i=0;i<3;i++)
        for(j=0;j<3;j++)
            cameraMat.at<double>(i,j) = pdblReal[(j * 3) + i];
    

    /* ------------------------------------ Distortion Coefficient matrix :- Third argument of the input -------------------------------------*/
    
    sciErr = getVarAddressFromPosition(pvApiCtx,3,&piAddr3);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }
    sciErr = getMatrixOfDouble(pvApiCtx, piAddr3, &iRows, &iCols, &pdblReal);
    if(sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    if(iRows== 1)
        n = iCols;
    else if (iCols == 1)
        n = iRows;
    else{
        Scierror(1,"Points matrix (arg 4) must be a 2 X N matrix");
        return 0;
    }
    if(n==4 or n==5 or n==8);
    else{
        Scierror(1," n must be 4 or 5 or 8");
        return 0;
    }

    Mat distCoeffsActual(1,n,CV_64F);
    for(i=0;i<n;i++)
        distCoeffsActual.at<double>(0,i) = pdblReal[i];


    /* ------------------------------------ Rectification matrix :- Fourth argument of the input -------------------------------------*/
    
    sciErr = getVarAddressFromPosition(pvApiCtx, 4, &piAddr4);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }
    sciErr = getMatrixOfDouble(pvApiCtx, piAddr4, &iRows, &iCols, &pdblReal);
    if(sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }
    Mat RectMat(3, 3, CV_64F);
    for(i=0;i<3;i++)
        for(j=0;j<3;j++)
            RectMat.at<double>(i,j) =pdblReal[(j*3)+i];
    

    /* ------------------------------------ New Camera matrix :- Fifth argument of the input -------------------------------------*/
    
    sciErr = getVarAddressFromPosition(pvApiCtx,5,&piAddr5);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }
    sciErr = getMatrixOfDouble(pvApiCtx, piAddr5, &iRows, &iCols , &pdblReal);
    if(sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    } 

    Mat newCameraMat(3,3,CV_64F);
    for(i=0;i<3;i++)
        for(j=0;j<3;j++)
            newCameraMat.at<double>(i, j) =pdblReal[(j*3)+i];  

    
    
    /* opencv function for undistortPoints */
    undistortPoints(Src, dst, cameraMat, distCoeffsActual, RectMat, newCameraMat);

    /* Writing to Scilab */
    double *pstdata = NULL;
    pstdata = (double*)malloc(sizeof(double) * dst.size() * 2);
    
    for(i = 0; i < dst.size(); i++)
    {
            pstdata[i + 0 * dst.size()] = dst[i].x;
            pstdata[i + 1 * dst.size()] = dst[i].y;
    }

    sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, dst.size(), 2, pstdata);
    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;

  }
/* ==================================================================== */
}