diff --git a/Demo.cpp b/Demo.cpp index 20829a73bb1c8b46e3ae663e1f048219f4488a79..e6a63a90fc487436a57342e1acd3f600326a783d 100644 --- a/Demo.cpp +++ b/Demo.cpp @@ -15,7 +15,7 @@ based on original demo.cpp #define PROCESS_CENTER_VERSION_MAJOR 0 #define PROCESS_CENTER_VERSION_MINOR 9 -#define PROCESS_CENTER_VERSION_MINOR_FIXES 1 +#define PROCESS_CENTER_VERSION_MINOR_FIXES 2 //opencv #include <opencv2/opencv.hpp> @@ -308,7 +308,9 @@ int main(int argc, char * argv[]) int everyPic=60*5; int frameCounter=0; - vector<string> myFileErrorList; + vector<string> myFileErrorList;//list for pure io error + vector<string> myFileListNoContour; //list for no contours found; + vector<string> myFileListAfterContourSelection; //list for no contours found after selection; for(frameCounter=0;frameCounter<amountFiles;frameCounter++) { @@ -321,7 +323,7 @@ int main(int argc, char * argv[]) //check the string cout <<"open fileName: "<< fileName<<endl; if(fileName.empty()) - cerr <<"error: empty string fileName"<<endl; + cerr <<"error: empty string fileName"<<endl; cout <<"\t"<<frameCounter<<"\tof \t"<<amountFiles<<" load file :"<< fileName<<endl; @@ -351,140 +353,158 @@ int main(int argc, char * argv[]) int apertureSize = 3; std::vector<vector<Point> > contours = TToolBox::applyCannyEdgeAndCalcCountours(img_mask,threshholdMin,threshholdMax,apertureSize); + //define what we will write down + std::vector<vector<Point> > contourSelection; + vector<Point2f> massCenters; + vector<Point> conHull; + Point2f muConvexHullMassCenter(0.0,0.0); - // //TODO: we need to order the points to get a nice polygon, otherwise not usefull - // //we also try to find the aproximate polygon***************** - //// vector<Point> aproxiCurve; + if(!contours.empty()) + { + // //TODO: we need to order the points to get a nice polygon, otherwise not usefull + // //we also try to find the aproximate polygon***************** + //// vector<Point> aproxiCurve; - //// // //see https://docs.opencv.org/3.4/d3/d63/classcv_1_1Mat.html#a167a8e0a3a3d86e84b70e33483af4466 - //// // if(aproxiCurve::checkVector(10,CV_32F)==-1) - //// // cout<<"error wrong format of vector"<<endl; + //// // //see https://docs.opencv.org/3.4/d3/d63/classcv_1_1Mat.html#a167a8e0a3a3d86e84b70e33483af4466 + //// // if(aproxiCurve::checkVector(10,CV_32F)==-1) + //// // cout<<"error wrong format of vector"<<endl; - //// //calc 0.1 percent of arc length of convex hull - //// double epsilon = 0.1 * cv::arcLength(hullComplete,true); + //// //calc 0.1 percent of arc length of convex hull + //// double epsilon = 0.1 * cv::arcLength(hullComplete,true); - //// //see https://docs.opencv.org/2.4.13.2/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#approxpolydp - //// cv::approxPolyDP(allContourPoints,aproxiCurve,epsilon,false); + //// //see https://docs.opencv.org/2.4.13.2/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#approxpolydp + //// cv::approxPolyDP(allContourPoints,aproxiCurve,epsilon,false); - ////#ifdef MC_SHOW_STEP_ANALYSE - //// if(aproxiCurve.size()>=2)//we only draw if we have at least a line - //// { - //// Mat imgPolyApr = Mat::zeros( img_input.size(), CV_8UC3 ); - //// imgPolyApr = Scalar(255,255,255); //fille the picture - //// Scalar colorC( 0,0,255,255 );//red + ////#ifdef MC_SHOW_STEP_ANALYSE + //// if(aproxiCurve.size()>=2)//we only draw if we have at least a line + //// { + //// Mat imgPolyApr = Mat::zeros( img_input.size(), CV_8UC3 ); + //// imgPolyApr = Scalar(255,255,255); //fille the picture + //// Scalar colorC( 0,0,255,255 );//red - //// //for(imgPolyApr) + //// //for(imgPolyApr) - //// polylines(imgPolyApr, aproxiCurve, true, colorC, 1, 8); + //// polylines(imgPolyApr, aproxiCurve, true, colorC, 1, 8); - //// cv::String outpath3= "/homes/tb55xemi/work/bugTrainingSet/testRec/rec04379437pp/result/"; - //// std::ostringstream convert3; - //// convert3 << outpath3 << frameCounter <<"_appr_poly.jpg"; - //// cv::imwrite(convert3.str().c_str(), imgPolyApr); - //// } - //// else - //// cout<<"approximate poly has not enough points will skip file: "<<frameCounter<<endl; + //// cv::String outpath3= "/homes/tb55xemi/work/bugTrainingSet/testRec/rec04379437pp/result/"; + //// std::ostringstream convert3; + //// convert3 << outpath3 << frameCounter <<"_appr_poly.jpg"; + //// cv::imwrite(convert3.str().c_str(), imgPolyApr); + //// } + //// else + //// cout<<"approximate poly has not enough points will skip file: "<<frameCounter<<endl; - ////#endif + ////#endif - //! step 4) we make a selection out of all counters with area sizes****************************** - vector<vector<Point> > contourSelection; - //we exlcude very small one and very big ones + //! step 4) we make a selection out of all counters with area sizes****************************** - //we calc all min rotated rectangles for all contour from candy egde detect - vector<RotatedRect> minRect( contours.size() ); + //we calc all min rotated rectangles for all contour from candy egde detect + //we exlcude very small one and very big ones + vector<RotatedRect> minRect( contours.size() ); - //calc boxes around the contours - for( size_t i = 0; i < contours.size(); i++ ) - minRect[i] = minAreaRect( Mat(contours[i]) ); //may use boundingRect ?? to use fix non rotated rectangles ? + //calc boxes around the contours + for( size_t i = 0; i < contours.size(); i++ ) + minRect[i] = minAreaRect( Mat(contours[i]) ); //may use boundingRect ?? to use fix non rotated rectangles ? - vector<Point2f> recCenterPoints; - float areaMinThreshold = 150; - float areaMaxThreshold = 15000; //TODO apply moving filter ??, an more adaptive approach + vector<Point2f> recCenterPoints; + float areaMinThreshold = 150; + float areaMaxThreshold = 15000; //TODO apply moving filter ??, an more adaptive approach - //iterate all rectangles - for( size_t i = 0; i< minRect.size(); i++ ) - { - Point2f rect_points[4]; - //get all points of the retange - minRect[i].points( rect_points ); - - //construct contour based on rectangle points because contour != rectangle with points - vector<Point> contourRect; - for(int j=0;j<4;j++) - contourRect.push_back(rect_points[j]); - - //calc the area of the contour - double area0 = contourArea(contourRect); - //over stepp all small areas - if(area0<areaMinThreshold||area0>areaMaxThreshold) + //iterate all rectangles + for( size_t i = 0; i< minRect.size(); i++ ) { - //cout<<i<<":area0:"<<area0<<" dismissed "<<endl; - //skipe if the area is too small - continue; - } - else + Point2f rect_points[4]; + //get all points of the retange + minRect[i].points( rect_points ); + + //construct contour based on rectangle points because contour != rectangle with points + vector<Point> contourRect; + for(int j=0;j<4;j++) + contourRect.push_back(rect_points[j]); + + //calc the area of the contour + double area0 = contourArea(contourRect); + //over stepp all small areas + if(area0<areaMinThreshold||area0>areaMaxThreshold) + { + //cout<<i<<":area0:"<<area0<<" dismissed "<<endl; + //skipe if the area is too small + continue; + } + else + { + //cout<<i<<":area0:"<<area0<<" choose "<<endl; + + //get the center of this rectangle + Point2f center = minRect[i].center; + recCenterPoints.push_back(center); + + //we also add the this contour to a selection + contourSelection.push_back(contours[i]); + + } + }//end iterate all min rectangle + + if(!contourSelection.empty()) { - //cout<<i<<":area0:"<<area0<<" choose "<<endl; - - //get the center of this rectangle - Point2f center = minRect[i].center; - recCenterPoints.push_back(center); - - //we also add the this contour to a selection - contourSelection.push_back(contours[i]); - - } - }//end iterate all min rectangle - - //! step 5) we calc the moments from the contour selection - vector<Moments> mu(contourSelection.size() ); - for( size_t i = 0; i < contourSelection.size(); i++ ) - { - mu[i] = moments( contourSelection[i], false ); - } - - // Get the mass centers: - vector<Point2f> massCenters( contourSelection.size() ); - for( size_t i = 0; i < contourSelection.size(); i++ ) - { - massCenters[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); - } - - //! step 6) calc convex hull of all points of the contour selection ********* - //TODO produce center of convex hull of all polygones - //TODO double check if ne contour sharing a center point ? nearby ?? - - //we merge all points - vector<Point> allContourPoints; - for (size_t cC = 0; cC < contourSelection.size(); ++cC) - for(size_t cP =0; cP < contourSelection[cC].size(); cP++) + //! step 5) we calc the moments from the contour selection + vector<Moments> mu(contourSelection.size() ); + for( size_t i = 0; i < contourSelection.size(); i++ ) + { + mu[i] = moments( contourSelection[i], false ); + } + + // Get the mass centers: + massCenters = vector<Point2f>( contourSelection.size() ); + for( size_t i = 0; i < contourSelection.size(); i++ ) + { + massCenters[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); + } + + //! step 6) calc convex hull of all points of the contour selection ********* + //TODO produce center of convex hull of all polygones + //TODO double check if ne contour sharing a center point ? nearby ?? + + //we merge all points + vector<Point> allContourPoints; + for (size_t cC = 0; cC < contourSelection.size(); ++cC) + for(size_t cP =0; cP < contourSelection[cC].size(); cP++) + { + Point currentContourPixel = contourSelection[cC][cP]; + allContourPoints.push_back(currentContourPixel); + } + + // calc the hull ****************** + conHull = vector<Point>(allContourPoints.size()); + convexHull( Mat(allContourPoints), conHull, false ); + // Point roiCenter; + // float roiRadius; + + //calc the min circle around + //minEnclosingCircle(conHull,roiCenter,roiRadius); + //we calc the mass center of the convex hull + + ///we calc the mass center of the convex hull + Moments muConvexHull; + if(!conHull.empty()) + { + muConvexHull = moments(conHull, true ); + muConvexHullMassCenter= Point2f( muConvexHull.m10/muConvexHull.m00 , muConvexHull.m01/muConvexHull.m00 ); + } + }//end after selection is empty { - Point currentContourPixel = contourSelection[cC][cP]; - allContourPoints.push_back(currentContourPixel); + cerr<<"error, no contour found after selection in file: "<< fileName<<endl; + myFileListAfterContourSelection.push_back(fileName); } - - // calc the hull ****************** - vector<Point> conHull(allContourPoints.size()); - convexHull( Mat(allContourPoints), conHull, false ); - // Point roiCenter; - // float roiRadius; - - //calc the min circle around - //minEnclosingCircle(conHull,roiCenter,roiRadius); - //we calc the mass center of the convex hull - - ///we calc the mass center of the convex hull - Moments muConvexHull; - Point2f muConvexHullMassCenter(0.0,0.0); - if(!conHull.empty()) + }//end if contours are empty, to canny edge found nothing + else { - muConvexHull = moments(conHull, true ); - muConvexHullMassCenter= Point2f( muConvexHull.m10/muConvexHull.m00 , muConvexHull.m01/muConvexHull.m00 ); - } + cerr<<"error, no contour found at all in file: "<< fileName<<endl; + myFileListNoContour.push_back(fileName); + } #ifdef MC_SHOW_STEP_ANALYSE if(!conHull.empty())//if we any elements, we process further @@ -516,7 +536,7 @@ int main(int argc, char * argv[]) //check the string cout <<"write output nameOutPutFileData: "<< nameOutPutFileData<<endl; if(nameOutPutFileData.empty()) - cerr <<"error: empty string nameOutPutFileData"<<endl; + cerr <<"error: empty string nameOutPutFileData"<<endl; FileStorage fs(nameOutPutFileData.c_str(), FileStorage::WRITE); @@ -565,7 +585,7 @@ int main(int argc, char * argv[]) //check the string cout <<"write dbg file: "<< nameOutPutFileDBGpic<<endl; if(nameOutPutFileDBGpic.empty()) - cerr <<"error: empty string nameOutPutFileDBGpic"<<endl; + cerr <<"error: empty string nameOutPutFileDBGpic"<<endl; imwrite(nameOutPutFileDBGpic.c_str(),imgOverlay2); @@ -594,20 +614,30 @@ int main(int argc, char * argv[]) 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; + + //we write the random file list to a file std::string nameOutRandomFile = outputDir + "randlist.yml"; - FileStorage fs(nameOutRandomFile.c_str(), FileStorage::WRITE); fs << "randomlist" << myRandomTrainList; fs.release(); + //TODO we should merge the file + //we write the random file list to a file std::string nameOutErrorList = outputDir + "fileErrorList.yml"; - cout <<"amount of file errors: "<< myFileErrorList.size()<<endl; - FileStorage fs2(nameOutErrorList.c_str(), FileStorage::WRITE); - fs2 << "fileErrorList" << myFileErrorList; + fs2 << "fileErrorIO" << myFileErrorList; + + //we 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 << "fileErrorNoContoursAfterSelection" << myFileListAfterContourSelection; + fs2.release(); delete bgs;