nvidia_optical_flow.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. #include <unordered_map>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <iterator>
  6. #include "opencv2/core.hpp"
  7. #include "opencv2/core/utility.hpp"
  8. #include "opencv2/highgui.hpp"
  9. #include "opencv2/imgproc.hpp"
  10. #include "opencv2/cudaoptflow.hpp"
  11. #include "opencv2/cudaarithm.hpp"
  12. #include "opencv2/video/tracking.hpp"
  13. using namespace cv;
  14. using namespace cv::cuda;
  15. //this function is taken from opencv/samples/gpu/optical_flow.cpp
  16. inline bool isFlowCorrect(Point2f u)
  17. {
  18. return !cvIsNaN(u.x) && !cvIsNaN(u.y) && fabs(u.x) < 1e9 && fabs(u.y) < 1e9;
  19. }
  20. //this function is taken from opencv/samples/gpu/optical_flow.cpp
  21. static Vec3b computeColor(float fx, float fy)
  22. {
  23. static bool first = true;
  24. // relative lengths of color transitions:
  25. // these are chosen based on perceptual similarity
  26. // (e.g. one can distinguish more shades between red and yellow
  27. // than between yellow and green)
  28. const int RY = 15;
  29. const int YG = 6;
  30. const int GC = 4;
  31. const int CB = 11;
  32. const int BM = 13;
  33. const int MR = 6;
  34. const int NCOLS = RY + YG + GC + CB + BM + MR;
  35. static Vec3i colorWheel[NCOLS];
  36. if (first)
  37. {
  38. int k = 0;
  39. for (int i = 0; i < RY; ++i, ++k)
  40. colorWheel[k] = Vec3i(255, 255 * i / RY, 0);
  41. for (int i = 0; i < YG; ++i, ++k)
  42. colorWheel[k] = Vec3i(255 - 255 * i / YG, 255, 0);
  43. for (int i = 0; i < GC; ++i, ++k)
  44. colorWheel[k] = Vec3i(0, 255, 255 * i / GC);
  45. for (int i = 0; i < CB; ++i, ++k)
  46. colorWheel[k] = Vec3i(0, 255 - 255 * i / CB, 255);
  47. for (int i = 0; i < BM; ++i, ++k)
  48. colorWheel[k] = Vec3i(255 * i / BM, 0, 255);
  49. for (int i = 0; i < MR; ++i, ++k)
  50. colorWheel[k] = Vec3i(255, 0, 255 - 255 * i / MR);
  51. first = false;
  52. }
  53. const float rad = sqrt(fx * fx + fy * fy);
  54. const float a = atan2(-fy, -fx) / (float)CV_PI;
  55. const float fk = (a + 1.0f) / 2.0f * (NCOLS - 1);
  56. const int k0 = static_cast<int>(fk);
  57. const int k1 = (k0 + 1) % NCOLS;
  58. const float f = fk - k0;
  59. Vec3b pix;
  60. for (int b = 0; b < 3; b++)
  61. {
  62. const float col0 = colorWheel[k0][b] / 255.0f;
  63. const float col1 = colorWheel[k1][b] / 255.0f;
  64. float col = (1 - f) * col0 + f * col1;
  65. if (rad <= 1)
  66. col = 1 - rad * (1 - col); // increase saturation with radius
  67. else
  68. col *= .75; // out of range
  69. pix[2 - b] = static_cast<uchar>(255.0 * col);
  70. }
  71. return pix;
  72. }
  73. //this function is taken from opencv/samples/gpu/optical_flow.cpp
  74. static void drawOpticalFlow(const Mat_<float>& flowx, const Mat_<float>& flowy
  75. , Mat& dst, float maxmotion = -1)
  76. {
  77. dst.create(flowx.size(), CV_8UC3);
  78. dst.setTo(Scalar::all(0));
  79. // determine motion range:
  80. float maxrad = maxmotion;
  81. if (maxmotion <= 0)
  82. {
  83. maxrad = 1;
  84. for (int y = 0; y < flowx.rows; ++y)
  85. {
  86. for (int x = 0; x < flowx.cols; ++x)
  87. {
  88. Point2f u(flowx(y, x), flowy(y, x));
  89. if (!isFlowCorrect(u))
  90. continue;
  91. maxrad = max(maxrad, sqrt(u.x * u.x + u.y * u.y));
  92. }
  93. }
  94. }
  95. for (int y = 0; y < flowx.rows; ++y)
  96. {
  97. for (int x = 0; x < flowx.cols; ++x)
  98. {
  99. Point2f u(flowx(y, x), flowy(y, x));
  100. if (isFlowCorrect(u))
  101. dst.at<Vec3b>(y, x) = computeColor(u.x / maxrad, u.y / maxrad);
  102. }
  103. }
  104. }
  105. /*
  106. ROI config file format.
  107. numrois 3
  108. roi0 640 96 1152 192
  109. roi1 640 64 896 864
  110. roi2 640 960 256 32
  111. */
  112. bool parseROI(std::string ROIFileName, std::vector<Rect>& roiData)
  113. {
  114. std::string str;
  115. uint32_t nRois = 0;
  116. std::ifstream hRoiFile;
  117. hRoiFile.open(ROIFileName, std::ios::in);
  118. if (hRoiFile.is_open())
  119. {
  120. while (std::getline(hRoiFile, str))
  121. {
  122. std::istringstream iss(str);
  123. std::vector<std::string> tokens{ std::istream_iterator<std::string>{iss},
  124. std::istream_iterator<std::string>{} };
  125. if (tokens.size() == 0) continue; // if empty line, coninue
  126. transform(tokens[0].begin(), tokens[0].end(), tokens[0].begin(), ::tolower);
  127. if (tokens[0] == "numrois")
  128. {
  129. nRois = atoi(tokens[1].data());
  130. }
  131. else if (tokens[0].rfind("roi", 0) == 0)
  132. {
  133. cv::Rect roi;
  134. roi.x = atoi(tokens[1].data());
  135. roi.y = atoi(tokens[2].data());
  136. roi.width = atoi(tokens[3].data());
  137. roi.height = atoi(tokens[4].data());
  138. roiData.push_back(roi);
  139. }
  140. else if (tokens[0].rfind("#", 0) == 0)
  141. {
  142. continue;
  143. }
  144. else
  145. {
  146. std::cout << "Unidentified keyword in roi config file " << tokens[0] << std::endl;
  147. hRoiFile.close();
  148. return false;
  149. }
  150. }
  151. }
  152. else
  153. {
  154. std::cout << "Unable to open ROI file " << std::endl;
  155. return false;
  156. }
  157. if (nRois != roiData.size())
  158. {
  159. std::cout << "NumRois(" << nRois << ")and specified roi rects (" << roiData.size() << ")are not matching " << std::endl;
  160. hRoiFile.close();
  161. return false;
  162. }
  163. hRoiFile.close();
  164. return true;
  165. }
  166. int main(int argc, char **argv)
  167. {
  168. std::unordered_map<std::string, NvidiaOpticalFlow_2_0::NVIDIA_OF_PERF_LEVEL> presetMap = {
  169. { "slow", NvidiaOpticalFlow_2_0::NVIDIA_OF_PERF_LEVEL::NV_OF_PERF_LEVEL_SLOW },
  170. { "medium", NvidiaOpticalFlow_2_0::NVIDIA_OF_PERF_LEVEL::NV_OF_PERF_LEVEL_MEDIUM },
  171. { "fast", NvidiaOpticalFlow_2_0::NVIDIA_OF_PERF_LEVEL::NV_OF_PERF_LEVEL_FAST } };
  172. std::unordered_map<int, NvidiaOpticalFlow_2_0::NVIDIA_OF_OUTPUT_VECTOR_GRID_SIZE> outputGridSize = {
  173. { 1, NvidiaOpticalFlow_2_0::NVIDIA_OF_OUTPUT_VECTOR_GRID_SIZE::NV_OF_OUTPUT_VECTOR_GRID_SIZE_1 },
  174. { 2, NvidiaOpticalFlow_2_0::NVIDIA_OF_OUTPUT_VECTOR_GRID_SIZE::NV_OF_OUTPUT_VECTOR_GRID_SIZE_2 },
  175. { 4, NvidiaOpticalFlow_2_0::NVIDIA_OF_OUTPUT_VECTOR_GRID_SIZE::NV_OF_OUTPUT_VECTOR_GRID_SIZE_4 } };
  176. std::unordered_map<int, NvidiaOpticalFlow_2_0::NVIDIA_OF_HINT_VECTOR_GRID_SIZE> hintGridSize = {
  177. { 1, NvidiaOpticalFlow_2_0::NVIDIA_OF_HINT_VECTOR_GRID_SIZE::NV_OF_HINT_VECTOR_GRID_SIZE_1 },
  178. { 2, NvidiaOpticalFlow_2_0::NVIDIA_OF_HINT_VECTOR_GRID_SIZE::NV_OF_HINT_VECTOR_GRID_SIZE_2 },
  179. { 4, NvidiaOpticalFlow_2_0::NVIDIA_OF_HINT_VECTOR_GRID_SIZE::NV_OF_HINT_VECTOR_GRID_SIZE_4 },
  180. { 8, NvidiaOpticalFlow_2_0::NVIDIA_OF_HINT_VECTOR_GRID_SIZE::NV_OF_HINT_VECTOR_GRID_SIZE_8 } };
  181. try
  182. {
  183. CommandLineParser cmd(argc, argv,
  184. "{ l left | ../data/basketball1.png | specify left image }"
  185. "{ r right | ../data/basketball2.png | specify right image }"
  186. "{ g gpuid | 0 | cuda device index}"
  187. "{ p preset | slow | perf preset for OF algo [ options : slow, medium, fast ]}"
  188. "{ og outputGridSize | 1 | Output grid size of OF vector [ options : 1, 2, 4 ]}"
  189. "{ hg hintGridSize | 1 | Hint grid size of OF vector [ options : 1, 2, 4, 8 ]}"
  190. "{ o output | OpenCVNvOF.flo | output flow vector file in middlebury format}"
  191. "{ rc roiConfigFile | | Region of Interest config file }"
  192. "{ th enableTemporalHints | false | Enable temporal hints}"
  193. "{ eh enableExternalHints | false | Enable external hints}"
  194. "{ cb enableCostBuffer | false | Enable output cost buffer}"
  195. "{ h help | | print help message }");
  196. cmd.about("Nvidia's optical flow sample.");
  197. if (cmd.has("help") || !cmd.check())
  198. {
  199. cmd.printMessage();
  200. cmd.printErrors();
  201. return 0;
  202. }
  203. std::string pathL = cmd.get<std::string>("left");
  204. std::string pathR = cmd.get<std::string>("right");
  205. std::string preset = cmd.get<std::string>("preset");
  206. std::string output = cmd.get<std::string>("output");
  207. std::string roiConfiFile = cmd.get<std::string>("roiConfigFile");
  208. bool enableExternalHints = cmd.get<bool>("enableExternalHints");
  209. bool enableTemporalHints = cmd.get<bool>("enableTemporalHints");
  210. bool enableCostBuffer = cmd.get<bool>("enableCostBuffer");
  211. int gpuId = cmd.get<int>("gpuid");
  212. int outputBufferGridSize = cmd.get<int>("outputGridSize");
  213. int hintBufferGridSize = cmd.get<int>("hintGridSize");
  214. if (pathL.empty()) std::cout << "Specify left image path" << std::endl;
  215. if (pathR.empty()) std::cout << "Specify right image path" << std::endl;
  216. if (preset.empty()) std::cout << "Specify perf preset for OpticalFlow algo" << std::endl;
  217. if (pathL.empty() || pathR.empty()) return 0;
  218. auto p = presetMap.find(preset);
  219. if (p == presetMap.end())
  220. {
  221. std::cout << "Invalid preset level : " << preset << std::endl;
  222. return 0;
  223. }
  224. NvidiaOpticalFlow_2_0::NVIDIA_OF_PERF_LEVEL perfPreset = p->second;
  225. auto o = outputGridSize.find(outputBufferGridSize);
  226. if (o == outputGridSize.end())
  227. {
  228. std::cout << "Invalid output grid size: " << outputBufferGridSize << std::endl;
  229. return 0;
  230. }
  231. NvidiaOpticalFlow_2_0::NVIDIA_OF_OUTPUT_VECTOR_GRID_SIZE outBufGridSize = o->second;
  232. NvidiaOpticalFlow_2_0::NVIDIA_OF_HINT_VECTOR_GRID_SIZE hintBufGridSize =
  233. NvidiaOpticalFlow_2_0::NV_OF_HINT_VECTOR_GRID_SIZE_UNDEFINED;
  234. if (enableExternalHints)
  235. {
  236. auto h = hintGridSize.find(hintBufferGridSize);
  237. if (h == hintGridSize.end())
  238. {
  239. std::cout << "Invalid hint grid size: " << hintBufferGridSize << std::endl;
  240. return 0;
  241. }
  242. hintBufGridSize = h->second;
  243. }
  244. std::vector<Rect> roiData;
  245. if (!roiConfiFile.empty())
  246. {
  247. if (!parseROI(roiConfiFile, roiData))
  248. {
  249. std::cout << "Wrong Region of Interest config file, proceeding without ROI" << std::endl;
  250. }
  251. }
  252. Mat frameL = imread(pathL, IMREAD_GRAYSCALE);
  253. Mat frameR = imread(pathR, IMREAD_GRAYSCALE);
  254. if (frameL.empty()) std::cout << "Can't open '" << pathL << "'" << std::endl;
  255. if (frameR.empty()) std::cout << "Can't open '" << pathR << "'" << std::endl;
  256. if (frameL.empty() || frameR.empty()) return -1;
  257. Ptr<NvidiaOpticalFlow_2_0> nvof = NvidiaOpticalFlow_2_0::create(
  258. frameL.size(), roiData, perfPreset, outBufGridSize, hintBufGridSize,
  259. enableTemporalHints, enableExternalHints, enableCostBuffer, gpuId);
  260. Mat flowx, flowy, flowxy, floatFlow, image;
  261. nvof->calc(frameL, frameR, flowxy);
  262. nvof->convertToFloat(flowxy, floatFlow);
  263. if (!output.empty())
  264. {
  265. if (!writeOpticalFlow(output, floatFlow))
  266. std::cout << "Failed to save Flow Vector" << std::endl;
  267. else
  268. std::cout << "Flow vector saved as '" << output << "'" << std::endl;
  269. }
  270. Mat planes[] = { flowx, flowy };
  271. split(floatFlow, planes);
  272. flowx = planes[0]; flowy = planes[1];
  273. drawOpticalFlow(flowx, flowy, image, 10);
  274. imshow("Colorize image", image);
  275. waitKey(0);
  276. nvof->collectGarbage();
  277. }
  278. catch (const std::exception &ex)
  279. {
  280. std::cout << ex.what() << std::endl;
  281. return 1;
  282. }
  283. return 0;
  284. }