#include "opencv2/opencv.hpp" #include "mex.h" using namespace cv; /******************************************************* Usage: [imageMatrix] = RGB2Gray('imageFile.jpeg'); Input: a image file OutPut: a matrix of image which can be read by Matlab **********************************************************/ void exit_with_help() { mexPrintf( "Usage: [imageMatrix] = DenseTrack('imageFile.jpg');\n" ); } static void fake_answer(mxArray *plhs[]) { plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL); } void RGB2Gray(char *filename, mxArray *plhs[]) { // read the image Mat image = imread(filename); if(image.empty()) { mexPrintf("can't open input file %s\n", filename); fake_answer(plhs); return; } // convert it to gray format Mat gray; if (image.channels() == 3) cvtColor(image, gray, CV_RGB2GRAY); else image.copyTo(gray); // convert the result to Matlab-supported format for returning int rows = gray.rows; int cols = gray.cols; plhs[0] = mxCreateDoubleMatrix(rows, cols, mxREAL); double *imgMat; imgMat = mxGetPr(plhs[0]); for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) *(imgMat + i + j * rows) = (double)gray.at<uchar>(i, j); return; } void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { if(nrhs == 1) { char filename[256]; mxGetString(prhs[0], filename, mxGetN(prhs[0]) + 1); if(filename == NULL) { mexPrintf("Error: filename is NULL\n"); exit_with_help(); return; } RGB2Gray(filename, plhs); } else { exit_with_help(); fake_answer(plhs); return; } }
編譯與測試ide
>> mex -I/opt/local/opencv2.4/include -L/opt/local/opencv2.4/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_video RGB2Gray.cpp >> img = RGB2Gray('test.jpg'); >> imshow(uint8(img)); Warning: Image is too big to fit on screen; displaying at 50% > In imuitools/private/initSize at 72 In imshow at 259 >>