SHARE
TWEET

drawOpticalFlow

jet47 Dec 3rd, 2012 316 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. inline bool isFlowCorrect(Point2f u)
  2. {
  3.     return !cvIsNaN(u.x) && !cvIsNaN(u.y) && fabs(u.x) < 1e9 && fabs(u.y) < 1e9;
  4. }
  5.  
  6. static Vec3b computeColor(float fx, float fy)
  7. {
  8.     static bool first = true;
  9.  
  10.     // relative lengths of color transitions:
  11.     // these are chosen based on perceptual similarity
  12.     // (e.g. one can distinguish more shades between red and yellow
  13.     //  than between yellow and green)
  14.     const int RY = 15;
  15.     const int YG = 6;
  16.     const int GC = 4;
  17.     const int CB = 11;
  18.     const int BM = 13;
  19.     const int MR = 6;
  20.     const int NCOLS = RY + YG + GC + CB + BM + MR;
  21.     static Vec3i colorWheel[NCOLS];
  22.  
  23.     if (first)
  24.     {
  25.         int k = 0;
  26.  
  27.         for (int i = 0; i < RY; ++i, ++k)
  28.             colorWheel[k] = Vec3i(255, 255 * i / RY, 0);
  29.  
  30.         for (int i = 0; i < YG; ++i, ++k)
  31.             colorWheel[k] = Vec3i(255 - 255 * i / YG, 255, 0);
  32.  
  33.         for (int i = 0; i < GC; ++i, ++k)
  34.             colorWheel[k] = Vec3i(0, 255, 255 * i / GC);
  35.  
  36.         for (int i = 0; i < CB; ++i, ++k)
  37.             colorWheel[k] = Vec3i(0, 255 - 255 * i / CB, 255);
  38.  
  39.         for (int i = 0; i < BM; ++i, ++k)
  40.             colorWheel[k] = Vec3i(255 * i / BM, 0, 255);
  41.  
  42.         for (int i = 0; i < MR; ++i, ++k)
  43.             colorWheel[k] = Vec3i(255, 0, 255 - 255 * i / MR);
  44.  
  45.         first = false;
  46.     }
  47.  
  48.     const float rad = sqrt(fx * fx + fy * fy);
  49.     const float a = atan2(-fy, -fx) / (float)CV_PI;
  50.  
  51.     const float fk = (a + 1.0f) / 2.0f * (NCOLS - 1);
  52.     const int k0 = static_cast<int>(fk);
  53.     const int k1 = (k0 + 1) % NCOLS;
  54.     const float f = fk - k0;
  55.  
  56.     Vec3b pix;
  57.  
  58.     for (int b = 0; b < 3; b++)
  59.     {
  60.         const float col0 = colorWheel[k0][b] / 255.f;
  61.         const float col1 = colorWheel[k1][b] / 255.f;
  62.  
  63.         float col = (1 - f) * col0 + f * col1;
  64.  
  65.         if (rad <= 1)
  66.             col = 1 - rad * (1 - col); // increase saturation with radius
  67.         else
  68.             col *= .75; // out of range
  69.  
  70.         pix[2 - b] = static_cast<uchar>(255.f * col);
  71.     }
  72.  
  73.     return pix;
  74. }
  75.  
  76. static void drawOpticalFlow(const Mat_<Point2f>& flow, Mat& dst, float maxmotion = -1)
  77. {
  78.     dst.create(flow.size(), CV_8UC3);
  79.     dst.setTo(Scalar::all(0));
  80.  
  81.     // determine motion range:
  82.     float maxrad = maxmotion;
  83.  
  84.     if (maxmotion <= 0)
  85.     {
  86.         maxrad = 1;
  87.         for (int y = 0; y < flow.rows; ++y)
  88.         {
  89.             for (int x = 0; x < flow.cols; ++x)
  90.             {
  91.                 Point2f u = flow(y, x);
  92.  
  93.                 if (!isFlowCorrect(u))
  94.                     continue;
  95.  
  96.                 maxrad = max(maxrad, sqrt(u.x * u.x + u.y * u.y));
  97.             }
  98.         }
  99.     }
  100.  
  101.     for (int y = 0; y < flow.rows; ++y)
  102.     {
  103.         for (int x = 0; x < flow.cols; ++x)
  104.         {
  105.             Point2f u = flow(y, x);
  106.  
  107.             if (isFlowCorrect(u))
  108.                 dst.at<Vec3b>(y, x) = computeColor(u.x / maxrad, u.y / maxrad);
  109.         }
  110.     }
  111. }
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
 
Top