summaryrefslogtreecommitdiff
path: root/sci_gateway1/cpp/opencv_convolver.cpp
blob: 8ffc67bf5bf18b53d69a1b52c41dfe28a196da54 (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
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
/******************************************************************************************
*Author : Kevin George
*
*-> To execute, Image = convolver(I1, size, values,scalar)
*   where 'I1' is image to be convoluted,
*   where 'size' is size of kernel i.e size x size gives total no. of values in kernel,
*   where 'values' contains the values of the kernel 
*   where 'scalar' is a float value
*
*******************************************************************************************/
#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_convolver(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
        double *values1 = NULL;
        float *values2 =NULL;
        double size;
        double scalar;
        int iRows = 0; 
        int iCols = 0;
        int *outList = NULL;
        unsigned char *red = NULL;
        unsigned char *green = NULL;
        unsigned char *blue = NULL;

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

     	//-> Taking in size of kernel
        sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr); 
        if (sciErr.iErr)
        {
            printError(&sciErr, 0); 
            return 0; 
        }
                    
        //intErr = getScalarInteger32(pvApiCtx, piAddr, &size);
        intErr = getScalarDouble(pvApiCtx, piAddr, &size); 
        if(intErr)
        {
             return intErr; 
        }

        if( size > 5)
        {
              Scierror(999," Invalid Value size. Please enter a non negative Double value\\n");
              return 0;
        }   

        //-> Taking the kernel values
        sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddr); 
        if (sciErr.iErr)
        {
            printError(&sciErr, 0); 
            return 0; 
        }

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

        if(iRows*iCols!=9 && iRows*iCols!=16 && iRows*iCols!=25 )
        {   
    	    Scierror(999,"Invalid Argument\n");
            return 0;
        }           

       	values2 = (float*)malloc(sizeof(float)*size*size);
        for(int i=0; i<size*size; i++)
        {
            values2[i] = (float)values1[i];
        }

        
        //-> Taking scalar values
        sciErr = getVarAddressFromPosition(pvApiCtx, 4, &piAddr); 
        if (sciErr.iErr)
        {
            printError(&sciErr, 0); 
            return 0; 
        }

        intErr = getScalarDouble(pvApiCtx, piAddr, &scalar); 
        if(intErr)
        {
             return intErr; 
        }

        if( scalar == 0)
        {
              Scierror(999," Invalid scalar value. Please enter a non negative Double value\\n");
              return 0;
        }

        //float kdata[] = {1, 4, 6, -1, 3, 5, -1, -2, 2};
  		Mat kernel(size,size,CV_32F, values2);

  	    Mat kernel2 = kernel/ (float)(scalar);

        Point anchor;
        double delta;
        int ddepth;

        anchor = Point( -1, -1 );
        delta = 0;
        ddepth = -1;

        //filter2D(image_1, image_2, CV_32F, kernel);
        filter2D(image_1, image_2, ddepth,kernel2,anchor,delta, BORDER_DEFAULT);
  	    
  	    //imshow("Convoluted Image",image_2);
  	    //waitKey(0);

        if( image_2.channels() == 1)
        {
            sciErr = createList(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, &outList);
            if(sciErr.iErr)
            {
                printError(&sciErr, 0);
                return 0;
            }
            red = (unsigned char *)malloc(sizeof(unsigned char)*image_2.rows*image_2.cols);

            for(int k=0;k<image_2.rows;k++)
                for(int p=0;p<image_2.cols;p++)
                    red[k+image_2.rows*p]=image_2.at<uchar>(k, p);

            sciErr = createMatrixOfUnsignedInteger8InList(pvApiCtx, nbInputArgument(pvApiCtx) + 1, outList, 1, image_2.rows, image_2.cols, red);
            if(sciErr.iErr)
            {
                printError(&sciErr, 0);
                return 0;
            }                       
            free(red);
        }

        else
        {
            sciErr = createList(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 3, &outList);
            if(sciErr.iErr)
            {
                    printError(&sciErr, 0);
                    return 0;
            }

            red = (unsigned char *)malloc(sizeof(unsigned char)*image_2.rows*image_2.cols);
            green = (unsigned char *)malloc(sizeof(unsigned char)*image_2.rows*image_2.cols);
            blue = (unsigned char *)malloc(sizeof(unsigned char)*image_2.rows*image_2.cols);

            for(int k=0;k<image_2.rows;k++)
            {
                for(int p=0;p<image_2.cols;p++)
                {
                    Vec3b intensity = image_2.at<Vec3b>(k, p);
                    red[k+image_2.rows*p]=intensity.val[2];
                    green[k+image_2.rows*p]=intensity.val[1];
                    blue[k+image_2.rows*p]=intensity.val[0];
                }
            }

            sciErr = createMatrixOfUnsignedInteger8InList(pvApiCtx, nbInputArgument(pvApiCtx) + 1, outList, 1, image_2.rows, image_2.cols, red);
                if(sciErr.iErr)
                {
                    printError(&sciErr, 0);
                    return 0;
                }
                sciErr = createMatrixOfUnsignedInteger8InList(pvApiCtx, nbInputArgument(pvApiCtx) + 1, outList, 2, image_2.rows, image_2.cols, green);
                if(sciErr.iErr)
                {
                    printError(&sciErr, 0);
                    return 0;
                }                   
                sciErr = createMatrixOfUnsignedInteger8InList(pvApiCtx, nbInputArgument(pvApiCtx) + 1, outList, 3, image_2.rows, image_2.cols, blue);
                if(sciErr.iErr)
                {
                    printError(&sciErr, 0);
                    return 0;
                }
                free(red);
                free(green);
                free(blue); 

        }

        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
        ReturnArguments(pvApiCtx);
        return 0;
    }

}