MATLAB Answers

error while using mexOpenCV

14 views (last 30 days)
I installed the Opencv support package on MATLAB R2015a. I could compile the example files using, e.g.
mexOpenCV matchTemplateOCV.cpp
However, when I try to compile my own *.cpp file, it gives me the following error,
Error using mexOpenCV (line 120)
Undefined symbols for architecture x86_64:
"cv::imdecode(cv::_InputArray const&, int)", referenced from:
_mexFunction in decodeUDPimg.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
my code is:
#include "opencvmex.hpp"
#define _DO_NOT_EXPORT
#if defined(_DO_NOT_EXPORT)
#define DllExport
#define DllExport __declspec(dllexport)
unsigned char *InputBuffer;
int buffLen;
using namespace cv;
using namespace std;
// Check inputs
void checkInputs(int nrhs, const mxArray *prhs[])
// Check number of inputs
// Expecting 2 inputs: uint8 buffer, and size of buffer
if (nrhs != 2)
mexErrMsgTxt("Incorrect number of inputs. Function expects 2 inputs.");
// Check buffer data type
if (!mxIsUint8(prhs[0]))
mexErrMsgTxt("Buffer must be UINT8.");
if (prhs[1] == 0)
mexErrMsgTxt("Buffer length should be positive.");
// Main entry point to a MEX function
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
// Check inputs to mex function
checkInputs(nrhs, prhs);
// process image data
InputBuffer = (unsigned char *)mxGetData(prhs[0]);
buffLen= (int)mxGetScalar(prhs[1]);
Mat rawData = Mat(1, buffLen, CV_8UC1, InputBuffer);
Mat frame = imdecode(rawData, CV_LOAD_IMAGE_COLOR);
if (frame.size().width == 0) {
cerr << "decode failure!" << endl;
//plhs[0] = mxCreateDoubleScalar(0);
//plhs[1] = mxCreateDoubleScalar(1);
// Put the data back into the output MATLAB array
plhs[0] = ocvMxArrayFromImage_single(frame);
The error appears on
Mat frame = imdecode(rawData, CV_LOAD_IMAGE_COLOR);
If I remove it, the code compiles normally.
Any hints on what could the problem be?


Sign in to comment.

Accepted Answer

Mohamed Abdelkader Zahana
Mohamed Abdelkader Zahana on 24 May 2016
My problem is solved.
The problem was that the library that contains the
function was not included by default. It has to be manually added in the
file. Namely, by adding 'highgui' to the libs variable.
libs = {'core', 'features2d', 'imgproc', 'ml', 'legacy', 'nonfree', ...
'objdetect', 'photo', 'calib3d', 'video', 'flann', 'contrib','highgui'};

  1 Comment

JAI PRAKASH on 13 Jan 2019
Apart of adding 'highgui' keyword, some lib file also need to be added in Matlab?
I am also looking for a mex of imdecode function. Target is first on CPU then on GPU.
Mohamed can you help on this..
Thank you

Sign in to comment.

More Answers (0)

Translated by