Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2013
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #include <mex.h>
  2. #include <matrix.h>
  3.  
  4. #include <opencv2/opencv.hpp>
  5.  
  6. std::string readMATLABString(const mxArray *prhs)
  7. {
  8.     int buflen = (mxGetM(prhs) * mxGetN(prhs)) + 1;
  9.     char *input_buf = (char *)mxCalloc(buflen, sizeof(char));
  10.     mxGetString(prhs, input_buf, buflen);
  11.  
  12.     std::string toReturn(input_buf);
  13.     mxFree(input_buf);
  14.  
  15.     return toReturn;
  16. }
  17.  
  18. void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
  19. {
  20.     std::string vidfilename = readMATLABString(prhs[0]);
  21.  
  22.     double *frameIdxs = mxGetPr(prhs[1]);
  23.     const mwSize *dims = mxGetDimensions(prhs[1]);
  24.  
  25.     cv::VideoCapture vc(vidfilename);
  26.     int w = vc.get(cv::CAP_PROP_FRAME_WIDTH);
  27.     int h = vc.get(cv::CAP_PROP_FRAME_HEIGHT);
  28.     int nf = vc.get(cv::CAP_PROP_FRAME_COUNT);
  29.     int tw = w, th = h;
  30.     if(nrhs > 2)
  31.     {
  32.         double * tres = mxGetPr(prhs[2]);
  33.         tw = tres[0], th = tres[1];
  34.     }
  35.  
  36.     mwSize outdim[4] = {th, tw, 3, dims[1]};
  37.     plhs[0] = mxCreateNumericArray(4, outdim, mxSINGLE_CLASS, mxREAL);
  38.     float *data = (float *)mxGetPr(plhs[0]);
  39.     cv::Mat m(h, w, CV_8UC3);
  40.     cv::Mat timg(th, tw, CV_32FC3);
  41.  
  42.     for(int i=0; i<dims[1]; i++)
  43.     {
  44.         long frame = frameIdxs[i];
  45.  
  46.         vc.set(cv::CAP_PROP_POS_FRAMES, frame);
  47.         if (frame >= nf)
  48.             m.setTo(0);
  49.         else
  50.             vc >> m;
  51.         if(m.size().area() > 0)
  52.             cv::resize(m, timg, timg.size(), 0, 0, cv::INTER_AREA);
  53.         float *ptr = &data[i*tw*th*3];
  54.         unsigned char *srcptr = timg.data;
  55.         for(int x=0, z=0; x<tw; x++)
  56.         {
  57.             for(int y=0; y<th; y++, z++)
  58.             {
  59.                 ptr[z] = srcptr[(y*tw+x)*3+2] / 255.f;
  60.                 ptr[tw*th+z] = srcptr[(y*tw+x)*3+1] / 255.f;
  61.                 ptr[tw*th*2+z] = srcptr[(y*tw+x)*3] / 255.f;
  62.             }
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement