Advertisement
j0h

Capture OpenCV to SDL

j0h
Sep 5th, 2016
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <SDL/SDL.h>
  2. #include "opencv2/core/core.hpp"
  3. #include "opencv2/imgproc/imgproc.hpp"
  4. #include "opencv2/imgcodecs.hpp"
  5. #include "opencv2/highgui/highgui.hpp"
  6. #include <opencv2/imgproc/imgproc.hpp>
  7. using namespace cv;
  8. //Build Flags
  9. //g++ -Wall -g -o "%e" "%f" -lSDL -lSDL_image -lSDL_ttf  -lSDL_mixer -lGL -lGLU -lglut -lGLEW -lXi  -lXmu `pkg-config --cflags --libs opencv`
  10. SDL_Surface* image = NULL;
  11. SDL_Surface* backbuffer = NULL;
  12. SDL_Surface* asurface = NULL;
  13. //pass in an opencv image, get a sureface out
  14. //https://wiki.libsdl.org/SDL_CreateRGBSurfaceFrom
  15. //http://stackoverflow.com/questions/22702630/converting-cvmat-to-sdl-texture
  16. SDL_Surface* convertCV_MatToSDL_Texture(const cv::Mat &frame){
  17.     IplImage opencvimg2 = (IplImage)frame;
  18.     IplImage* opencvimg = &opencvimg2;
  19.  
  20.      //Convert to SDL_Surface
  21.     asurface = SDL_CreateRGBSurfaceFrom(
  22.                          (void*)opencvimg->imageData,
  23.                          opencvimg->width, opencvimg->height,
  24.                          opencvimg->depth*opencvimg->nChannels,
  25.                          opencvimg->widthStep,
  26.                          0xff0000, 0x00ff00, 0x0000ff, 0);
  27.  
  28.     if(asurface == NULL){
  29.         printf("Couldn't convert Mat to Surface.");
  30.         return NULL;
  31.     }    else    {
  32.         printf("SUCCESS! conversion!");
  33.         return asurface;
  34.     }
  35.  
  36.     cvReleaseImage(&opencvimg);
  37.  
  38. }
  39. int main(int argc, char **argv){
  40.    
  41.     VideoCapture cap(0); // open the default camera
  42.     if(!cap.isOpened())  // check if we succeeded
  43.         return -1;
  44.        
  45.        
  46.     //Init SDL
  47.     if(SDL_Init(SDL_INIT_EVERYTHING) < 0){
  48.         printf("SDL failed to initialize!");
  49.         SDL_Quit();
  50.         return 0;
  51.     }
  52.  
  53.     backbuffer = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
  54.  
  55.     SDL_WM_SetCaption("SDL!!!", NULL);
  56.  
  57.     //Load the image
  58.     //image = SDL_LoadBMP("graphics/image.bmp" );
  59.     Mat frame;
  60.     cap >> frame; // get a new frame from camera
  61.     //imshow("edges", frame);
  62.     image = convertCV_MatToSDL_Texture(frame);
  63.    
  64.     if(image == NULL){
  65.         printf("Image failed to load!\n");
  66.         SDL_Quit();
  67.         return 0;
  68.     }
  69.  
  70.     //Draw the image
  71.     SDL_BlitSurface(image, NULL, backbuffer, NULL );
  72.     SDL_Flip(backbuffer);
  73.  
  74.     //Wait
  75.     SDL_Delay(3000);
  76.  
  77.     //Finish
  78.     SDL_FreeSurface( image);
  79.     SDL_Quit();
  80.  
  81.     return 1;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement