tvl1_optical_flow.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include <iostream>
  2. #include <fstream>
  3. #include <opencv2/core/utility.hpp>
  4. #include "opencv2/video.hpp"
  5. #include "opencv2/optflow.hpp"
  6. #include "opencv2/imgcodecs.hpp"
  7. #include "opencv2/highgui.hpp"
  8. using namespace cv;
  9. using namespace std;
  10. using namespace optflow;
  11. inline bool isFlowCorrect(Point2f u)
  12. {
  13. return !cvIsNaN(u.x) && !cvIsNaN(u.y) && fabs(u.x) < 1e9 && fabs(u.y) < 1e9;
  14. }
  15. static Vec3b computeColor(float fx, float fy)
  16. {
  17. static bool first = true;
  18. // relative lengths of color transitions:
  19. // these are chosen based on perceptual similarity
  20. // (e.g. one can distinguish more shades between red and yellow
  21. // than between yellow and green)
  22. const int RY = 15;
  23. const int YG = 6;
  24. const int GC = 4;
  25. const int CB = 11;
  26. const int BM = 13;
  27. const int MR = 6;
  28. const int NCOLS = RY + YG + GC + CB + BM + MR;
  29. static Vec3i colorWheel[NCOLS];
  30. if (first)
  31. {
  32. int k = 0;
  33. for (int i = 0; i < RY; ++i, ++k)
  34. colorWheel[k] = Vec3i(255, 255 * i / RY, 0);
  35. for (int i = 0; i < YG; ++i, ++k)
  36. colorWheel[k] = Vec3i(255 - 255 * i / YG, 255, 0);
  37. for (int i = 0; i < GC; ++i, ++k)
  38. colorWheel[k] = Vec3i(0, 255, 255 * i / GC);
  39. for (int i = 0; i < CB; ++i, ++k)
  40. colorWheel[k] = Vec3i(0, 255 - 255 * i / CB, 255);
  41. for (int i = 0; i < BM; ++i, ++k)
  42. colorWheel[k] = Vec3i(255 * i / BM, 0, 255);
  43. for (int i = 0; i < MR; ++i, ++k)
  44. colorWheel[k] = Vec3i(255, 0, 255 - 255 * i / MR);
  45. first = false;
  46. }
  47. const float rad = sqrt(fx * fx + fy * fy);
  48. const float a = atan2(-fy, -fx) / (float)CV_PI;
  49. const float fk = (a + 1.0f) / 2.0f * (NCOLS - 1);
  50. const int k0 = static_cast<int>(fk);
  51. const int k1 = (k0 + 1) % NCOLS;
  52. const float f = fk - k0;
  53. Vec3b pix;
  54. for (int b = 0; b < 3; b++)
  55. {
  56. const float col0 = colorWheel[k0][b] / 255.f;
  57. const float col1 = colorWheel[k1][b] / 255.f;
  58. float col = (1 - f) * col0 + f * col1;
  59. if (rad <= 1)
  60. col = 1 - rad * (1 - col); // increase saturation with radius
  61. else
  62. col *= .75; // out of range
  63. pix[2 - b] = static_cast<uchar>(255.f * col);
  64. }
  65. return pix;
  66. }
  67. static void drawOpticalFlow(const Mat_<Point2f>& flow, Mat& dst, float maxmotion = -1)
  68. {
  69. dst.create(flow.size(), CV_8UC3);
  70. dst.setTo(Scalar::all(0));
  71. // determine motion range:
  72. float maxrad = maxmotion;
  73. if (maxmotion <= 0)
  74. {
  75. maxrad = 1;
  76. for (int y = 0; y < flow.rows; ++y)
  77. {
  78. for (int x = 0; x < flow.cols; ++x)
  79. {
  80. Point2f u = flow(y, x);
  81. if (!isFlowCorrect(u))
  82. continue;
  83. maxrad = max(maxrad, sqrt(u.x * u.x + u.y * u.y));
  84. }
  85. }
  86. }
  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. if (isFlowCorrect(u))
  93. dst.at<Vec3b>(y, x) = computeColor(u.x / maxrad, u.y / maxrad);
  94. }
  95. }
  96. }
  97. // binary file format for flow data specified here:
  98. // http://vision.middlebury.edu/flow/data/
  99. static void writeOpticalFlowToFile(const Mat_<Point2f>& flow, const string& fileName)
  100. {
  101. static const char FLO_TAG_STRING[] = "PIEH";
  102. ofstream file(fileName.c_str(), ios_base::binary);
  103. file << FLO_TAG_STRING;
  104. file.write((const char*) &flow.cols, sizeof(int));
  105. file.write((const char*) &flow.rows, sizeof(int));
  106. for (int i = 0; i < flow.rows; ++i)
  107. {
  108. for (int j = 0; j < flow.cols; ++j)
  109. {
  110. const Point2f u = flow(i, j);
  111. file.write((const char*) &u.x, sizeof(float));
  112. file.write((const char*) &u.y, sizeof(float));
  113. }
  114. }
  115. }
  116. int main(int argc, const char* argv[])
  117. {
  118. cv::CommandLineParser parser(argc, argv, "{help h || show help message}"
  119. "{ @frame0 | | frame 0}{ @frame1 | | frame 1}{ @output | | output flow}");
  120. if (parser.has("help"))
  121. {
  122. parser.printMessage();
  123. return 0;
  124. }
  125. string frame0_name = parser.get<string>("@frame0");
  126. string frame1_name = parser.get<string>("@frame1");
  127. string file = parser.get<string>("@output");
  128. if (frame0_name.empty() || frame1_name.empty() || file.empty())
  129. {
  130. cerr << "Usage : " << argv[0] << " [<frame0>] [<frame1>] [<output_flow>]" << endl;
  131. return -1;
  132. }
  133. Mat frame0 = imread(frame0_name, IMREAD_GRAYSCALE);
  134. Mat frame1 = imread(frame1_name, IMREAD_GRAYSCALE);
  135. if (frame0.empty())
  136. {
  137. cerr << "Can't open image [" << parser.get<string>("frame0") << "]" << endl;
  138. return -1;
  139. }
  140. if (frame1.empty())
  141. {
  142. cerr << "Can't open image [" << parser.get<string>("frame1") << "]" << endl;
  143. return -1;
  144. }
  145. if (frame1.size() != frame0.size())
  146. {
  147. cerr << "Images should be of equal sizes" << endl;
  148. return -1;
  149. }
  150. Mat_<Point2f> flow;
  151. Ptr<DualTVL1OpticalFlow> tvl1 = DualTVL1OpticalFlow::create();
  152. const double start = (double)getTickCount();
  153. tvl1->calc(frame0, frame1, flow);
  154. const double timeSec = (getTickCount() - start) / getTickFrequency();
  155. cout << "calcOpticalFlowDual_TVL1 : " << timeSec << " sec" << endl;
  156. Mat out;
  157. drawOpticalFlow(flow, out);
  158. if (!file.empty())
  159. writeOpticalFlowToFile(flow, file);
  160. imshow("Flow", out);
  161. waitKey();
  162. return 0;
  163. }