From a6df67e8bcd5159cde27556f4f6a315f8dc2215f Mon Sep 17 00:00:00 2001 From: shamikam Date: Mon, 16 Jan 2017 02:56:17 +0530 Subject: First Commit --- sci_gateway/cpp/opencv_fsamp2.cpp | 157 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 sci_gateway/cpp/opencv_fsamp2.cpp (limited to 'sci_gateway/cpp/opencv_fsamp2.cpp') diff --git a/sci_gateway/cpp/opencv_fsamp2.cpp b/sci_gateway/cpp/opencv_fsamp2.cpp new file mode 100644 index 0000000..02780b6 --- /dev/null +++ b/sci_gateway/cpp/opencv_fsamp2.cpp @@ -0,0 +1,157 @@ +/******************************************************** +Author: Vinay + +Function: ind2gray(image, colormap) +********************************************************/ + +#include +#include "opencv2/core/core.hpp" +#include "opencv2/highgui/highgui.hpp" +#include "opencv2/opencv.hpp" +#include +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(i, j); + m.at(i, j) = m.at(m.rows-i-1, m.cols-j-1); + m.at(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((i+a)%m.rows, (j+b)%m.cols) = m.at(i, j); + } + } + return r; +} + +extern "C" +{ + #include "api_scilab.h" + #include "Scierror.h" + #include "BOOL.h" + #include + #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_(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(i, j); + //cout<(i, j)<<" "; + im[i+planes[0].rows*j]=planes[1].at(i, j); + } + //cout<