//------------------------------------------------------------------------------------------- /*! \file blob_tracker2_test.cpp \brief Marker tracker (blob tracker) of FingerVision. \author Akihiko Yamaguchi, info@akihikoy.net \version 0.1 \date May.10, 2016 \version 0.2 \date Aug.9, 2018 Using the same core programs as the ROS version. Supporting to load YAML configuration. Supporting camera rectification. \version 0.3 \date May.20, 2019 Modified the trackbars. g++ -g -Wall -O2 -o blob_tracker2_test.out blob_tracker2_test.cpp -I../3rdparty -I../fv_core -lopencv_core -lopencv_imgproc -lopencv_features2d -lopencv_calib3d -lopencv_highgui -lopencv_videoio Run: $ ./blob_tracker2_test.out [CAMERA_NUMBER [WIDTH HEIGHT]] CAMERA_NUMBER: Camera device number (e.g. 1). $ ./blob_tracker2_test.out CONFIG_FILE CONFIG_FILE: Configuration file in YAML (e.g. fv_1.yaml). The configuration file may include camera configuration (CameraInfo) and the configuration of blob tracker (BlobTracker2). Usage: Press 'q' or Esc: Exit the program. Press 'c': Calibrate the tracker (detecting markers and storing the initial positions and sizes). Tips: Show a white paper or white wall during the calibration. Press 'C': Switch the parameter configuration trackbar mode. Press 'P': Save the tracker parameter into a file in /tmp. Press 'W' (shift+'w'): Start/stop video recording. Press 'p': Print the calibration result. Press 's': Save the calibration result to file "blob_calib.yaml". Press 'l': Load a calibration from file "blob_calib.yaml". */ //------------------------------------------------------------------------------------------- #include "blob_tracker2.h" #include "ay_vision/vision_util.h" #include "ay_cpp/cpp_util.h" #include "ay_cpp/sys_util.h" //------------------------------------------------------------------------------------------- #include "blob_tracker2.cpp" #include "ay_vision/vision_util.cpp" //------------------------------------------------------------------------------------------- using namespace std; using namespace trick; //------------------------------------------------------------------------------------------- cv::Mat Capture(cv::VideoCapture &cap, TCameraInfo &info) { cv::Mat frame; while(!cap.read(frame)) { if(CapWaitReopen(info,cap)) continue; else return cv::Mat(); } return frame; } //------------------------------------------------------------------------------------------- struct TMouseEventData { cv::Mat &frame; TBlobTracker2 &tracker; TMouseEventData(cv::Mat &f, TBlobTracker2 &t) : frame(f), tracker(t) {} }; void OnMouse(int event, int x, int y, int flags, void *data) { if(event == cv::EVENT_RBUTTONDOWN && (flags & cv::EVENT_FLAG_SHIFTKEY)) { TMouseEventData &d(*reinterpret_cast(data)); d.tracker.RemovePointAt(cv::Point2f(x,y)); } } //------------------------------------------------------------------------------------------- int main(int argc, char**argv) { std::string cam("0"), config_file; if(argc>1) cam= argv[1]; std::vector cam_info; // If cam exists as a file, it is considered as a configuration YAML file. if(FileExists(cam)) { ReadFromYAML(cam_info, cam); config_file= cam; } else { cam_info.push_back(TCameraInfo()); cam_info[0].DevID= cam; cam_info[0].Width= ((argc>2)?atoi(argv[2]):320); cam_info[0].Height= ((argc>3)?atoi(argv[3]):240); } cv::VideoCapture cap; if(!CapOpen(cam_info[0], cap)) return -1; TCameraRectifier cam_rectifier; if(cam_info[0].Rectification) { cv::Size size_in(cam_info[0].Width,cam_info[0].Height), size_out(cam_info[0].Width,cam_info[0].Height); cam_rectifier.Setup(cam_info[0].K, cam_info[0].D, cam_info[0].R, size_in, cam_info[0].Alpha, size_out); } TBlobTracker2 tracker; if(config_file=="") { tracker.Init(); } else { std::vector blobtrack_info; ReadFromYAML(blobtrack_info, config_file); tracker.Params()= blobtrack_info[0]; tracker.Init(); } bool calib_request(true); std::string blob_calib_yaml("blob_calib.yaml"); if(FileExists(blob_calib_yaml)) { tracker.LoadCalib(blob_calib_yaml); std::cerr<<"Loaded calibration data from "<=int(sizeof(dim_levels)/sizeof(dim_levels[0]))) dim_idx=0; } else if(c=='p') tracker.SaveCalib("/dev/stdout"); else if(c=='s') { tracker.SaveCalib(blob_calib_yaml); std::cerr<<"Saved calibration data to "< p; p.push_back(tracker.Params()); WriteToYAML(p,"/tmp/blobtr_params.yaml"); std::cerr<<"Parameters of the tracker are saved into /tmp/blobtr_params.yaml"< frames; for(int i(0); i