Skip to content
Snippets Groups Projects
Commit 42d80cea authored by Raphael Verbücheln's avatar Raphael Verbücheln
Browse files

added backgroundextractor and backspace stops video tab releases

parent 39699bed
Branches dev-raphael
No related tags found
No related merge requests found
......@@ -4,6 +4,11 @@
*.pyc
*.swp
*.out
*.user
*.creator
*.config
*.includes
*.files
test-robotics
doc/*
.ycm_extra_conf.py
......
......@@ -34,3 +34,11 @@ target_link_libraries(track
ball_tracking
${Boost_LIBRARIES}
)
add_executable(bgdetect
tracking/bg_detector.cpp
)
target_link_libraries(bgdetect
ball_tracking
${Boost_LIBRARIES}
)
//opencv
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
#include <chrono>
//C++
#include <iostream>
#include <sstream>
using namespace cv;
using namespace std;
// Global variables
Mat frame; //current frame
Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
Ptr<BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
char keyboard; //input from keyboard
void help();
void processVideo(char* videoFilename);
void processImages(char* firstFrameFilename);
void help()
{
cout
<< "--------------------------------------------------------------------------" << endl
<< "This program shows how to use background subtraction methods provided by " << endl
<< " OpenCV. You can process both videos (-vid) and images (-img)." << endl
<< endl
<< "Usage:" << endl
<< "./bg_sub {-vid <video filename>|-img <image filename>}" << endl
<< "for example: ./bg_sub -vid video.avi" << endl
<< "or: ./bg_sub -img /data/images/1.png" << endl
<< "--------------------------------------------------------------------------" << endl
<< endl;
}
int main(int argc, char* argv[])
{
//print help information
help();
//check for the input parameter correctness
if(argc != 3) {
cerr <<"Incorret input list" << endl;
cerr <<"exiting..." << endl;
return EXIT_FAILURE;
}
//create GUI windows
namedWindow("Frame");
namedWindow("FG Mask MOG 2");
//create Background Subtractor objects
pMOG2 = createBackgroundSubtractorMOG2(); //MOG2 approach
if(strcmp(argv[1], "-vid") == 0) {
//input data coming from a video
processVideo(argv[2]);
}
else if(strcmp(argv[1], "-img") == 0) {
//input data coming from a sequence of images
processImages(argv[2]);
}
else {
//error in reading input parameters
cerr <<"Please, check the input parameters." << endl;
cerr <<"Exiting..." << endl;
return EXIT_FAILURE;
}
//destroy GUI windows
destroyAllWindows();
return EXIT_SUCCESS;
}
void processVideo(char* videoFilename) {
//create the capture object
VideoCapture capture(videoFilename);
if(!capture.isOpened()){
//error in opening the video input
cerr << "Unable to open video file: " << videoFilename << endl;
exit(EXIT_FAILURE);
}
//read input data. ESC or 'q' for quitting
keyboard = 0;
vector<double> proc_time;
while( keyboard != 'q' && keyboard != 27 ){
auto start_t = std::chrono::steady_clock::now();
auto st_0 = start_t;
//read the current frame
if(!capture.read(frame)) {
cerr << "Unable to read next frame." << endl;
cerr << "Exiting..." << endl;
exit(EXIT_FAILURE);
}
//use blur on image
blur( frame, frame, Size( 3, 3 ), Point(-1,-1));
//update the background model
pMOG2->apply(frame, fgMaskMOG2);
//get the frame number and write it on the current frame
imshow("FG Mask MOG 2", fgMaskMOG2);
//get the input from the keyboard
keyboard = (char)waitKey( 20 );
auto end_t = std::chrono::steady_clock::now();
//proc_time.push_back(std::chrono::duration_cast<std::chrono::milliseconds>(end_t - start_t).count());
cout << "calc-time:" << (end_t - start_t).count() << " ms, " << endl;
}
//delete capture object
capture.release();
}
void processImages(char* fistFrameFilename) {
//read the first file of the sequence
frame = imread(fistFrameFilename);
if(frame.empty()){
//error in opening the first image
cerr << "Unable to open first image frame: " << fistFrameFilename << endl;
exit(EXIT_FAILURE);
}
//current image filename
string fn(fistFrameFilename);
//read input data. ESC or 'q' for quitting
keyboard = 0;
while( keyboard != 'q' && keyboard != 27 ){
//update the background model
pMOG2->apply(frame, fgMaskMOG2);
//get the frame number and write it on the current frame
size_t index = fn.find_last_of("/");
if(index == string::npos) {
index = fn.find_last_of("\\");
}
size_t index2 = fn.find_last_of(".");
string prefix = fn.substr(0,index+1);
string suffix = fn.substr(index2);
string frameNumberString = fn.substr(index+1, index2-index-1);
istringstream iss(frameNumberString);
int frameNumber = 0;
iss >> frameNumber;
rectangle(frame, cv::Point(10, 2), cv::Point(100,20),
cv::Scalar(255,255,255), -1);
putText(frame, frameNumberString.c_str(), cv::Point(15, 15),
FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0));
//show the current frame and the fg masks
imshow("Frame", frame);
imshow("FG Mask MOG 2", fgMaskMOG2);
//get the input from the keyboard
keyboard = (char)waitKey( 30 );
//search for the next image in the sequence
ostringstream oss;
oss << (frameNumber + 1);
string nextFrameNumberString = oss.str();
string nextFrameFilename = prefix + nextFrameNumberString + suffix;
//read the next frame
frame = imread(nextFrameFilename);
if(frame.empty()){
//error in opening the next image in the sequence
cerr << "Unable to open image frame: " << nextFrameFilename << endl;
exit(EXIT_FAILURE);
}
//update the path of the current frame
fn.assign(nextFrameFilename);
}
}
......@@ -13,6 +13,9 @@ using namespace std;
using namespace ball_tracking;
using json = nlohmann::json;
Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
Ptr<BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
int main(int argc, char** argv) {
try {
namespace po = boost::program_options;
......@@ -40,6 +43,8 @@ int main(int argc, char** argv) {
Binarizer bin(jobj.at("binarizer"));
FindBallBlob bf(jobj.at("blob_detection"));
pMOG2 = createBackgroundSubtractorMOG2(); //MOG2 approach
string fname = vm["input"].as<string>();
VideoCapture in_video(fname);
Size in_size((int)in_video.get(CV_CAP_PROP_FRAME_WIDTH),
......@@ -60,6 +65,9 @@ int main(int argc, char** argv) {
cerr << "Input video could not be opened: " << fname << endl;
return -1;
}
// set the video at 5s
//in_video.set(CAP_PROP_POS_AVI_RATIO, 0.1);
if(!out_video.isOpened()) {
cerr << "Output video could not be opened: " << outname << endl;
return -1;
......@@ -68,6 +76,8 @@ int main(int argc, char** argv) {
Mat img, l_lh, tmp;
Mat res(out_size, CV_8UC3);
vector<double> llh_time, blob_time, proc_time, in_time, draw_time;
int x=5;
while (true) {
//1) Read the image
auto start_t = std::chrono::steady_clock::now();
......@@ -77,6 +87,11 @@ int main(int argc, char** argv) {
auto end_t = std::chrono::steady_clock::now();
in_time.push_back(std::chrono::duration_cast<std::chrono::milliseconds>(end_t - start_t).count());
//Bluring input image
blur( img, img, Size( 3, 3 ), Point(-1,-1));
//adding backgroundsubtractors
pMOG2->apply(img, fgMaskMOG2);
//2) Compute log likelihood image
start_t = std::chrono::steady_clock::now();
l_lh = bll(img);
......@@ -92,7 +107,8 @@ int main(int argc, char** argv) {
proc_time.push_back(llh_time.back() + blob_time.back());
cout << "N: " << key_pts.size() << ", FPT: " << proc_time.back() << " ms, ";
if(key_pts.size()>0)
cout << key_pts[0].pt.x;
//4) Compute output image
start_t = std::chrono::steady_clock::now();
drawKeypoints(img, key_pts, res(Rect(0,0,in_size.width,in_size.height)),
......@@ -104,6 +120,10 @@ int main(int argc, char** argv) {
//res.adjustROI(0,0,in_size.width,0);
drawKeypoints(bin_img, key_pts, res(Rect(0,in_size.height,in_size.width,in_size.height)),
Scalar(255,255,0), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
drawKeypoints(fgMaskMOG2, key_pts, res(Rect(in_size.width,in_size.height,in_size.width,in_size.height)),
Scalar(255,255,0), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
end_t = std::chrono::steady_clock::now();
draw_time.push_back(std::chrono::duration_cast<std::chrono::milliseconds>(end_t - start_t).count());
cout << "Draw: " << draw_time.back() << " ms, ";
......@@ -114,10 +134,13 @@ int main(int argc, char** argv) {
//6) Display output
imshow("Output", res);
int k = waitKey(5) & 0xff;
int k = waitKey(x) & 0xff;
end_t = std::chrono::steady_clock::now();
cout << " TT: " << std::chrono::duration_cast<std::chrono::milliseconds>(end_t - st_0).count()
<< " ms" << endl;
if (k==8) x=0;
if (k==9) x=5;
if (k==27) break;
}
return 0;
......
......@@ -16,6 +16,23 @@ namespace ball_tracking {
*/
namespace {
class GaussSmooth {
private:
int size;
double sigma;
public:
GaussSmooth(const json& conf) {
size = conf.at("size");
sigma = conf.at("sigma");
}
cv::Mat operator()(cv::InputArray _src) {
Mat dst;
GaussianBlur(_src, dst, Size(size,size), sigma, sigma);
return dst;
}
};
/**
* Color and background based logistic regression
*/
......@@ -23,18 +40,36 @@ namespace ball_tracking {
private:
Mat bkg; //!< Background image
Mat weights; //!< Model weights
vector<preproc> pre; //!< Preprocessing functions for the images
public:
cv::Mat operator()(cv::InputArray src) {
/*for update the background
void ChangeBack(cv::InputArray _bkg){
bkg = _bkg;
}*/
Mat pre_chain(cv::InputArray src) {
Mat tmp = src.getMat();
for (auto p : pre) {
tmp = p(tmp);
}
return tmp;
}
cv::Mat operator()(cv::InputArray _src) {
Mat src = pre_chain(_src);
if (bkg.empty()) {
bkg = src.getMat();
bkg = src;
}
return quadf_log_reg(src, bkg, weights);
}
CB_log_reg(const json& conf) {
weights = json2cvmat(conf.at("weights"));
if (conf.count("gauss_smooth")) {
pre.push_back(GaussSmooth(conf.at("gauss_smooth")));
}
if (conf.count("background")) {
bkg = imread(conf.at("background"), CV_LOAD_IMAGE_COLOR);
bkg = pre_chain( imread(conf.at("background"), CV_LOAD_IMAGE_COLOR) );
if (!bkg.data) {
throw std::logic_error("File not found for background image");
}
......
tests/images/c9.jpg

90.7 KiB

{
"background": "dataset/c8.jpg",
"model_coefs": [
-0.12809208050991031,
0.45351780461524455,
1.000187126883715,
-0.62768654237217136,
-0.89286177482557705,
-1.5392970491099109,
-0.42325560598722445,
0.99756435541399791,
1.4936352455663291,
0.16492476099963882,
-0.31791743751435003,
-1.2762469309753672,
0.63378027046157748,
1.887639943950973,
1.1520081646410667,
0.39797919710017193,
-1.0480923526527788,
2.2570968776408722,
0.19908623890186553,
-0.17220334142491209,
-1.0301920563928393,
1.1307851807421287,
-0.61981945858382004,
-1.4852665274521921,
-0.053392314939854468,
-2.2881257619423905,
-1.9175169537470884,
-2.0342208094741356
]
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment