summaryrefslogtreecommitdiff
path: root/sci_gateway1/cpp/opencv_isEpipoleInImage.cpp
blob: 86630320ee55087c6ee58a7056b2e64d2cf22952 (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
/********************************************************
Author: Suraj Prakash
[isepi, epipole] = isEpipoleInImage(fundamental_matrix, imagesize)
********************************************************/

#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_isEpipoleInImage(char *fname, unsigned long fname_len){

    /// Error management variable
    SciErr sciErr;
    
    /// Variables
    int i, j, n = 0;

    int iRows = 0;
    int iCols = 0;
    
    int *piLen = NULL;
    int *piAddr = NULL;
    int *piAddr2 = NULL;
    
    double *pdblReal = NULL;
    double *imagesize = NULL;
    
    int isepi = 0;
    double *epipole = NULL;
    double inf = 1e17;
    /// take the fundamental matrix 
    Mat fundamental_matrix(3, 3, CV_64F);

    epipole = (double*)malloc(sizeof(double*) * 2);
    memset(epipole, 0, 2);
   
    sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
    if (sciErr.iErr){
        printError(&sciErr, 0);
        return 0;
    }

    sciErr = getMatrixOfDouble(pvApiCtx, piAddr, &iRows, &iCols, &pdblReal);
    if(sciErr.iErr)
    {
        printError(&sciErr, 0);
        return 0;
    }

    if(iRows != 3 and iCols != 3){
        Scierror(999, "Incorrect dimension of martrix for argument\n"); 
        return 0; 
    }

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


    /// Get image size    
    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;
    }

    if(iRows != 1 and iCols != 2){
        Scierror(999, "Invalid array for imagesize provided\n"); 
        return 0; 
    }

    imagesize = (double*)malloc(sizeof(double) * 2);
    imagesize[0] = pdblReal[0];
    imagesize[1] = pdblReal[1];

    /// compute the SVD of the matrix provided
    Mat w, u, vt;
    SVD::compute(fundamental_matrix, w, u, vt);

    transpose(vt, vt);

    /// getting the last column of the vt
    Mat epipoleHmg(1, 3, CV_64F);

    epipoleHmg.at<double>(0,0) = vt.at<double>(0, 2);
    epipoleHmg.at<double>(0,1) = vt.at<double>(1, 2);
    epipoleHmg.at<double>(0,2) = vt.at<double>(2, 2);


    if(epipoleHmg.at<double>(0,2) != 0){
        /// location of epipolecl

        epipole[0] = epipoleHmg.at<double>(0, 0) / epipoleHmg.at<double>(0, 2);
        epipole[1] = epipoleHmg.at<double>(0, 1) / epipoleHmg.at<double>(0, 2);

        double *imageOrigin = NULL;
        imageOrigin = (double*)malloc(sizeof(double) * 2);
        memset(imageOrigin, 0.5, 2);

        double *imageEnd = NULL;
        imageEnd = (double*)malloc(sizeof(double) * 2);
        memset(imageEnd, 0, 2);

        /// imageend = imageorigin + imagesize(::-1)
        imageEnd[0] = imageOrigin[0] + imagesize[1];
        imageEnd[1] = imageOrigin[1] + imagesize[0];

        if(epipole[0] >= imageOrigin[0] and epipole[1] >= imageOrigin[1] and epipole[0] <= imageEnd[0] and epipole[1] <= imageEnd[1])
            isepi = 1;
        else
            isepi = 0;
    }

    else{
        epipole[0] = epipoleHmg.at<double>(0, 0) > 0 ? inf : -inf;
        epipole[1] = epipoleHmg.at<double>(0, 1) > 0 ? inf : -inf;
    }

    
     


    int iRet = createScalarBoolean(pvApiCtx, nbInputArgument(pvApiCtx) + 1, isepi);
    if(iRet){
        printError(&sciErr, 0); 
        return 0; 
    }
    
    sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, 1, 2, epipole); 
    if(sciErr.iErr){
        printError(&sciErr, 0); 
        return 0; 
    }
    //Assigning the list as the Output Variable
    AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
    AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2; 

    //Returning the Output Variables as arguments to the Scilab environment
    ReturnArguments(pvApiCtx);
    return 0;
    }
    
/* ==================================================================== */
}