Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lidio601
Created May 8, 2015 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lidio601/56f2e3ce44d55adf39d3 to your computer and use it in GitHub Desktop.
Save lidio601/56f2e3ce44d55adf39d3 to your computer and use it in GitHub Desktop.
Octave to C bridging for calling an Octave / MatLAB routine from a C program
// clear; mkoctfile extlibrary.cpp -o extlibrary.a
// mkoctfile extlibrary.cpp -M --link-stand-alone -o extlibrary.a
// clear; mkoctfile extlibrary.cpp --link-stand-alone -o extlibrary.o
//#include <iostream>
#include <octave-3.2.4/octave/oct.h>
#include <octave-3.2.4/octave/octave.h>
#include <octave-3.2.4/octave/parse.h>
#include <octave-3.2.4/octave/toplev.h>
#include "extlibrary.h"
#include <opencv2/opencv.hpp>
//using namespace std;
using namespace cv;
//int main (void) {
// cout << "Hello Octave world!\n";
// int n = 2;
// Matrix a_matrix = Matrix (n, n);
// for (octave_idx_type i = 0; i < n; i++)
// {
// for (octave_idx_type j = 0; j < n; j++)
// {
// a_matrix (i, j) = (i + 1) * 10 + (j + 1);
// }
// }
// cout << a_matrix;
// return 0;
//}
Mat extfunction(Mat input) {
string_vector argv (2);
argv(0) = "embedded";
argv(1) = "-q";
octave_main (2, argv.c_str_vec(), 1);
octave_idx_type m = input.rows;
octave_idx_type n = input.cols;
Matrix a_matrix = Matrix (n, m);
// cout << "X of [";
for (octave_idx_type i = 0; i < n; i++) {
for (octave_idx_type j = 0; j < m; j++) {
a_matrix(i,j) = input.at<float>(i,j);
// cout<< a_matrix(i,j);
// if ( j != m - 1 ) {
// cout<<", ";
// } else {
// cout<<"]";
// if( i != n - 1 ) {
// cout<<", "<<endl;
// } else {
// cout<<" ";
// }
// }
}
}
// cout << "] is ";
octave_value_list in = octave_value (a_matrix);
octave_value_list out = feval ("getIdentity", in, 1);
// return Mat();
if (!error_state && out.length() > 0) {
a_matrix = out(0).matrix_value();
// if (a_matrix.numel () == 1) {
// cout << a_matrix(0) << "\n";
// } else {
// cout << "invalid\n";
// }
Mat ris = Mat::zeros(input.rows,input.cols,CV_32FC1);
for(int i=0; i<a_matrix.rows(); i++) {
for(int j=0; j<a_matrix.cols(); j++) {
ris.at<float>(i,j) = a_matrix(i,j);
}
}
// cout<<ris<<endl;
return ris;
} else {
// cout<<"invalid\n";
return Mat();
}
}
double** extfunction(double* input, int input_m, int input_n) {
string_vector argv (2);
argv(0) = "embedded";
argv(1) = "-q";
octave_main (2, argv.c_str_vec(), 1);
octave_idx_type m = input_m;
octave_idx_type n = input_n;
Matrix a_matrix = Matrix (n, m);
// std::cout << "X of [";
for (octave_idx_type i = 0; i < n; i++) {
for (octave_idx_type j = 0; j < m; j++) {
a_matrix(i,j) = input[ j + i * m ];
// std::cout<< input[ j + i * m ];
// if ( j != m - 1 ) {
// std::cout<<", ";
// } else {
// std::cout<<"]";
// if( i != n - 1 ) {
// std::cout<<", "<<std::endl;
// } else {
// std::cout<<" ";
// }
// }
}
}
// std::cout << "] is ";
octave_value_list in = octave_value (a_matrix);
octave_value_list out = feval ("getIdentity", in, 1);
if (!error_state && out.length() > 0) {
a_matrix = out(0).matrix_value();
// if (a_matrix.numel () == 1) {
// cout << a_matrix(0) << "\n";
// } else {
// cout << "invalid\n";
// }
double ** ris = new double*[a_matrix.rows()];
for(int i=0; i<a_matrix.rows(); i++) {
ris[i] = new double[a_matrix.cols()];
for(int j=0; j<a_matrix.cols(); j++) {
ris[i][j] = a_matrix(i,j);
}
}
return ris;
} else {
// std::cout<<"invalid\n";
return 0;
}
}
#include <opencv2/opencv.hpp>
cv::Mat extfunction(cv::Mat input);
double** extfunction(double* input, int input_m, int input_n);
function ris = getIdentity(a)
[m n] = size(a);
ris = eye(max([m n]));
end
//
// main.cpp
// testMathlab
//
// Created by Fabio Cigliano on 23/08/13.
// Copyright (c) 2013 Fabio Cigliano. All rights reserved.
//
// http://www.mathworks.it/it/help/matlab/calling-matlab-engine-from-c-c-and-fortran-programs.html
// http://www.mathworks.it/videos/generating-c-code-from-matlab-code-68964.html
// http://www.gnu.org/software/octave/doc/interpreter/Standalone-Programs.html#Standalone-Programs
// http://octave.1599824.n4.nabble.com/Calling-Octave-Function-from-Xcode-Program-td4654943.html
// mkoctfile --link-stand-alone main.cpp -o main
// http://stackoverflow.com/questions/9410328/how-to-call-a-matlab-code-from-objective-c
// http://octave.1599824.n4.nabble.com/Matlab-vs-Octave-td2275851.html
// Currently, there is no Octave compiler. Octave is a pure interpreter.
// http://www.mathworks.it/products/compiler/
// http://www.mathworks.com/matlabcentral/fileexchange/12987
// http://www.mathworks.it/it/help/matlab/programming-interfaces-for-c-c-fortran-com.htmls
#include <iostream>
#include <opencv2/opencv.hpp>
#include "extlibrary.h"
using namespace std;
using namespace cv;
int main (void) {
// double input[2] = { 0 , 1 };
//
// cout<<"calling extfunction"<<endl<<endl;
//
// double** ris;
//
// ris = extfunction(input,2,1);
//
// cout<<endl<<"result: "<<ris<<endl;
// for(int i=0;i<2;i++) {
// for(int j=0;j<2;j++) {
// cout<<ris[i][j]<<" ";
// }
// cout<<endl;
// }
Mat in = Mat::zeros(10,10,CV_8UC1);
cout<<"input: "<<in<<endl;
cout<<"calling extfunction"<<endl<<endl;
Mat out = extfunction(in);
cout<<endl<<"result: "<<out<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment