mirror of
https://github.com/opencv/opencv_contrib.git
synced 2025-10-18 17:24:28 +08:00
Merge pull request #2255 from sturkmen72:patch-4
This commit is contained in:
@@ -8,15 +8,15 @@ using namespace cv;
|
||||
using namespace cv::bgsegm;
|
||||
|
||||
const String about =
|
||||
"\nA program demonstrating the use and capabilities of different background subtraction algrorithms\n"
|
||||
"\nA program demonstrating the use and capabilities of different background subtraction algorithms\n"
|
||||
"Using OpenCV version " + String(CV_VERSION) +
|
||||
"\nPress q or ESC to exit\n";
|
||||
"\n\nPress 'c' to change the algorithm"
|
||||
"\nPress 'm' to toggle showing only foreground mask or ghost effect"
|
||||
"\nPress 'n' to change number of threads"
|
||||
"\nPress SPACE to toggle wait delay of imshow"
|
||||
"\nPress 'q' or ESC to exit\n";
|
||||
|
||||
const String keys =
|
||||
"{help h usage ? | | print this message }"
|
||||
"{vid | | path to a video file }"
|
||||
"{algo | GMG | name of the algorithm (GMG, CNT, KNN, MOG, MOG2) }"
|
||||
;
|
||||
const String algos[7] = { "GMG", "CNT", "KNN", "MOG", "MOG2", "GSOC", "LSBP" };
|
||||
|
||||
static Ptr<BackgroundSubtractor> createBGSubtractorByName(const String& algoName)
|
||||
{
|
||||
@@ -31,42 +31,26 @@ static Ptr<BackgroundSubtractor> createBGSubtractorByName(const String& algoName
|
||||
algo = createBackgroundSubtractorMOG();
|
||||
else if(algoName == String("MOG2"))
|
||||
algo = createBackgroundSubtractorMOG2();
|
||||
else if(algoName == String("GSOC"))
|
||||
algo = createBackgroundSubtractorGSOC();
|
||||
else if(algoName == String("LSBP"))
|
||||
algo = createBackgroundSubtractorLSBP();
|
||||
|
||||
return algo;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
CommandLineParser parser(argc, argv, keys);
|
||||
CommandLineParser parser(argc, argv, "{@video | vtest.avi | path to a video file}");
|
||||
parser.about(about);
|
||||
parser.printMessage();
|
||||
if (parser.has("help"))
|
||||
{
|
||||
parser.printMessage();
|
||||
return 0;
|
||||
}
|
||||
|
||||
String videoPath = parser.get<String>("vid");
|
||||
String algoName = parser.get<String>("algo");
|
||||
String videoPath = samples::findFile(parser.get<String>(0),false);
|
||||
|
||||
if (!parser.check())
|
||||
{
|
||||
parser.printErrors();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Ptr<BackgroundSubtractor> bgfs = createBGSubtractorByName(algoName);
|
||||
if (!bgfs)
|
||||
{
|
||||
std::cerr << "Failed to create " << algoName << " background subtractor" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
Ptr<BackgroundSubtractor> bgfs = createBGSubtractorByName(algos[0]);
|
||||
|
||||
VideoCapture cap;
|
||||
if (argc > 1)
|
||||
cap.open(videoPath);
|
||||
else
|
||||
cap.open(0);
|
||||
cap.open(videoPath);
|
||||
|
||||
if (!cap.isOpened())
|
||||
{
|
||||
@@ -76,24 +60,63 @@ int main(int argc, char** argv)
|
||||
|
||||
Mat frame, fgmask, segm;
|
||||
|
||||
namedWindow("FG Segmentation", WINDOW_NORMAL);
|
||||
int delay = 30;
|
||||
int algo_index = 0;
|
||||
int nthreads = getNumberOfCPUs();
|
||||
bool show_fgmask = false;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
cap >> frame;
|
||||
|
||||
if (frame.empty())
|
||||
break;
|
||||
{
|
||||
cap.set(CAP_PROP_POS_FRAMES, 0);
|
||||
cap >> frame;
|
||||
}
|
||||
|
||||
bgfs->apply(frame, fgmask);
|
||||
|
||||
frame.convertTo(segm, CV_8U, 0.5);
|
||||
add(frame, Scalar(100, 100, 0), segm, fgmask);
|
||||
if (show_fgmask)
|
||||
segm = fgmask;
|
||||
else
|
||||
{
|
||||
frame.convertTo(segm, CV_8U, 0.5);
|
||||
add(frame, Scalar(100, 100, 0), segm, fgmask);
|
||||
}
|
||||
|
||||
putText(segm, algos[algo_index], Point(10, 30), FONT_HERSHEY_PLAIN, 2.0, Scalar(255, 0, 255), 2, LINE_AA);
|
||||
putText(segm, format("%d threads", nthreads), Point(10, 60), FONT_HERSHEY_PLAIN, 2.0, Scalar(255, 0, 255), 2, LINE_AA);
|
||||
|
||||
imshow("FG Segmentation", segm);
|
||||
|
||||
int c = waitKey(30);
|
||||
if (c == 'q' || c == 'Q' || (c & 255) == 27)
|
||||
int c = waitKey(delay);
|
||||
|
||||
if (c == ' ')
|
||||
delay = delay == 30 ? 1 : 30;
|
||||
|
||||
if (c == 'c' || c == 'C')
|
||||
{
|
||||
algo_index++;
|
||||
if ( algo_index > 6 )
|
||||
algo_index = 0;
|
||||
|
||||
bgfs = createBGSubtractorByName(algos[algo_index]);
|
||||
}
|
||||
|
||||
if (c == 'n' || c == 'N')
|
||||
{
|
||||
nthreads++;
|
||||
if ( nthreads > 8 )
|
||||
nthreads = 1;
|
||||
|
||||
setNumThreads(nthreads);
|
||||
}
|
||||
|
||||
if (c == 'm' || c == 'M')
|
||||
show_fgmask = !show_fgmask;
|
||||
|
||||
if (c == 'q' || c == 'Q' || c == 27)
|
||||
break;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user