Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cv::Mat background, current_result;
- void Refresh(const cv::Mat* img) {
- cv::cvtColor(*img, background, cv::COLOR_BGR2GRAY);
- cv::GaussianBlur(background, background, cv::Size(21, 21), 0);
- }
- bool ProcessImage(const cv::Mat* img) {
- cv::Mat gray, frameDelta, thresh;
- std::vector<std::vector<cv::Point> > cnts;
- cv::cvtColor(*img, gray, cv::COLOR_BGR2GRAY);
- cv::GaussianBlur(gray, gray, cv::Size(21, 21), 0);
- //compute difference between first frame and current frame
- cv::absdiff(background, gray, frameDelta);
- cv::threshold(frameDelta, thresh, settings_.threshold, 255, cv::THRESH_BINARY);
- cv::dilate(thresh, thresh, cv::Mat(), cv::Point(-1, -1), 1);
- cv::findContours(thresh, cnts, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
- current_result = img->clone();
- for (int i = 0; i < cnts.size(); i++) {
- std::pair<int, int> x = { img->size().width, 0 }, y = { img->size().height, 0 };
- for (int z = 0; z < cnts[i].size(); ++z) {
- x.first = std::min(cnts[i][z].x, x.first);
- x.second = std::max(cnts[i][z].x, x.second);
- y.first = std::min(cnts[i][z].y, y.first);
- y.second = std::max(cnts[i][z].y, y.second);
- }
- cv::Rect bb(x.first, y.first, x.second - x.first, y.second - y.first);
- if (bb.area() < settings_.lower_area) continue;
- if (bb.area() > settings_.upper_area) continue;
- cv::rectangle(current_result, bb, cv::Scalar(0, 0, 255));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement