金子邦彦研究室プログラミングOctave の活用Octave の mkoctfile コマンドを使って OCT ファイルを作る

Octave の mkoctfile コマンドを使って OCT ファイルを作る

C++ のプログラムをコンパイルし,Octave から呼び出せるようにします.

ベクトルを扱う関数の例

  1. サンプルプログラム

    hoge.cc のような名前で次のファイルを作る.

    #include<stdio.h>
    #include<octave/oct.h>
    
    DEFUN_DLD (hoge, args, , "vector operator sample,")
    {
        ColumnVector x (args(0).vector_value());
        ColumnVector y (3);
    
        fprintf( stderr, "x(0) = %f, x(1) = %f, x(2) = %f\n", x(0), x(1), x(2) );
        
        y(0) = x(0) + 1;
        y(1) = x(1) + 1;
        y(2) = x(2) + 1;
    
        return octave_value (y); 
    }
    
  2. mkoctfile コマンドを用いてコンパイルする.

    CC=g++ mkoctfile hoge.cc -I/usr/local/include -L/usr/local/lib 
    
  3. コンパイルすると hoge.oct のような名前のファイルができる.
  4. Octave を起動し「hoge([1, 2, 3], 0)」のような式を評価させてみる.

    [image]

    #include<stdio.h>
    #include<opencv2/core/core.hpp>
    #include<opencv2/highgui/highgui.hpp>
    #include<octave/oct.h>
    
    DEFUN_DLD (hoge, args, , "color image sample,")
    {
      Matrix r (args(0).matrix_value());
      Matrix g (args(1).matrix_value());
      Matrix b (args(2).matrix_value());
        int i, j;
        int a;
    
        cv::Mat img(512, 512, CV_8UC3); // 512x512x3 matric
        for( i = 0; i < 512; i++ ) {
          for( j = 0; j < 512; j++ ) {
        img.at<uchar[3]>(i, j)[0] = (uchar)b(i, j); 
        img.at<uchar[3]>(i, j)[1] = (uchar)r(i, j); 
        img.at<uchar[3]>(i, j)[2] = (uchar)g(i, j); 
          }
        }
    
        // open a window and show an image
        cv::namedWindow("imshow", CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO);
        cv::imshow("image1", img);
        // wait for a key input
        cv::waitKey(0);
    
        return octave_value (r); 
    }