Advertisement
Guest User

Converting OpenGL Image to OpenCV

a guest
Aug 15th, 2014
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. /**
  2.   * http://answers.opencv.org/question/39386/opencv-error-segmentation-fault-converting-data/
  3.   */
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <cstring>
  7.  
  8. #include "camera.h"
  9. #include "graphics.h"
  10.  
  11. #include <opencv2/opencv.hpp>
  12. #include <opencv2/highgui/highgui.hpp>
  13.  
  14. #define MAIN_TEXTURE_WIDTH 512
  15. #define MAIN_TEXTURE_HEIGHT 512
  16.  
  17. //entry point
  18. int main(int argc, const char **argv)
  19. {
  20.  
  21.     //how many detail levels (1 = just the capture res, > 1 goes down by half each level, 4 max)
  22.     int num_levels = 4;
  23.  
  24.     //init graphics and the camera
  25.     InitGraphics();
  26.     CCamera* cam = StartCamera(MAIN_TEXTURE_WIDTH, MAIN_TEXTURE_HEIGHT,30,num_levels,true);
  27.    
  28.     //create MOG background substractor model
  29.     //BackgroundSubtractorMOG bg_model;
  30.  
  31.     //create 4 textures of decreasing size
  32.     GfxTexture textures[4];
  33.     for(int texidx = 0; texidx < num_levels; texidx++)
  34.         textures[texidx].Create(MAIN_TEXTURE_WIDTH >> texidx, MAIN_TEXTURE_HEIGHT >> texidx);
  35.  
  36.     printf("Running frame loop\n");
  37.     for(;;)
  38.     {
  39.  
  40.         //lock the chosen frame buffer, and copy it directly into the corresponding open gl texture
  41.         const void* frame_data; int frame_sz;
  42.         if(cam->BeginReadFrame(1,frame_data,frame_sz))
  43.         {
  44.             //if doing argb conversion the frame data will be exactly the right size so just set directly
  45.             textures[1].SetPixels(frame_data);
  46.             char* frame_data_copy = new char[frame_sz]; // create a buffer of same size in bytes
  47.             std::memcpy(frame_data_copy, frame_data, frame_sz); // copy data from frame_data to frame_data_copy
  48.             cv::Mat TempMat(MAIN_TEXTURE_HEIGHT, MAIN_TEXTURE_WIDTH, CV_8UC4, frame_data_copy); //create cv::Mat
  49.             delete [] frame_data_copy; //free allocated memory for copy, this also invalidates the data of TempMat
  50.             imshow("Camera Feed",TempMat);
  51.             cam->EndReadFrame(1);
  52.         }
  53.        
  54.         //begin frame, draw the texture then end frame (the bit of maths just fits the image to the screen while maintaining aspect ratio)
  55.         BeginFrame();
  56.         float aspect_ratio = float(MAIN_TEXTURE_WIDTH)/float(MAIN_TEXTURE_HEIGHT);
  57.         float screen_aspect_ratio = 1280.f/720.f;
  58.         DrawTextureRect(&textures[1],-aspect_ratio/screen_aspect_ratio,-1.f,aspect_ratio/screen_aspect_ratio,1.f);
  59.         EndFrame();
  60.         int c = cv::waitKey(30);
  61.         if (c == 'q' || c == 'Q' || (c & 255) == 27)
  62.             break;
  63.     }
  64.  
  65.     StopCamera();
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement