summaryrefslogtreecommitdiff
path: root/sci_gateway/cpp/opencv_imwarp.cpp
blob: d5bc6df5940cc7ed70ce46f5adb6736838e06a6b (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
/********************************************************
    Author: V Srinivas
*********************************************************
   imwarp(image,matrix(2*3))=output.
********************************************************/

#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_imwarp(char *fname, unsigned long fname_len)
  {
    SciErr sciErr;
    int intErr=0;
    int iRows=0,iCols=0;
    int *piAddr1 = NULL;
    //int *piAddr2 = NULL;
    double *tform;
    int maxx,maxy,minx,miny;

    //checking input argument
    CheckInputArgument(pvApiCtx, 2, 2);//two arguments 1 takes the image and the other takes the transformation matrix
    CheckOutputArgument(pvApiCtx, 1, 1);
    //retieve Image
    Mat image;
    retrieveImage(image,1);
    //retreive the transformation matrix 
    sciErr = getVarAddressFromPosition(pvApiCtx,2,&piAddr1);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return -1;
    }
    sciErr = getMatrixOfDouble(pvApiCtx, piAddr1, &iRows, &iCols,&tform);
    if(sciErr.iErr)
    {
        printError(&sciErr, 0);
        return -1;
    }  
    if(iRows!=2 || iCols!=3)//checking if the transformation matrix is of the desired size
    {
        sciprint("Please enter a valid transformation matrix");
        return -1;
    }
    Mat dst;
    //This and the next few lines are dedicated to finding the size of the frame in which the whole transformed image can be viewed.
    double x [4]={0,0,image.cols-1,image.cols-1};
    double y [4]={0,image.rows-1,0,image.rows-1};
    int modx[4];
    int mody[4];
    for(int i=0;i<4;i++)// the modifications performed by the matrix is performed on the edges of the image to find the optimum size. This works as the transformation is linear so the edges always are on the extreme points. 
    {
        modx[i]=(int)(tform[0] * x[i] + tform[1] * y[i] + tform[4]);
        mody[i]=(int)(tform[2] * x[i] + tform[3] * y[i] + tform[5]);
        //cout<<*(modx+i)<<" "<<*(mody+i)<<endl;
    }
    Mat trans=(Mat_<double>(2,3)<<tform[0],tform[1],tform[4],tform[2],tform[3],tform[5]);//creating a matrix of type Mat.
    minx=modx[0];maxx=modx[0];miny=mody[0];maxy=mody[0];
    for(int i=0;i<4;i++)//used to find the maximum and minimum of image coordinates.
    {
        if(modx[i]<minx)
        {minx=modx[i];}
        if(modx[i]>maxx)
        {maxx=modx[i];}
        if(mody[i]<miny)
        {miny=mody[i];}
        if(mody[i]>maxy)
        {maxy=mody[i];}
    }
    warpAffine(image,dst,trans,Size(maxx-minx,maxy-miny));
    string tempstring = type2str(dst.type());
    char *checker;
    checker = (char *)malloc(tempstring.size() + 1);
    memcpy(checker, tempstring.c_str(), tempstring.size() + 1);
    returnImage(checker,dst,1); 
    free(checker);




    //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;
    


 }

}