Advertisement
Guest User

Untitled

a guest
May 20th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. cv::Mat background, current_result;
  2.  
  3. void Refresh(const cv::Mat* img) {
  4.     cv::cvtColor(*img, background, cv::COLOR_BGR2GRAY);
  5.     cv::GaussianBlur(background, background, cv::Size(21, 21), 0);
  6. }
  7.  
  8.  
  9. bool ProcessImage(const cv::Mat* img) {
  10.     cv::Mat gray, frameDelta, thresh;
  11.     std::vector<std::vector<cv::Point> > cnts;
  12.     cv::cvtColor(*img, gray, cv::COLOR_BGR2GRAY);
  13.     cv::GaussianBlur(gray, gray, cv::Size(21, 21), 0);
  14.     //compute difference between first frame and current frame
  15.     cv::absdiff(background, gray, frameDelta);
  16.     cv::threshold(frameDelta, thresh, settings_.threshold, 255, cv::THRESH_BINARY);
  17.     cv::dilate(thresh, thresh, cv::Mat(), cv::Point(-1, -1), 1);
  18.     cv::findContours(thresh, cnts, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
  19.     current_result = img->clone();
  20.     for (int i = 0; i < cnts.size(); i++) {
  21.         std::pair<int, int> x = { img->size().width, 0 }, y = { img->size().height, 0 };
  22.         for (int z = 0; z < cnts[i].size(); ++z) {
  23.             x.first = std::min(cnts[i][z].x, x.first);
  24.             x.second = std::max(cnts[i][z].x, x.second);
  25.             y.first = std::min(cnts[i][z].y, y.first);
  26.             y.second = std::max(cnts[i][z].y, y.second);
  27.         }
  28.         cv::Rect bb(x.first, y.first, x.second - x.first, y.second - y.first);
  29.         if (bb.area() < settings_.lower_area) continue;
  30.         if (bb.area() > settings_.upper_area) continue;
  31.         cv::rectangle(current_result, bb, cv::Scalar(0, 0, 255));
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement