SHARE
TWEET

blobdetection.hpp

a guest Jan 29th, 2020 86 in 198 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef DEF_BLOB_DETECTION
  2. #define DEF_BLOB_DETECTION
  3.  
  4. //#define USE_READ_PIXELS
  5. //#define ENABLE_READ_BACK
  6.  
  7. #ifndef USE_READ_PIXELS
  8. #include <interface/vcsm/user-vcsm.h>
  9. #endif
  10.  
  11. /* Includes & CPU stuff omitted */
  12.  
  13. static int maskW, maskH, mapW, mapH;
  14.  
  15. // Regions as read from the GPU memory
  16. typedef uint16_t BlobMapRegion;
  17. static BlobMapRegion *blobMapRegions;
  18.  
  19. // Intermediate render targets
  20. #ifdef USE_READ_PIXELS
  21. static FrameRenderTarget *blobMask, *blobMap;
  22. #else
  23. static FrameRenderTarget *blobMask;
  24. static VCSMRenderTarget *blobMap;
  25. #endif
  26.  
  27. static void initBlobDetection (int width, int height, EGL_Setup eglSetup)
  28. {
  29.     maskW = width;
  30.     maskH = height;
  31.     mapW = maskW / 4;
  32.     mapH = maskH / 4;
  33.  
  34.     /* Init of CPU stuff omitted */
  35.  
  36. #ifdef USE_READ_PIXELS
  37.     // Setup intermediate Render Targets
  38.     blobMask = new FrameRenderTarget(maskW, maskH, GL_RGBA, GL_UNSIGNED_BYTE);
  39.     blobMap = new FrameRenderTarget(mapW/2, mapH, GL_RGBA, GL_UNSIGNED_BYTE);
  40.     // Allocate memory for regions map read back from the GPU
  41.     blobMapRegions = (BlobMapRegion*)malloc(mapW * mapH * 2);
  42. #else
  43.     // Setup Render Targets in Shared Memory
  44.     vcsm_init();
  45.  
  46.     // Switch type of render target between texture and VCSM
  47.     blobMask = new FrameRenderTarget(maskW, maskH, GL_RGBA, GL_UNSIGNED_BYTE);
  48.     //blobMask = new VCSMRenderTarget(maskW, maskH, eglSetup.display);
  49.     blobMap = new VCSMRenderTarget(mapW/2, mapH, eglSetup.display);
  50.     //blobMap = new FrameRenderTarget(mapW/2, mapH, GL_RGBA, GL_UNSIGNED_BYTE);
  51. #endif
  52. }
  53.  
  54.  
  55. static void performBlobDetection(CamGL_Frame *frame, std::vector<Cluster> &blobs)
  56. {
  57.     // -- GPU PART --
  58.  
  59.     // Render from camera frame source to blobMask
  60.     shaderESBlobDetect->use();
  61.     frame->setSource(shaderESBlobDetect, 0);
  62.     blobMask->setTarget();
  63.     SSQuad->draw();
  64.  
  65.     // Encode binary blob flag in source alpha into regions of full color
  66.     // Each region is 4x4 and stores 4bit per channel in 4 channels
  67.     shaderESBlobEncode->use();
  68.     blobMask->setSource(shaderESBlobEncode, 0);
  69.     blobMap->setTarget();
  70.     SSQuad->draw();
  71.  
  72.     // -- CPU PART --
  73.  
  74. #ifdef ENABLE_READ_BACK
  75.  
  76.     int xMin = 0, xMax = mapW, yMin = 0, yMax = mapH;
  77.     int bufW = mapW, bufH = mapH;
  78.  
  79. #ifdef USE_READ_PIXELS
  80.     // Read back encoded regions map from GPU memory
  81.     glReadPixels(0, 0, mapW/2, mapH, GL_RGBA, GL_UNSIGNED_BYTE, blobMapRegions);
  82. #else
  83.     // Wait for current GL operations to finish
  84.     glFinish();
  85.     // Lock the blobMap shared memory so CPU can access it
  86.     blobMapRegions = (BlobMapRegion*)blobMap->lock();
  87.     bufW = blobMap->bufferWidth*2;
  88.     bufH = blobMap->bufferHeight;
  89. #endif
  90.  
  91.     /* Processing omitted */
  92.  
  93. #ifndef USE_READ_PIXELS
  94.     blobMap->unlock();
  95. #endif
  96.  
  97. #else // ENABLE_READ_BACK
  98.     glFinish();
  99. #endif // ENABLE_READ_BACK
  100. }
  101.  
  102. #endif
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top