optical_flow.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #include <iostream>
  2. #include <fstream>
  3. #include "opencv2/core.hpp"
  4. #include <opencv2/core/utility.hpp>
  5. #include "opencv2/highgui.hpp"
  6. #include "opencv2/cudaoptflow.hpp"
  7. #include "opencv2/cudaarithm.hpp"
  8. using namespace std;
  9. using namespace cv;
  10. using namespace cv::cuda;
  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.0f;
  57. const float col1 = colorWheel[k1][b] / 255.0f;
  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.0 * col);
  64. }
  65. return pix;
  66. }
  67. static void drawOpticalFlow(const Mat_<float>& flowx, const Mat_<float>& flowy, Mat& dst, float maxmotion = -1)
  68. {
  69. dst.create(flowx.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 < flowx.rows; ++y)
  77. {
  78. for (int x = 0; x < flowx.cols; ++x)
  79. {
  80. Point2f u(flowx(y, x), flowy(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 < flowx.rows; ++y)
  88. {
  89. for (int x = 0; x < flowx.cols; ++x)
  90. {
  91. Point2f u(flowx(y, x), flowy(y, x));
  92. if (isFlowCorrect(u))
  93. dst.at<Vec3b>(y, x) = computeColor(u.x / maxrad, u.y / maxrad);
  94. }
  95. }
  96. }
  97. static void showFlow(const char* name, const GpuMat& d_flow)
  98. {
  99. GpuMat planes[2];
  100. cuda::split(d_flow, planes);
  101. Mat flowx(planes[0]);
  102. Mat flowy(planes[1]);
  103. Mat out;
  104. drawOpticalFlow(flowx, flowy, out, 10);
  105. imshow(name, out);
  106. }
  107. int main(int argc, const char* argv[])
  108. {
  109. string filename1, filename2;
  110. if (argc < 3)
  111. {
  112. cerr << "Usage : " << argv[0] << " <frame0> <frame1>" << endl;
  113. filename1 = "../data/basketball1.png";
  114. filename2 = "../data/basketball2.png";
  115. }
  116. else
  117. {
  118. filename1 = argv[1];
  119. filename2 = argv[2];
  120. }
  121. Mat frame0 = imread(filename1, IMREAD_GRAYSCALE);
  122. Mat frame1 = imread(filename2, IMREAD_GRAYSCALE);
  123. if (frame0.empty())
  124. {
  125. cerr << "Can't open image [" << filename1 << "]" << endl;
  126. return -1;
  127. }
  128. if (frame1.empty())
  129. {
  130. cerr << "Can't open image [" << filename2 << "]" << endl;
  131. return -1;
  132. }
  133. if (frame1.size() != frame0.size())
  134. {
  135. cerr << "Images should be of equal sizes" << endl;
  136. return -1;
  137. }
  138. GpuMat d_frame0(frame0);
  139. GpuMat d_frame1(frame1);
  140. GpuMat d_flow(frame0.size(), CV_32FC2), d_flowxy;
  141. Stream inputStream, outputStream;
  142. Ptr<cuda::BroxOpticalFlow> brox = cuda::BroxOpticalFlow::create(0.197f, 50.0f, 0.8f, 10, 77, 10);
  143. Ptr<cuda::DensePyrLKOpticalFlow> lk = cuda::DensePyrLKOpticalFlow::create(Size(7, 7));
  144. Ptr<cuda::FarnebackOpticalFlow> farn = cuda::FarnebackOpticalFlow::create();
  145. Ptr<cuda::OpticalFlowDual_TVL1> tvl1 = cuda::OpticalFlowDual_TVL1::create();
  146. Ptr<cuda::NvidiaOpticalFlow_1_0> nvof_1_0 = cuda::NvidiaOpticalFlow_1_0::create(frame0.size(),
  147. NvidiaOpticalFlow_1_0::NVIDIA_OF_PERF_LEVEL::NV_OF_PERF_LEVEL_FAST, false, false, false, 0, inputStream, outputStream);
  148. Ptr<cuda::NvidiaOpticalFlow_2_0> nvof_2_0 = cuda::NvidiaOpticalFlow_2_0::create(frame0.size(),
  149. NvidiaOpticalFlow_2_0::NVIDIA_OF_PERF_LEVEL::NV_OF_PERF_LEVEL_FAST, NvidiaOpticalFlow_2_0::NVIDIA_OF_OUTPUT_VECTOR_GRID_SIZE::NV_OF_OUTPUT_VECTOR_GRID_SIZE_1,
  150. NvidiaOpticalFlow_2_0::NVIDIA_OF_HINT_VECTOR_GRID_SIZE::NV_OF_HINT_VECTOR_GRID_SIZE_UNDEFINED, false, false, false, 0, inputStream, outputStream);
  151. {
  152. GpuMat d_frame0f;
  153. GpuMat d_frame1f;
  154. d_frame0.convertTo(d_frame0f, CV_32F, 1.0 / 255.0);
  155. d_frame1.convertTo(d_frame1f, CV_32F, 1.0 / 255.0);
  156. const int64 start = getTickCount();
  157. brox->calc(d_frame0f, d_frame1f, d_flow);
  158. const double timeSec = (getTickCount() - start) / getTickFrequency();
  159. cout << "Brox : " << timeSec << " sec" << endl;
  160. showFlow("Brox", d_flow);
  161. }
  162. {
  163. const int64 start = getTickCount();
  164. lk->calc(d_frame0, d_frame1, d_flow);
  165. const double timeSec = (getTickCount() - start) / getTickFrequency();
  166. cout << "LK : " << timeSec << " sec" << endl;
  167. showFlow("LK", d_flow);
  168. }
  169. {
  170. const int64 start = getTickCount();
  171. farn->calc(d_frame0, d_frame1, d_flow);
  172. const double timeSec = (getTickCount() - start) / getTickFrequency();
  173. cout << "Farn : " << timeSec << " sec" << endl;
  174. showFlow("Farn", d_flow);
  175. }
  176. {
  177. const int64 start = getTickCount();
  178. tvl1->calc(d_frame0, d_frame1, d_flow);
  179. const double timeSec = (getTickCount() - start) / getTickFrequency();
  180. cout << "TVL1 : " << timeSec << " sec" << endl;
  181. showFlow("TVL1", d_flow);
  182. }
  183. {
  184. //The timing displayed below includes the time taken to copy the input buffers to the OF CUDA input buffers
  185. //and to copy the output buffers from the OF CUDA output buffer to the output buffer.
  186. //Hence it is expected to be more than what is displayed in the NVIDIA Optical Flow SDK documentation.
  187. const int64 start = getTickCount();
  188. nvof_1_0->calc(d_frame0, d_frame1, d_flowxy);
  189. const double timeSec = (getTickCount() - start) / getTickFrequency();
  190. cout << "NVIDIAOpticalFlow_1_0 : " << timeSec << " sec" << endl;
  191. nvof_1_0->upSampler(d_flowxy, frame0.size(), nvof_1_0->getGridSize(), d_flow);
  192. showFlow("NVIDIAOpticalFlow_1_0", d_flow);
  193. nvof_1_0->collectGarbage();
  194. }
  195. {
  196. //The timing displayed below includes the time taken to copy the input buffers to the OF CUDA input buffers
  197. //and to copy the output buffers from the OF CUDA output buffer to the output buffer.
  198. //Hence it is expected to be more than what is displayed in the NVIDIA Optical Flow SDK documentation.
  199. const int64 start = getTickCount();
  200. nvof_2_0->calc(d_frame0, d_frame1, d_flowxy);
  201. const double timeSec = (getTickCount() - start) / getTickFrequency();
  202. cout << "NVIDIAOpticalFlow_2_0 : " << timeSec << " sec" << endl;
  203. nvof_2_0->convertToFloat(d_flowxy, d_flow);
  204. showFlow("NVIDIAOpticalFlow_2_0", d_flow);
  205. nvof_2_0->collectGarbage();
  206. }
  207. imshow("Frame 0", frame0);
  208. imshow("Frame 1", frame1);
  209. waitKey();
  210. return 0;
  211. }