Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://www.youtube.com/user/thecplusplusguy
- //play a video with SDL and OpenCV
- #include <opencv2/opencv.hpp>
- #include <SDL/SDL.h>
- using namespace cv;
- SDL_Surface* convertToSDLSurface(const Mat& img)
- {
- //"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
- IplImage opencvimg2=(IplImage)img;
- IplImage* opencvimg=&opencvimg2;
- //do the actual conversation to the good ol' SDL_Surface
- SDL_Surface *surface = SDL_CreateRGBSurfaceFrom((void*)opencvimg->imageData,opencvimg->width,opencvimg->height,opencvimg->depth*opencvimg->nChannels,opencvimg->widthStep,0xff0000, 0x00ff00, 0x0000ff, 0);
- return surface;
- }
- int main()
- {
- //basic SDL stuff
- SDL_Init(SDL_INIT_EVERYTHING);
- SDL_Surface* screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
- //open up that avi file (lot of formats are supported)
- VideoCapture vc("immortal.avi");
- //create an image, which is the current frame
- Mat image;
- //basic SDL stuff
- Uint32 start;
- SDL_Event event;
- bool running=true;
- while(running)
- {
- start=SDL_GetTicks();
- while(SDL_PollEvent(&event))
- {
- switch(event.type)
- {
- case SDL_QUIT:
- running=false;
- break;
- case SDL_KEYDOWN:
- switch(event.key.keysym.sym)
- {
- case SDLK_ESCAPE:
- running=false;
- break;
- }
- }
- }
- //read current frame to image
- vc >> image;
- //convert it to SDL_Surface
- SDL_Surface* frame=convertToSDLSurface(image);
- //render the whole thing out to 0,0 coordinate
- SDL_BlitSurface(frame,NULL,screen,NULL);
- //avoid memory leaks
- SDL_FreeSurface(frame);
- //show it
- SDL_Flip(screen);
- //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:
- //int FPS=vc.get(CV_CAP_PROP_FPS);
- // if(1000.0/30>SDL_GetTicks()-start)
- // SDL_Delay(1000.0/30-(SDL_GetTicks()-start));
- }
- SDL_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement