Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * http://answers.opencv.org/question/39386/opencv-error-segmentation-fault-converting-data/
- */
- #include <stdio.h>
- #include <unistd.h>
- #include <cstring>
- #include "camera.h"
- #include "graphics.h"
- #include <opencv2/opencv.hpp>
- #include <opencv2/highgui/highgui.hpp>
- #define MAIN_TEXTURE_WIDTH 512
- #define MAIN_TEXTURE_HEIGHT 512
- //entry point
- int main(int argc, const char **argv)
- {
- //how many detail levels (1 = just the capture res, > 1 goes down by half each level, 4 max)
- int num_levels = 4;
- //init graphics and the camera
- InitGraphics();
- CCamera* cam = StartCamera(MAIN_TEXTURE_WIDTH, MAIN_TEXTURE_HEIGHT,30,num_levels,true);
- //create MOG background substractor model
- //BackgroundSubtractorMOG bg_model;
- //create 4 textures of decreasing size
- GfxTexture textures[4];
- for(int texidx = 0; texidx < num_levels; texidx++)
- textures[texidx].Create(MAIN_TEXTURE_WIDTH >> texidx, MAIN_TEXTURE_HEIGHT >> texidx);
- printf("Running frame loop\n");
- for(;;)
- {
- //lock the chosen frame buffer, and copy it directly into the corresponding open gl texture
- const void* frame_data; int frame_sz;
- if(cam->BeginReadFrame(1,frame_data,frame_sz))
- {
- //if doing argb conversion the frame data will be exactly the right size so just set directly
- textures[1].SetPixels(frame_data);
- char* frame_data_copy = new char[frame_sz]; // create a buffer of same size in bytes
- std::memcpy(frame_data_copy, frame_data, frame_sz); // copy data from frame_data to frame_data_copy
- cv::Mat TempMat(MAIN_TEXTURE_HEIGHT, MAIN_TEXTURE_WIDTH, CV_8UC4, frame_data_copy); //create cv::Mat
- delete [] frame_data_copy; //free allocated memory for copy, this also invalidates the data of TempMat
- imshow("Camera Feed",TempMat);
- cam->EndReadFrame(1);
- }
- //begin frame, draw the texture then end frame (the bit of maths just fits the image to the screen while maintaining aspect ratio)
- BeginFrame();
- float aspect_ratio = float(MAIN_TEXTURE_WIDTH)/float(MAIN_TEXTURE_HEIGHT);
- float screen_aspect_ratio = 1280.f/720.f;
- DrawTextureRect(&textures[1],-aspect_ratio/screen_aspect_ratio,-1.f,aspect_ratio/screen_aspect_ratio,1.f);
- EndFrame();
- int c = cv::waitKey(30);
- if (c == 'q' || c == 'Q' || (c & 255) == 27)
- break;
- }
- StopCamera();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement