Advertisement
jack06215

[OpenCV] DISOpticalFlow

Jul 17th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include "barter.h"
  2. #include "fps_stats.hpp"
  3. #include <opencv2/core/utility.hpp>
  4. #include <opencv2/highgui.hpp>
  5. #include <opencv2/imgproc.hpp>
  6. #include <opencv2/video.hpp>
  7. #include <string>
  8. #include <list>
  9. #include <thread>
  10. #include <iostream>
  11.  
  12. enum ThreadState {
  13.     Pause, Running, Quit
  14. };
  15.  
  16. std::list<cv::Mat> frames;
  17. cv::VideoCapture cap;
  18. barter<cv::Mat> frame_barter;
  19. std::atomic_int t_state{ ThreadState::Pause };
  20.  
  21. // thread function for video getting and show
  22. void StreamThread(barter<cv::Mat>& frame_barter_, std::atomic_int& t_state_)
  23. {
  24.     auto frame = std::make_unique<cv::Mat>();
  25.     auto grey = std::make_unique<cv::Mat>();
  26.     auto prevGrey = std::make_unique<cv::Mat>();
  27.     cv::Mat flow, flow_uv[2];
  28.     cv::Mat mag, ang;
  29.     cv::Mat hsv_split[3], hsv;
  30.     cv::Ptr<cv::DenseOpticalFlow> denseOpticalFlow = cv::DISOpticalFlow::create(cv::DISOpticalFlow::PRESET_MEDIUM);
  31.  
  32.     for (;;) {
  33.         // wait for a new frame to arrive
  34.         while (t_state_ != ThreadState::Running) {
  35.  
  36.             if (t_state_ == ThreadState::Quit) return;
  37.             std::this_thread::sleep_for(std::chrono::microseconds(30));
  38.         }
  39.         frame_barter_.exchange(frame);
  40.         CV_Assert(frame);
  41.         if (frame->empty()) continue;
  42.  
  43.         // do the actual stuff
  44.         cv::cvtColor(*frame, *grey, cv::COLOR_RGB2GRAY);
  45.         if (!prevGrey->empty())
  46.         {
  47.             cv::Mat result;
  48.             denseOpticalFlow->calc(*prevGrey, *grey, flow);
  49.             cv::split(flow, flow_uv);
  50.             cv::multiply(flow_uv[1], -1, flow_uv[1]);
  51.             cv::cartToPolar(flow_uv[0], flow_uv[1], mag, ang, true);
  52.             cv::normalize(mag, mag, 0, 1, cv::NORM_MINMAX);
  53.             hsv_split[0] = ang;
  54.             hsv_split[1] = mag;
  55.             hsv_split[2] = cv::Mat::ones(ang.size(), ang.type());
  56.             cv::merge(hsv_split, 3, hsv);
  57.             cv::cvtColor(hsv, result, cv::COLOR_HSV2BGR);
  58.             cv::imshow("result", result);
  59.             cv::waitKey(30);
  60.         }
  61.         std::swap(*prevGrey, *grey);
  62.     }
  63. }
  64.  
  65.  
  66. int main(int, char) {
  67.    
  68.     // Open camera
  69.     cap.open(0);
  70.     if (!cap.isOpened()) {
  71.         std::cerr << "Stream open failed : " << std::endl;
  72.         return 1;
  73.     }
  74.  
  75.     // multi-threading setup
  76.     cv::setNumThreads(8);
  77.     std::thread t1{ &StreamThread, std::ref(frame_barter), std::ref(t_state) };
  78.  
  79.     auto frame = std::unique_ptr<cv::Mat>(new cv::Mat);
  80.     std::this_thread::sleep_for(std::chrono::seconds(1));
  81.     std::cout << "cv::getNumThreads(): " << cv::getNumThreads() << std::endl;
  82.  
  83.     fps_stats fps{ "Video" };
  84.  
  85.     cap >> *frame;
  86.     while (!frame->empty()) {
  87.         cv::imshow("Original", *frame);
  88.  
  89.         frame_barter.exchange(frame);
  90.         t_state = ThreadState::Running;
  91.  
  92.         if (cv::waitKey(30) >= 0) {
  93.             break;
  94.         }
  95.         cap >> *frame;
  96.         fps.tick();
  97.     }
  98.  
  99.     if (t1.joinable()) {
  100.         t_state = ThreadState::Quit;
  101.         t1.join();
  102.         std::cout << "t1 is joined" << std::endl;
  103.     }
  104.  
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement