From b35d87f91a35396e0003e2f50d630144d2459a56 Mon Sep 17 00:00:00 2001 From: am0ebe <am0ebe@gmx.de> Date: Mon, 17 Jan 2022 15:40:18 +0100 Subject: [PATCH] . --- examples/Demo.cpp | 195 ++++++++++++++++++------------------ opencv | 36 +++++++ src/algorithms/ttoolbox.cpp | 6 +- 3 files changed, 139 insertions(+), 98 deletions(-) create mode 100644 opencv diff --git a/examples/Demo.cpp b/examples/Demo.cpp index 1b0cbc3..b9b89b5 100644 --- a/examples/Demo.cpp +++ b/examples/Demo.cpp @@ -36,21 +36,21 @@ int g_badSignalFlagAbort = 0; void my_handler(int signum); -char* getCmdOption(char ** begin, char ** end, const std::string & option); +char* getCmdOption(char ** begin, char ** end, const string & option); -bool cmdOptionExists(char** begin, char** end, const std::string& option); +bool cmdOptionExists(char** begin, char** end, const string& option); -std::vector<char> copyDataInBuffer(struct archive *aw); +vector<char> copyDataInBuffer(struct archive *aw); //! we convert our actual jpg file number to framenumber -int toFrameNumber(std::string filename); +int toFrameNumber(string filename); int main(int argc, char * argv[]) { signal(SIGUSR1, my_handler); - std::cout << "using processcenter " << PROCESS_CENTER_VERSION_MAJOR <<"."<< PROCESS_CENTER_VERSION_MINOR<<"."<<PROCESS_CENTER_VERSION_MINOR_FIXES<< endl; - std::cout << "Using OpenCV " << CV_MAJOR_VERSION << "." << CV_MINOR_VERSION << "." << CV_SUBMINOR_VERSION << std::endl; + cout << "Using OpenCV " << CV_MAJOR_VERSION << "." << CV_MINOR_VERSION << "." << CV_SUBMINOR_VERSION << endl; + cout << "Using processcenter " << PROCESS_CENTER_VERSION_MAJOR << "." << PROCESS_CENTER_VERSION_MINOR<< "." <<PROCESS_CENTER_VERSION_MINOR_FIXES<< endl << endl; //!** parse programm input****************/ @@ -61,8 +61,8 @@ int main(int argc, char * argv[]) || !cmdOptionExists(argv, argv+argc, "-o") || !cmdOptionExists(argv, argv+argc, "-p")) { - cout <<" Usage:" << endl; - cout << " " << argv[0] << " -i in.tar -a amount -c center.xml -o outPath -p camparameterFile.xml"<< endl << endl; + cout << " Usage:" << endl; + cout << " " << argv[0] << " -i in.tar -a amount -c center.xml -o outPath -p camparameterFile.xml" << endl << endl; cout << " Options:" << endl; cout << " i - input tarfile/dir containing tars ?" << endl; cout << " a - amount of files/frames ?" << endl; @@ -74,7 +74,7 @@ int main(int argc, char * argv[]) } char *testFile = getCmdOption(argv, argv + argc, "-i"); - std::string inputFile("."); + string inputFile("."); if (testFile) { //test dir exists @@ -92,7 +92,7 @@ int main(int argc, char * argv[]) char *centerFile = getCmdOption(argv, argv + argc, "-c"); - std::string centerFileString("."); + string centerFileString("."); if (centerFile) { //test dir exists @@ -100,7 +100,7 @@ int main(int argc, char * argv[]) } char *testOutputDir = getCmdOption(argv, argv + argc, "-o"); - std::string outputDir("."); + string outputDir("."); if (testOutputDir) { //test dir exists @@ -108,7 +108,7 @@ int main(int argc, char * argv[]) } char *camerFile = getCmdOption(argv, argv + argc, "-p"); - std::string cameraParameterFile("."); + string cameraParameterFile("."); if (camerFile) { //test dir exists @@ -116,7 +116,7 @@ int main(int argc, char * argv[]) } - cout <<"args: -i "<<inputFile<<" -a "<<amountFiles << " -c" << centerFileString<<" -o " << outputDir <<" -p " << cameraParameterFile << endl; + cout << "args: -i " <<inputFile<< " -a " <<amountFiles << " -c" << centerFileString<< " -o " << outputDir << " -p " << cameraParameterFile << endl; //!**** end parse input***********/ //libarchive things @@ -141,10 +141,12 @@ int main(int argc, char * argv[]) //and open the handler if ((r = archive_read_open_filename(archive, filename, 10240))) { - cerr<<"archive_read_open_filename: error: "<< archive_error_string(archive) <<" will abort"<< endl; + cerr<< "archive_read_open_filename: error: " << archive_error_string(archive) << " will abort" << endl; exit(1); } + + /* #### Background Subtraction Methods ##### Codebook is new! @@ -180,25 +182,25 @@ int main(int argc, char * argv[]) int circleCenterX = 880; int circleCenterY = 750; int circleRadius = 700; - cv::String configFileNameCenter(centerFileString); + cv::String configFileNameCenter(centerFileString); //read the config - cout << "parameter of centerConfigFile.xml" << endl; + cout << "parameter of centerConfigFile.xml" << endl; FileStorage fsCen; fsCen.open(configFileNameCenter, FileStorage::READ); if (!fsCen.isOpened()) { - cout << "error during open " << centerFileString << " will abort" << endl; + cout << "error during open " << centerFileString << " will abort" << endl; return EXIT_FAILURE; } circleCenterX = (int) fsCen["circleCenterX"]; - cout <<"circleCenterX: "<< circleCenterX << endl; + cout << "circleCenterX: " << circleCenterX << endl; circleCenterY = (int) fsCen["circleCenterY"]; - cout <<"circleCenterY: "<< circleCenterY << endl; + cout << "circleCenterY: " << circleCenterY << endl; circleRadius = (int) fsCen["circleRadius"]; - cout <<"circleRadius: "<< circleRadius << endl; + cout << "circleRadius: " << circleRadius << endl; fsCen.release(); @@ -206,33 +208,33 @@ int main(int argc, char * argv[]) cv::String camParam (cameraParameterFile); Mat intrinsicsCameraMatrix, distortionCoeff; { - cout << "read cam parameter of: "<< cameraParameterFile << endl; + cout << "read cam parameter of: " << cameraParameterFile << endl; FileStorage fs; fs.open(camParam, FileStorage::READ); if (!fs.isOpened()) { - cerr << "error during open " << cameraParameterFile << " will abort" << endl; + cerr << "error during open " << cameraParameterFile << " will abort" << endl; return -1; } - // fs["camera_matrix"] >> intrinsicsCameraMatrix; - // fs["distortion_coefficients"] >> distortionCoeff; + fs["camera_matrix"] >> intrinsicsCameraMatrix; + fs["distortion_coefficients"] >> distortionCoeff; - intrinsicsCameraMatrix = (int) fs["camera_matrix"]; + // intrinsicsCameraMatrix = (int) fs["camera_matrix"]; // intrinsicsCameraMatrix = fs["camera_matrix"].mat(); - cout << "camera_matrix = "<< intrinsicsCameraMatrix << endl; + cout << "camera_matrix = " << intrinsicsCameraMatrix << endl; - distortionCoeff = (int) fs["distortion_coefficients"]; + // distortionCoeff = (int) fs["distortion_coefficients"]; // distortionCoeff = fs["distortion_coefficients"].mat(); cout << "distortion_coefficients = " << distortionCoeff << endl; } cout.flush(); - cout <<"a) we process all frames "<<endl; + cout << "a) we process all frames " << endl; int everyPic= 60*5; - //std::string fileName; + //string fileName; int frameCounter=0; int myFileTarWriterHeaderCounter = 0; int frameCounterOld = -1; @@ -240,13 +242,15 @@ int main(int argc, char * argv[]) vector<string> myFileListNoContour; //list for no contours found; vector<string> myFileListAfterContourSelection; //list for no contours found after selection; + cout << "here" << endl; + system("pause"); //loop over all picture of the tar archive, independing how many this are for (;;) { //measure time consumption clock_t begin = clock(); //for every single file - std::vector<char> vec; + vector<char> vec; //we read the next header r = archive_read_next_header(archive, &entry); @@ -254,20 +258,21 @@ int main(int argc, char * argv[]) break; if (r != ARCHIVE_OK) { - cerr << "archive_read_next_header: error: "<< archive_error_string(archive) <<" will abort"<< endl; + cerr << "archive_read_next_header: error: " << archive_error_string(archive) << " will abort" << endl; exit(1); } + cout << "here2" << endl; // get filename const char *fileNamePtr = archive_entry_pathname(entry); - std::string filename(fileNamePtr,strlen(fileNamePtr) ); - //cout << "fileName: "<< filename <<endl; + string filename(fileNamePtr,strlen(fileNamePtr) ); + //cout << "fileName: " << filename <<endl; // convert it to a framenumber frameCounter = toFrameNumber(filename); if(frameCounterOld>frameCounter) { - cerr <<" error during read in the file number, we do have non montonic order, will abort"; + cerr << " error during read in the file number, we do have non montonic order, will abort"; exit(1); } @@ -276,7 +281,7 @@ int main(int argc, char * argv[]) r = archive_write_header(ext, entry); if (r != ARCHIVE_OK) { - cerr<<"archive_write_header() error: "<< archive_error_string(ext)<<endl; + cerr<< "archive_write_header() error: " << archive_error_string(ext)<<endl; myFileTarWriterHeaderCounter++; continue; //we overjump all in our for loop } @@ -288,7 +293,7 @@ int main(int argc, char * argv[]) r = archive_write_finish_entry(ext); if (r != ARCHIVE_OK) { - cerr<<"archive_write_finish_entry: error: "<< archive_error_string(ext) <<" will abort"<< endl; + cerr<< "archive_write_finish_entry: error: " << archive_error_string(ext) << " will abort" << endl; exit(1); } @@ -296,10 +301,10 @@ int main(int argc, char * argv[]) Mat img_input = imdecode(Mat(vec), 1); // //we define the type better to prevent error - // fileName = std::string(inputFile + TToolBox::getFileName(frameCounter)); + // fileName = string(inputFile + TToolBox::getFileName(frameCounter)); // cv::String fileNameCV(fileName); - cout <<"\t"<<frameCounter<<"\tof \t"<<amountFiles<<" load file :"<< filename<<endl; + cout << "\t" << frameCounter << "\tof \t" << amountFiles << " load file :" << filename << endl; //of data is present if(img_input.data ) @@ -329,10 +334,10 @@ int main(int argc, char * argv[]) double threshholdMin = 150; double threshholdMax = 200; int apertureSize = 3; - std::vector<vector<Point> > contours = TToolBox::applyCannyEdgeAndCalcCountours(img_mask,threshholdMin,threshholdMax,apertureSize); + vector<vector<Point> > contours = TToolBox::applyCannyEdgeAndCalcCountours(img_mask,threshholdMin,threshholdMax,apertureSize); - //define what we will write down - std::vector<vector<Point> > contourSelection; + //define what will be written + vector<vector<Point> > contourSelection; vector<Point2f> massCenters; vector<Point> conHull; Point2f muConvexHullMassCenter(0.0,0.0); @@ -340,10 +345,10 @@ int main(int argc, char * argv[]) if(!contours.empty()) { - //! step 4) we make a selection out of all counters with area sizes****************************** + //! step 4) we make a selection out of all contours with area sizes****************************** - //we calc all min rotated rectangles for all contour from candy egde detect - //we exlcude very small one and very big ones + //calc all min rotated rectangles for all contour from canny edge detect + //exclude very small/big vector<RotatedRect> minRect( contours.size() ); //calc boxes around the contours @@ -371,13 +376,13 @@ int main(int argc, char * argv[]) //over stepp all small areas if(area0<areaMinThreshold||area0>areaMaxThreshold) { - //cout<<i<<":area0:"<<area0<<" dismissed "<<endl; + //cout<<i<< ":area0:" <<area0<< " dismissed " <<endl; //skipe if the area is too small continue; } else { - //cout<<i<<":area0:"<<area0<<" choose "<<endl; + //cout<<i<< ":area0:" <<area0<< " choose " <<endl; //get the center of this rectangle Point2f center = minRect[i].center; @@ -438,13 +443,13 @@ int main(int argc, char * argv[]) } else //end after selection is empty { - cerr<<"error, no contour found after selection in file: "<< filename<<endl; + cerr<< "error, no contour found after selection in file: " << filename<<endl; myFileListAfterContourSelection.push_back(filename); } }//end if contours are empty, to canny edge found nothing else { - cerr<<"error, no contour found at all in file: "<< filename<<endl; + cerr<< "error, no contour found at all in file: " << filename<<endl; myFileListNoContour.push_back(filename); } @@ -463,29 +468,29 @@ int main(int argc, char * argv[]) //we draw it cv::String outpath2= outputDir; - std::ostringstream convert2; - convert2 << outpath2 <<TToolBox::mNzero(frameCounter) <<"_convex_hull.jpg"; + ostringstream convert2; + convert2 << outpath2 << TToolBox::mNzero(frameCounter) << "_convex_hull.jpg"; cv::imwrite(convert2.str().c_str(), imgConvexHull); } else - cout<<"convex hull has no points will skip file: "<<i<<endl; + cout<< "convex hull has no points will skip file: " << i<< endl; #endif - //cout <<" found center at : "<< massCenters.at(0).x<< ";"<<massCenters.at(0).y<<endl; + //cout << " found center at : " << massCenters.at(0).x<< ";" << massCenters.at(0).y<< endl; //! step 8: we write down all our results in yml file - std::string nameOutPutFileData = outputDir + TToolBox::mNzero(frameCounter) + ".yml"; + string nameOutPutFileData = outputDir + TToolBox::mNzero(frameCounter) + ".yml"; - //cout <<"output file:" << nameOutPutFileData; + //cout << "output file:" << nameOutPutFileData; FileStorage fs(nameOutPutFileData.c_str(), FileStorage::WRITE); - fs << "masscenters" << massCenters; - fs << "polygonselection"<< contourSelection; - fs << "convexhull"<<conHull; - fs << "masscenterconvexhull"<<muConvexHullMassCenter; + fs << "masscenters" << massCenters; + fs << "polygonselection" << contourSelection; + fs << "convexhull" << conHull; + fs << "masscenterconvexhull" << muConvexHullMassCenter; fs.release(); - //! we write from time to time a dbg picture + //! occasionaly write a dbg picture if(frameCounter%everyPic==0) { Scalar colorRed( 0,0,255,255 );//red @@ -504,7 +509,7 @@ int main(int argc, char * argv[]) circle( imgDebugPaint2, massCenters[i], 4, color, -1, 8, 0 ); } - //we write the convex hull + // write the convex hull if(!conHull.empty()) { //the poly @@ -513,26 +518,26 @@ int main(int argc, char * argv[]) circle( imgDebugPaint2,muConvexHullMassCenter, 4, colorRed, -1, 8, 0 ); } - //we make a copy + // copy Mat imgOverlay2 = img_input.clone(); - //we add a overlay of our paitings + // add overlay of paintings addWeighted( imgDebugPaint2, 0.7, imgOverlay2, 0.3, 0.0, imgOverlay2); - //we write the file down - std::string nameOutPutFileDBGpic = outputDir + TToolBox::mNzero(frameCounter) + std::string(".jpg"); + // writeout file + string nameOutPutFileDBGpic = outputDir + TToolBox::mNzero(frameCounter) + string(".jpg"); imwrite(nameOutPutFileDBGpic.c_str(),imgOverlay2); - }//end if we write a dbg picture + }//end if dbg picture }//end if the loaded picture has data else { - cerr<<"error loading file: "<< filename<<", will skip this file"<<endl; + cerr<< "error loading file: " << filename<< ", will skip this file" << endl; myFileErrorList.push_back(filename); } - //calc the time which we used for a picture + //calc the time which we used for a picture //TODO move out of loop clock_t end = clock(); double elapsedSecs = double(end - begin) / CLOCKS_PER_SEC; @@ -540,7 +545,7 @@ int main(int argc, char * argv[]) //calc the time which was used for all pictures clock_t endAll = clock(); double elapsedSecTotal = double(endAll - beginAll) / CLOCKS_PER_SEC; - cout <<"process single pic:\t"<<elapsedSecs<<" s - \t\t"<<(int)(elapsedSecTotal/60)<<" min -\t"<<(int)(elapsedSecTotal/60/60)<<" h"<<endl; + cout << "process single pic:\t" << elapsedSecs<< " s - \t\t" << (int)(elapsedSecTotal/60)<< " min -\t" << (int)(elapsedSecTotal/60/60)<< " h" << endl; if(g_badSignalFlagAbort) frameCounter = amountFiles; //we abort @@ -552,38 +557,38 @@ int main(int argc, char * argv[]) //finishing time clock_t endAll = clock(); double elapsed = double(endAll - beginAll) / CLOCKS_PER_SEC; - cout <<"process : "<<amountFiles<<" files took:\t"<<(int)(elapsed/60)<<" min -\t"<<(int)(elapsed/60/60)<<" h \n in total"<<endl; + cout << "process : " << amountFiles<< " files took:\t" << (int)(elapsed/60)<< " min -\t" << (int)(elapsed/60/60)<< " h \n in total" << endl; // //we write the random file list to a file - // std::string nameOutRandomFile = outputDir + "randlist.yml"; + // string nameOutRandomFile = outputDir + "randlist.yml"; // FileStorage fs(nameOutRandomFile.c_str(), FileStorage::WRITE); - // fs << "randomlist" << myRandomTrainList; + // fs << "randomlist" << myRandomTrainList; // fs.release(); //TODO we should merge the file - cout<< "amount of libarchive read header errors :"<< myFileTarWriterHeaderCounter; + cout<< "amount of libarchive read header errors :" << myFileTarWriterHeaderCounter; - //we write the random file list to a file - std::string nameOutErrorList = outputDir + "fileErrorList.yml"; - cout <<"amount of file errors: "<< myFileErrorList.size()<<endl; + // write the random file list to a file + string nameOutErrorList = outputDir + "fileErrorList.yml"; + cout << "amount of file errors: " << myFileErrorList.size()<< endl; FileStorage fs2(nameOutErrorList.c_str(), FileStorage::WRITE); - fs2 << "fileErrorIOs" << myFileErrorList; + fs2 << "fileErrorIOs" << myFileErrorList; - //we write error list no contours found in file - cout <<"amount contour errors with canny edge: "<< myFileListNoContour.size()<<endl; - fs2 << "fileErrorNoContours" << myFileListNoContour; + // write error list no contours found in file + cout << "amount contour errors with canny edge: " << myFileListNoContour.size() << endl; + fs2 << "fileErrorNoContours" << myFileListNoContour; - //we write error list no contours found in file - cout <<"amount contour errors after selection: "<< myFileListAfterContourSelection.size()<<endl; - fs2 << "fileErrorNoContoursAfterSelections" << myFileListAfterContourSelection; + // write error list no contours found in file + cout << "amount contour errors after selection: " << myFileListAfterContourSelection.size() << endl; + fs2 << "fileErrorNoContoursAfterSelections" << myFileListAfterContourSelection; - //the bgs related things + // bgs related things delete bgs; - //opencv related things + // opencv related things fs2.release(); - // capture.release(); + cvDestroyAllWindows(); //close lib archive related things @@ -598,11 +603,11 @@ int main(int argc, char * argv[]) -std::vector<char> copyDataInBuffer(struct archive *aw) +vector<char> copyDataInBuffer(struct archive *aw) { int r; const void *buff; - std::vector<char> vec; + vector<char> vec; size_t size; #if ARCHIVE_VERSION_NUMBER >= 3000000 int64_t offset; @@ -641,13 +646,13 @@ void my_handler(int signum) if (signum == SIGUSR1) { g_badSignalFlagAbort = 1; - cerr << "receive signal to abort"<<endl; + cerr << "receive signal to abort" << endl; } } -char* getCmdOption(char ** begin, char ** end, const std::string & option) +char* getCmdOption(char ** begin, char ** end, const string & option) { - char ** itr = std::find(begin, end, option); + char ** itr = find(begin, end, option); if (itr != end && ++itr != end) { return *itr; @@ -655,12 +660,12 @@ char* getCmdOption(char ** begin, char ** end, const std::string & option) return 0; } -bool cmdOptionExists(char** begin, char** end, const std::string& option) +bool cmdOptionExists(char** begin, char** end, const string& option) { - return std::find(begin, end, option) != end; + return find(begin, end, option) != end; } -int toFrameNumber(std::string filename) +int toFrameNumber(string filename) { int retVal = -1; @@ -670,9 +675,9 @@ int toFrameNumber(std::string filename) filename = regex_replace(filename, regex("jpg"), ""); filename = regex_replace(filename, regex("\\."), ""); - retVal = std::stoi( filename ); + retVal = stoi( filename ); - //cout <<"filename out:" << filename << " number: "<< retVal << endl; + //cout << "filename out:" << filename << " number: " << retVal << endl; return retVal; } diff --git a/opencv b/opencv new file mode 100644 index 0000000..24f4ea4 --- /dev/null +++ b/opencv @@ -0,0 +1,36 @@ +point2f(y,x) # inverted! + +Types: + #depth + CV_8U unsigned char + CV_8S char + CV_16U unsigned short + CV_16S short + CV_32S int + CV_32F float + CV_64F double + # + channels C1..4 eg RGBA: + +Camera Calibration: + distortion_coefficients - 5 values + camera_matrix - 3x3 matrix - eg focal length etc. + + might need to calibrate using chess board, to get values + sa + https://github.com/abidrahmank/OpenCV2-Python-Tutorials/blob/master/source/py_tutorials/py_calib3d/py_calibration/py_calibration.rst + + + +roi - region of interest - x, y, w, h + +BS: +https://docs.opencv.org/4.x/d1/dc5/tutorial_background_subtraction.html +https://web.archive.org/web/20140418093037/http://bmc.univ-bpclermont.fr/ +https://web.archive.org/web/20140221195750/http://docs.opencv.org/trunk/doc/tutorials/video/background_subtraction/background_subtraction.html + +https://www.pyimagesearch.com/2020/07/27/opencv-grabcut-foreground-segmentation-and-extraction/ +https://learnopencv.com/applications-of-foreground-background-separation-with-semantic-segmentation/ +https://docs.opencv.org/4.x/d2/d55/group__bgsegm.html + +tuts:)) +https://docs.opencv.org/4.x/d6/d00/tutorial_py_root.html diff --git a/src/algorithms/ttoolbox.cpp b/src/algorithms/ttoolbox.cpp index 2fc4ab5..a23ab1d 100644 --- a/src/algorithms/ttoolbox.cpp +++ b/src/algorithms/ttoolbox.cpp @@ -47,7 +47,7 @@ std::vector<std::vector<cv::Point>> TToolBox::applyCannyEdgeAndCalcCountours(cv: #ifdef MC_SHOW_STEP_ANALYSE // Draw contours on extra mat cv::Mat imgContour = cv::Mat::zeros( imgCannyEdge.size(), CV_8UC3 ); - imgContour = cv::Scalar(255,255,255); //fille the picture + imgContour = cv::Scalar(255,255,255); //fill the picture cv::RNG rng(232323); for( size_t i = 0; i< contours.size(); i++ ) { @@ -57,8 +57,8 @@ std::vector<std::vector<cv::Point>> TToolBox::applyCannyEdgeAndCalcCountours(cv: cv::drawContours( imgContour, contours, i, color, 1, cv::LINE_AA, hierarchy, 0, cv::Point() ); } - - cv::String outpath= "/homes/tb55xemi/work/dev/bgslibrary-bgslib_qtgui_2.0.0/build/test0815/results"; + // bgslibrary-bgslib_qtgui_2.0.0 + cv::String outpath = "./"; std::ostringstream convert; convert << outpath << "test_countour_canny_edge.jpg"; cv::imwrite(convert.str().c_str(), imgContour); -- GitLab