blob: 08c06777d09dcb6e3229efb8a2354700f6f156eb [file] [log] [blame]
Austin Schuh8c794d52019-03-03 21:17:37 -08001/*-----------------------------------------------------------------------
2
3 File : use_cimgmatlab.cpp
4
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.
10
11 This simple example implements a mex function that can be called
12 as
13
14 - v = cimgmatlab_cannyderiche(u,s)
15 - v = cimgmatlab_cannyderiche(u,sx,sy)
16 - v = cimgmatlab_cannyderiche(u,sx,sy,sz)
17
18 The corresponding m-file is cimgmatlab_cannyderiche.m
19
20
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
24
25 The plugin home page is at
26 http://www.itu.dk/people/francois/cimgmatlab.html
27
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!
31
32--------------------------------------------------------------------------*/
33
34#include <mex.h>
35#define cimg_plugin "plugins/cimgmatlab.h"
36#include <CImg.h>
37
38void 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}
56
57/*------------------------------------------------------------------
58
59 SPECIAL NOTE :
60 -------------
61
62 How to read a .mat file using plugin 'cimgmatlab.h' ?
63 (contribution by Vo Duc Khanh/Denso IT Lab, Tokyo, Japan).
64
65 #include <mex.h>
66 #include <mat.h>
67 #include <matrix.h>
68
69 #define cimg_plugin "cimgmatlab.h"
70
71 #include "CImg.h"
72 #include <iostream>
73 #include <string>
74
75 .........
76
77 using namespace cimg_library;
78 using namespace std;
79
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;
85
86 pmat = matOpen(data_path, "r");
87 if (pmat == NULL) {
88 cout << "Error opening file " << data_path << endl;
89 return (1);
90 }
91
92 pa = matGetNextVariable(pmat, &var_name);
93 if (pa == NULL){
94 cout << "Error reading in file " << data_path << endl;
95 return (1);
96 }
97
98 CImg<unsigned char> train_db(pa,false);
99 ........
100
101
102 -----------------------------------------------------------------------------*/