summaryrefslogtreecommitdiff
path: root/sci_gateway/cpp/opencv_fsamp2.cpp
blob: 02780b66f872cd9f3a904986d50cce01770cbba9 (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
/********************************************************
Author: Vinay

Function: ind2gray(image, colormap)
********************************************************/

#include <numeric>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;

void rotate180(Mat &m) {
    double temp;
    for (int i=0; i<(m.rows+1)/2; i++) {
        int k = m.cols;
        if ((i+1)>=((m.rows+1)/2) && m.rows%2!=0) {
            k = (m.cols+1)/2;
        }
        for (int j=0; j<k; j++) {
            temp = m.at<double>(i, j);
            m.at<double>(i, j) = m.at<double>(m.rows-i-1, m.cols-j-1);
            m.at<double>(m.rows-i-1, m.cols-j-1) = temp;
        }
    }

}  

Mat fftshift(Mat m) {
    int a = m.rows/2;
    int b = m.cols/2;
    Mat r = Mat::zeros(m.size(), m.type());
    for (int i=0; i<m.rows; i++) {
        for (int j=0; j<m.cols; j++) {
            r.at<double>((i+a)%m.rows, (j+b)%m.cols) = m.at<double>(i, j);
        }
    }
    return r;
}

extern "C"
{
  #include "api_scilab.h"
  #include "Scierror.h"
  #include "BOOL.h"
  #include <localization.h>
  #include "sciprint.h"
  #include "../common.h"


  int opencv_fsamp2(char *fname, unsigned long fname_len)
  {

    SciErr sciErr;
    int intErr = 0;
    int iRows=0,iCols=0;
    int cRows=0,cCols=0;
    int *piAddr = NULL;
    int *piAddrNew = NULL;

    //checking input argument
    CheckInputArgument(pvApiCtx, 1, 4);
    CheckOutputArgument(pvApiCtx, 1, 1) ;

    // Mat image, imgcpy;
    // retrieveImage(image, 1);
    // string tempstring = type2str(image.type());
    // char *imtype;
    // imtype = (char *)malloc(tempstring.size() + 1);
    // memcpy(imtype, tempstring.c_str(), tempstring.size() + 1);
    // bool integer = true;
    // int scale = 1;
    // double error = 0;

    if (nbInputArgument(pvApiCtx) == 1) {
        Mat hd, hdcpy;
        retrieveImage(hdcpy, 1);
        hdcpy.convertTo(hd, CV_64F);
        rotate180(hd);

        fftshift(hd).copyTo(hd);

        rotate180(hd);


        Mat padded;                            //expand input image to optimal size
        int m = getOptimalDFTSize( hd.rows );
        int n = getOptimalDFTSize( hd.cols ); // on the border add zero values
        copyMakeBorder(hd, padded, 0, m - hd.rows, 0, n - hd.cols, BORDER_CONSTANT, Scalar::all(0));

        Mat planes[] = {Mat_<double>(padded), Mat::zeros(padded.size(), CV_64F)};
        Mat complexI;
        merge(planes, 2, complexI);         // Add to the expanded another plane with zeros

        dft(complexI, complexI, DFT_INVERSE | DFT_SCALE);            // this way the result may fit in the source matrix

        split(complexI, planes);   

        fftshift(planes[0]).copyTo(planes[0]);
        fftshift(planes[1]).copyTo(planes[1]);
        rotate180(planes[0]);
        rotate180(planes[1]);

        double *re = (double *)malloc(planes[0].rows * planes[0].cols * sizeof(double));
        double *im = (double *)malloc(planes[0].rows * planes[0].cols * sizeof(double));

        for(int i=0;i<planes[0].rows;i++)
        {
            for(int j=0;j<planes[0].cols;j++)
            {
                re[i+planes[0].rows*j]=planes[0].at<double>(i, j);
                //cout<<planes[0].at<double>(i, j)<<" ";
                im[i+planes[0].rows*j]=planes[1].at<double>(i, j);
            }
            //cout<<endl;
        }



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


        //Adding the R value matrix to the list
        //Syntax : createMatrixOfInteger32InList(void* _pvCtx, int _iVar, int* _piParent, int _iItemPos, int _iRows, int _iCols, const int* _piData)
        sciErr = createComplexMatrixOfDoubleInList(pvApiCtx, nbInputArgument(pvApiCtx)+1 , piAddrNew, 1, planes[0].rows,planes[0].cols, re, im);
        free(re);
        free(im);
        if(sciErr.iErr)
        {
            printError(&sciErr, 0);
            return 0;
        }

    }
    else if (nbInputArgument(pvApiCtx)==2 || nbInputArgument(pvApiCtx)==3) {
        sciprint("1 or 4 arguments expected.");
        return 0;
    }



    AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
    //Returning the Output Variables as arguments to the Scilab environment
    ReturnArguments(pvApiCtx);            
    return 0;

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