Advertisement
thecplusplusguy

Play video with SDL and OpenCV

Mar 5th, 2013
936
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. //http://www.youtube.com/user/thecplusplusguy
  2. //play a video with SDL and OpenCV
  3. #include <opencv2/opencv.hpp>
  4. #include <SDL/SDL.h>
  5. using namespace cv;
  6.  
  7.  
  8. SDL_Surface* convertToSDLSurface(const Mat& img)
  9. {
  10.     //"convert" it to older format, because I found conversation for that, and it's too late for me to rewrite it to the newer Mat
  11.     IplImage opencvimg2=(IplImage)img;
  12.     IplImage* opencvimg=&opencvimg2;
  13.    
  14.     //do the actual conversation to the good ol' SDL_Surface
  15.     SDL_Surface *surface = SDL_CreateRGBSurfaceFrom((void*)opencvimg->imageData,opencvimg->width,opencvimg->height,opencvimg->depth*opencvimg->nChannels,opencvimg->widthStep,0xff0000, 0x00ff00, 0x0000ff, 0);
  16.    
  17.     return surface;
  18. }
  19.  
  20. int main()
  21. {
  22.     //basic SDL stuff
  23.     SDL_Init(SDL_INIT_EVERYTHING);
  24.     SDL_Surface* screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
  25.    
  26.     //open up that avi file (lot of formats are supported)
  27.     VideoCapture vc("immortal.avi");
  28.    
  29.     //create an image, which is the current frame
  30.   Mat image;
  31.    
  32.  
  33.     //basic SDL stuff  
  34.     Uint32 start;
  35.     SDL_Event event;
  36.     bool running=true;
  37.     while(running)
  38.     {
  39.         start=SDL_GetTicks();
  40.         while(SDL_PollEvent(&event))
  41.         {
  42.             switch(event.type)
  43.             {
  44.                 case SDL_QUIT:
  45.                     running=false;
  46.                     break;
  47.                 case SDL_KEYDOWN:
  48.                     switch(event.key.keysym.sym)
  49.                     {
  50.                         case SDLK_ESCAPE:
  51.                             running=false;
  52.                             break;
  53.                     }
  54.             }
  55.         }
  56.  
  57.         //read current frame to image
  58.         vc >> image;
  59.         //convert it to SDL_Surface
  60.         SDL_Surface* frame=convertToSDLSurface(image);
  61.         //render the whole thing out to 0,0 coordinate
  62.         SDL_BlitSurface(frame,NULL,screen,NULL);
  63.         //avoid memory leaks
  64.         SDL_FreeSurface(frame);
  65.        
  66.         //show it
  67.         SDL_Flip(screen);
  68.  
  69.     //regulate FPS, if you want to make the movie as quick as it was made, you can get the original FPS of the video with:
  70.     //int FPS=vc.get(CV_CAP_PROP_FPS);
  71.     //  if(1000.0/30>SDL_GetTicks()-start)
  72. //          SDL_Delay(1000.0/30-(SDL_GetTicks()-start));
  73.     }
  74.  
  75.    
  76.     SDL_Quit();
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement