summaryrefslogtreecommitdiff
path: root/sci_gateway1/cpp/opencv_integralImage.cpp
blob: b40e268a71af09b4a75890919993810b80737e44 (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
/***************************************************
Author : Tanmay Chaudhari
***************************************************/

#include <numeric>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include <bits/stdc++.h>
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_integralImage(char *fname, unsigned long fname_len)
{
	//Error management variable
	SciErr sciErr;
	
	//Variable declaration
	int nbInputArguments = 0;
	int iRows = 0;
	int iCols = 0;
	int *piLen = NULL;
	int *piAddr = NULL;
	int tilted = 0;
	char **pstData  = NULL;
	double *integralImage;
	Mat image, sum, sqsum, tiltedsum;

	//Check input output arguments
	checkInputArgument(pvApiCtx, 1, 2);
	checkOutputArgument(pvApiCtx, 1, 1);

	nbInputArguments = *getNbInputArgument(pvApiCtx);
	
	//getting input arguments
	retrieveImage(image, 1); 

	if(nbInputArguments == 2)
	{
		sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr); 
		if (sciErr.iErr)
		{
			printError(&sciErr, 0); 
			return 0; 
		}

		// Extracting name of next argument takes three calls to getMatrixOfString
		sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, NULL, NULL); 
		if (sciErr.iErr)
		{
			printError(&sciErr, 0); 
			return 0; 
		}

		piLen = (int*) malloc(sizeof(int) * iRows * iCols); 

		sciErr = getMatrixOfString(pvApiCtx,  piAddr,  &iRows,  &iCols,  piLen,  NULL); 
		if (sciErr.iErr)
		{
			printError(&sciErr, 0); 
			return 0; 
		}

		pstData = (char**) malloc(sizeof(char*) * iRows * iCols); 
		for(int iterPstData = 0; iterPstData < iRows * iCols; iterPstData++)
		{
			pstData[iterPstData] = (char*) malloc(sizeof(char) * piLen[iterPstData] + 1); 
		}

		sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, pstData); 
		if (sciErr.iErr)
		{
			printError(&sciErr, 0); 
			return 0; 
		}

		if(strcmp(pstData[0],"upright")==0)
			tilted=0;
		else if(strcmp(pstData[0],"rotated")==0)
			tilted=1;
		else
		{
			Scierror(999, "Error: Please provide proper value for \"%s\". Permissible values are upright or rotated.\n", pstData[0]); 
			return 0; 
		}
	}

	integral(image,sum,sqsum,tiltedsum);

	if(tilted==0)
	{
		integralImage = (double*)malloc(sizeof(double) * int(sum.rows) * int(sum.cols));
		for(int i=0;i<sum.rows;i++)
		{
			for(int j=0;j<sum.cols;j++)
			{
				integralImage[j*int(sum.rows)+i] = sum.at<int>(i,j);
			}
		}
	}
	else
	{
		integralImage = (double*)malloc(sizeof(double) * int(sum.rows) * (int(sum.cols)+1));
		for(int i=0;i<sum.rows;i++)
		{
			for(int j=0;j<=sum.cols;j++)
			{
				integralImage[j*sum.rows+i]=0;
			}
		}
		for(int i=0;i<sum.rows;i++)
		{
			for(int j=0;j<sum.cols;j++)
			{
					integralImage[j*int(sum.rows)+i] = tiltedsum.at<int>(i,j);
			}
		}
	}

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

	if(tilted==0)
		sciErr = createMatrixOfDoubleInList(pvApiCtx, nbInputArgument(pvApiCtx) + 1, piAddr, 1, int(sum.rows), int(sum.cols), integralImage);
	else
		sciErr = createMatrixOfDoubleInList(pvApiCtx, nbInputArgument(pvApiCtx) + 1, piAddr, 1, int(sum.rows), int(sum.cols)+1, integralImage);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}

	//Return output arguments
	AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
	ReturnArguments(pvApiCtx);
	
	return 0;
}
/* ==================================================================== */
}