PTdecode/CImg-1.3.0/examples/use_cimgmatlab.cpp

Mon, 03 Aug 2009 23:41:04 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Mon, 03 Aug 2009 23:41:04 +0100
changeset 11
69416826d18c
parent 5
1204ebf9340d
permissions
-rwxr-xr-x

added dep/*.d and obj/*.o to hgignore

     1 /*-----------------------------------------------------------------------
     3   File : use_cimgmatlab.cpp
     5   Description: Example of use for the CImg plugin 'plugins/cimgmatlab.h'
     6   which allows to use CImg in order to develop matlab external
     7   functions (mex functions).
     8   User should be familiar with Matlab C/C++ mex function concepts,
     9   as this file is by no way a mex programming tutorial.
    11   This simple example implements a mex function that can be called
    12   as
    14   - v = cimgmatlab_cannyderiche(u,s)
    15   - v = cimgmatlab_cannyderiche(u,sx,sy)
    16   - v = cimgmatlab_cannyderiche(u,sx,sy,sz)
    18   The corresponding m-file is cimgmatlab_cannyderiche.m
    21   Copyright : Francois Lauze - http://www.itu.dk/people/francois
    22   This software is governed by the Gnu Lesser General Public License
    23   see http://www.gnu.org/copyleft/lgpl.html
    25   The plugin home page is at
    26   http://www.itu.dk/people/francois/cimgmatlab.html
    28   for the compilation: using the mex utility provided with matlab, just
    29   remember to add the -I flags with paths to CImg.h and/or cimgmatlab.h.
    30   The default lcc cannot be used, it is a C compiler and not a C++ one!
    32 --------------------------------------------------------------------------*/
    34 #include <mex.h>
    35 #define cimg_plugin "plugins/cimgmatlab.h"
    36 #include <CImg.h>
    38 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    39   if (nrhs < 2) mexErrMsgTxt("No enough input arguments.");
    40   if (nrhs > 4) mexErrMsgTxt("Too many input arguments.");
    41   cimg_library::CImg<> u(prhs[0],true);
    42   if (nrhs == 2) {
    43     const float s = (float)mxGetScalar(prhs[1]);
    44     plhs[0] = u.get_blur(s).toMatlab();
    45   } else if (nrhs == 3) {
    46     const float sx = (float)mxGetScalar(prhs[1]);
    47     const float sy = (float)mxGetScalar(prhs[2]);
    48     plhs[0] = u.get_blur(sx,sy,0).toMatlab();
    49   } else if (nrhs == 4) {
    50     const float sx = (float)mxGetScalar(prhs[1]);
    51     const float sy = (float)mxGetScalar(prhs[2]);
    52     const float sz = (float)mxGetScalar(prhs[3]);
    53     plhs[0] = u.get_blur(sx,sy,sz).toMatlab();
    54   }
    55 }
    57 /*------------------------------------------------------------------
    59   SPECIAL NOTE :
    60   -------------
    62   How to read a .mat file using plugin 'cimgmatlab.h' ?
    63   (contribution by Vo Duc Khanh/Denso IT Lab, Tokyo, Japan).
    65   #include <mex.h>
    66   #include <mat.h>
    67   #include <matrix.h>
    69   #define cimg_plugin "cimgmatlab.h"
    71   #include "CImg.h"
    72   #include <iostream>
    73   #include <string>
    75   .........
    77   using namespace cimg_library;
    78   using namespace std;
    80   // Load input images (125700 images) from training database 'BmpTrainingDb.mat'
    81   MATFile *pmat, *pmat_out;
    82   mxArray *pa, *pa_out;
    83   const char data_path[256] = ".\\BmpTrainingDb.mat\0";
    84   const char *var_name;
    86   pmat = matOpen(data_path, "r");
    87   if (pmat == NULL) {
    88     cout << "Error opening file " << data_path << endl;
    89     return (1);
    90   }
    92   pa = matGetNextVariable(pmat, &var_name);
    93   if (pa == NULL){
    94     cout << "Error reading in file " << data_path << endl;
    95     return (1);
    96   }
    98   CImg<unsigned char> train_db(pa,false);
    99   ........
   102   -----------------------------------------------------------------------------*/