PTdecode/CImg-1.3.0/examples/use_cimgmatlab.cpp

Mon, 03 Aug 2009 14:21:23 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Mon, 03 Aug 2009 14:21:23 +0100
changeset 6
a274aba0a9d3
parent 5
1204ebf9340d
permissions
-rwxr-xr-x

added keepme for ptdecode obj directory

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