2 * openvc_trail_effect - experiments about video trail effects
4 * Copyright (C) 2015 Antonio Ospite <ao2@ao2.it>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef SEGMENTATION_HPP
21 #define SEGMENTATION_HPP
23 #include <opencv2/opencv.hpp>
27 virtual cv::Mat getForegroundMask(const cv::Mat& frame) = 0;
28 virtual ~Segmentation() {}
31 class DummySegmentation : public Segmentation {
33 cv::Mat getForegroundMask(const cv::Mat& frame)
35 return cv::Mat(frame.size(), CV_8UC1, cv::Scalar(1));
39 class ThresholdSegmentation : public Segmentation {
44 ThresholdSegmentation(int threshold_value)
46 threshold = threshold_value;
49 cv::Mat getForegroundMask(const cv::Mat& frame)
54 cvtColor(frame, gray_frame, CV_RGB2GRAY);
55 cv::threshold(gray_frame, frame_mask, threshold, 255, CV_THRESH_TOZERO);
62 * Some background on... background subtraction can be found here:
63 * http://docs.opencv.org/master/d1/dc5/tutorial_background_subtraction.html
66 class MOG2Segmentation : public Segmentation {
68 MOG2Segmentation(cv::VideoCapture& inputVideo, int learning_frames)
71 cv::Mat foreground_mask;
72 pMOG2 = cv::createBackgroundSubtractorMOG2();
74 for (int i = 0; i < learning_frames; i++) {
75 inputVideo >> background;
76 pMOG2->apply(background, foreground_mask);
80 cv::Mat getForegroundMask(const cv::Mat& frame)
82 cv::Mat foreground_mask;
84 pMOG2->apply(frame, foreground_mask, 0);
85 cv::erode(foreground_mask, foreground_mask, cv::Mat());
86 cv::dilate(foreground_mask, foreground_mask, cv::Mat());
87 cv::threshold(foreground_mask, foreground_mask, 0, 255, CV_THRESH_OTSU);
88 cv::medianBlur(foreground_mask, foreground_mask, 9);
90 return foreground_mask;
93 void getBackgroundImage(const cv::Mat& background)
95 pMOG2->getBackgroundImage(background);
99 cv::Ptr<cv::BackgroundSubtractor> pMOG2;
102 #endif // SEGMENTATION_HPP