diff --git a/Demo.cpp b/Demo.cpp index ae8e9c4e973a302a274e9ef407ca873e397c722d..9631a66284d2becd58d9c9c07ba9ab910d5b231c 100644 --- a/Demo.cpp +++ b/Demo.cpp @@ -63,12 +63,12 @@ int main(int argc, char * argv[]) //!** parse programm input****************/ if(cmdOptionExists(argv, argv+argc, "-h")) { - cout <<" error: please use command as\n./bgs_demo -i pathToInputDir -a amountOfJpgFiles -o outPutPath"<<endl; + cout <<" error: please use command as\n./bgs_demo -i pathToInputDir -a amountOfJpgFiles -c exactCenterConfFile.xml -o outPutPath"<<endl; return EXIT_FAILURE; } if(!cmdOptionExists(argv, argv+argc, "-i")||!cmdOptionExists(argv, argv+argc, "-a")||!cmdOptionExists(argv, argv+argc, "-o") ) { - cout <<" error: please use command as\n./bgs_demo -i pathToInputDir -a amountOfJpgFiles -o outPutPath"<<endl; + cout <<" error: please use command as\n./bgs_demo -i pathToInputDir -a amountOfJpgFiles -c exactCenterConfFile.xml -o outPutPath"<<endl; return EXIT_FAILURE; } @@ -88,6 +88,15 @@ int main(int argc, char * argv[]) foo >> amountFiles; } + + char *centerFile = getCmdOption(argv, argv + argc, "-c"); + string centerFileString("."); + if (centerFile) + { + //test dir exists + centerFileString = string(centerFile); + } + char *testOutputDir = getCmdOption(argv, argv + argc, "-o"); string outputDir("."); if (testOutputDir) @@ -96,13 +105,14 @@ int main(int argc, char * argv[]) outputDir = string(testOutputDir); } - cout <<"args: -i "<<inputDir<<" -a "<<amountFiles <<" -o " << outputDir; + cout <<"args: -i "<<inputDir<<" -a "<<amountFiles << "-c" << centerFileString<<" -o " << outputDir; //!**** end parse input***********/ //./program -i pathToInputDir -a amountOfJpgFiles -o outPutPath //pathToInputDir //----should have + // centerFile.xml // -data/ // #which include all jpg and tt files // first file mus be: 0000000000.jpg @@ -111,21 +121,21 @@ int main(int argc, char * argv[]) // -// VideoCapture capture; + // VideoCapture capture; -// if (argc > 1) -// { -// std::cout << "Openning: " << argv[1] << std::endl; -// capture.open(argv[1]); -// } -// else -// capture.open(0); + // if (argc > 1) + // { + // std::cout << "Openning: " << argv[1] << std::endl; + // capture.open(argv[1]); + // } + // else + // capture.open(0); -// if (!capture.isOpened()) -// { -// std::cerr << "Cannot initialize video!" << std::endl; -// return -1; -// } + // if (!capture.isOpened()) + // { + // std::cerr << "Cannot initialize video!" << std::endl; + // return -1; + // } /* Background Subtraction Methods */ //IBGS *bgs; @@ -199,6 +209,33 @@ int main(int argc, char * argv[]) clock_t beginAll = clock(); + //! we read the center config file for cut out the ROI + //TODO merge this config with the Tapter.xml ?? + //circle param + int circleCenterX = 880; + int circleCenterY = 750; + int circleRadius = 700; + cv::String configFileNameCenter(centerFileString); + //read the config + cout << "parameter of centerConfigFile.xml"<<endl; + FileStorage fsCen; + fsCen.open(configFileNameCenter, FileStorage::READ); + if (!fsCen.isOpened()) + { + cout << "error during open " <<centerFileString << " will abort\n "; + return EXIT_FAILURE; + } + + circleCenterX = (int) fsCen["circleCenterX"]; + cout <<"circleCenterX: "<< circleCenterX<<endl; + + circleCenterY = (int) fsCen["circleCenterY"]; + cout <<"circleCenterY: "<< circleCenterY<<endl; + + circleRadius = (int) fsCen["circleRadius"]; + cout <<"circleRadius: "<< circleRadius<<endl; + fsCen.release(); + //first the static pic********** //std::string fileName = getFileName(begin); std::string staticFile = inputDir+"/bk.jpg"; @@ -206,6 +243,9 @@ int main(int argc, char * argv[]) img_input = imread(staticFile.c_str(), CV_LOAD_IMAGE_COLOR); if(img_input.data ) { + //we cut out a smaller ROI + img_input = TToolBox::cropImageCircle(img_input,circleCenterX,circleCenterY,circleRadius); + cv::Mat img_mask; cv::Mat img_bkgmodel; bgs->process(img_input, img_mask, img_bkgmodel); @@ -248,8 +288,12 @@ int main(int argc, char * argv[]) cout <<"load rnd file :"<< fileName<<endl; img_input = imread(fileName.c_str(), CV_LOAD_IMAGE_COLOR); + if(img_input.data ) { + //we cut out a smaller ROI + img_input = TToolBox::cropImageCircle(img_input,circleCenterX,circleCenterY,circleRadius); + //cv::imshow("input", img_input); cv::Mat img_mask; cv::Mat img_bkgmodel; @@ -275,12 +319,15 @@ int main(int argc, char * argv[]) clock_t begin = clock(); fileName = inputDir + TToolBox::getFileName(i); - cout <<"load file :"<< fileName<<endl; + cout <<"\t"<<i<<"of \t"<<begin+steps<<"load file :"<< fileName<<endl; //cv::imwrite(convert.str().c_str(), img_output); img_input = imread(fileName.c_str(), CV_LOAD_IMAGE_COLOR); if(img_input.data ) { + //we cut out a smaller ROI + img_input = TToolBox::cropImageCircle(img_input,circleCenterX,circleCenterY,circleRadius); + //cv::imshow("input", img_input); cv::Mat img_mask; @@ -294,15 +341,19 @@ int main(int argc, char * argv[]) } clock_t end = clock(); - double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC; + double elapsedSecs = double(end - begin) / CLOCKS_PER_SEC; + + + clock_t endAll = clock(); + double elapsedSecTotal = double(endAll - beginAll) / CLOCKS_PER_SEC; + cout <<"process single pic:\t"<<elapsedSecs<<"s"<<amountFiles<<" total:\t"<<(int)(elapsedSecTotal/60)<<" min"<<endl; - cout <<"process took:\t"<<elapsed_secs<<"s"<<endl; } - clock_t endAll = clock(); - double elapsed_secs = double(endAll - beginAll) / CLOCKS_PER_SEC; - cout <<"process"<<amountFiles<<" took:\t"<<(int)(elapsed_secs/60)<<" min"<<endl; - //we write the random file list + // clock_t endAll = clock(); + // double elapsed_secs = double(endAll - beginAll) / CLOCKS_PER_SEC; + // cout <<"process"<<amountFiles<<" took:\t"<<(int)(elapsed_secs/60)<<" min"<<endl; + // //we write the random file list //format output file std::string nameOutRandomFile = outputDir + "randlist.yml"; @@ -312,7 +363,7 @@ int main(int argc, char * argv[]) delete bgs; -// capture.release(); + // capture.release(); cvDestroyAllWindows(); return 0; diff --git a/package_bgs/Tapter.cpp b/package_bgs/Tapter.cpp index 5e86569ab9500313c5a5db1060337a60b89b544a..e55c5e52a3778117b2ca23f0e85b3d93c49e3335 100644 --- a/package_bgs/Tapter.cpp +++ b/package_bgs/Tapter.cpp @@ -25,6 +25,7 @@ Tapter::Tapter() : setup("./config/Tapter.xml"); frameCounter = 0; //inputPath = "."; + //centerFile = "."; outputPath = "."; flagWrite = -1; writeDbgPic = -1; @@ -65,10 +66,15 @@ void Tapter::setInitialFrameCounter(int counter) void Tapter::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &img_bgmodel) { + + //! continue the normal init(img_input, img_output, img_bgmodel); IplImage *frame = new IplImage(img_input); + + + if (firstTime) { int w = cvGetSize(frame).width; diff --git a/package_bgs/Tapter.h b/package_bgs/Tapter.h index 840f7fa2669c3613cd12814fac29c3689c308a3e..ce7f6d836dbb9a86db2b4a09a9e346be27804a0d 100644 --- a/package_bgs/Tapter.h +++ b/package_bgs/Tapter.h @@ -52,6 +52,7 @@ namespace bgslibrary int frameCounter; // std::string inputPath; +// std::string centerFile; short flagWrite; short writeDbgPic; std::string outputPath; diff --git a/package_bgs/ttoolbox.cpp b/package_bgs/ttoolbox.cpp index 1246c9d8856ee05d95582fdc253403598fbed112..d6bfd1e56c77ae551cc1e870ea0d118ec1b58e38 100644 --- a/package_bgs/ttoolbox.cpp +++ b/package_bgs/ttoolbox.cpp @@ -13,3 +13,33 @@ std::string TToolBox::getFileName(int i) std::string fileName = "/data/"+ mNzero(i) + ".jpg"; return fileName; } + + +cv::Mat TToolBox::cropImageCircle(cv::Mat image, int x, int y, int r) +{ + + // Your Hough circle + cv::Vec3f circ(x,y,r); + + // Draw the mask: white circle on black background + cv::Mat1b mask(image.size(), uchar(0)); + cv::circle(mask, cv::Point(circ[0], circ[1]), circ[2], cv::Scalar(255), CV_FILLED); + + //NO CROP NEEDED + + // Compute the bounding box + //Rect bbox(circ[0] - circ[2], circ[1] - circ[2], 2 * circ[2], 2 * circ[2]); + + // Create a black image + cv::Mat res = cv::Mat::zeros( image.size(), CV_8UC3 ); + res = cv::Scalar(0,0,0); //fill with black color + + + // Copy only the image under the white circle to black image + image.copyTo(res, mask); + + // Crop according to the roi + //res = res(bbox); + + return res; +} diff --git a/package_bgs/ttoolbox.h b/package_bgs/ttoolbox.h index 47d225238232403e6240d50d1da0003bc69e3253..cc0a80c9ad8a07e0a5867eba3812947963430805 100644 --- a/package_bgs/ttoolbox.h +++ b/package_bgs/ttoolbox.h @@ -1,6 +1,7 @@ #ifndef TTOOLBOX_H #define TTOOLBOX_H +//cpp,c #include <iostream> #include <algorithm> #include <cstdlib> @@ -10,6 +11,12 @@ #include <ctime> #include <sstream> +//open cv +#include "opencv2/core/core.hpp" +#include "opencv2/opencv.hpp" +#include "opencv2/imgproc/imgproc.hpp" +#include "opencv2/highgui/highgui.hpp" + class TToolBox { public: @@ -20,6 +27,12 @@ public: //! return string plus data path and jpg suffix static std::string getFileName(int i); + //! returns a image, which is resulting circle + //! return roi of image which is cloned + //! without resize the image + //! will fill rest outer bound black + static cv::Mat cropImageCircle(cv::Mat image, int x, int y, int r); + }; #endif // TTOOLBOX_H