$search
00001 00005 #ifndef _THERMALVIS_CALIBRATOR_H_ 00006 #define _THERMALVIS_CALIBRATOR_H_ 00007 00008 #include "ros_resources.hpp" 00009 #include "general_resources.hpp" 00010 #include "opencv_resources.hpp" 00011 00012 #include <signal.h> 00013 00014 #include "improc.hpp" 00015 #include "video.hpp" 00016 #include "calibration.hpp" 00017 #include "intrinsics.hpp" 00018 #include "extrinsics.hpp" 00019 #include "features.hpp" 00020 00021 00022 00023 #include <ros/ros.h> 00024 #include <boost/thread/thread.hpp> 00025 #include <image_transport/image_transport.h> 00026 #include <opencv/cv.h> 00027 #include <opencv/highgui.h> 00028 #include <cv_bridge/CvBridge.h> 00029 00030 #include <dynamic_reconfigure/server.h> 00031 #include "calibratorConfig.h" 00032 00033 #define MAX_CAMS 16 00034 #define DEFAULT_MAX_PATTERNS 100 00035 #define DEFAULT_MAX_FRAMES 200 00036 #define DEFAULT_SET_SIZE 10 00037 #define DEFAULT_GRID_SIZE 10 00038 #define DEFAULT_X_COUNT 12 00039 #define DEFAULT_Y_COUNT 8 00040 00041 #define DEFAULT_MAX_TIME_DIFF 0.001 00042 00043 //HGH 00044 #define PATTERN_CODE_INVALID -1 00045 #define PATTERN_CODE_FIND_TRACK 0 00046 #define PATTERN_CODE_TRACK_FIND 1 00047 #define PATTERN_CODE_FIND 2 00048 00049 const char __PROGRAM__[] = "THERMAL_CALIBRATOR"; 00050 00051 bool wantsToShutdown = false; 00052 void mySigintHandler(int sig); 00053 00055 struct calibratorData { 00056 00057 string video_stream, video_sequence; 00058 00059 string patternType, optMethod; 00060 00061 double candidatePatternSampleRate; // Proportion of captured patterns to consider as candidates for model 00062 double testPatternSampleRate; // Proportion of captured patterns to use to test model accuracy 00063 00064 bool trackingMode; 00065 00066 double alpha; 00067 bool autoAlpha; 00068 00069 string outputFolder; 00070 00071 double maxTimeDiff; 00072 00073 double flowThreshold; 00074 //HGH 00075 double errorThreshold; 00076 00077 00078 double maxFrac; 00079 00080 string read_addr; 00081 00082 bool debugMode; 00083 00084 string calibType; 00085 00086 bool wantsIntrinsics; 00087 00088 Mat K1, K2; 00089 Mat distCoeffs1, distCoeffs2; 00090 00091 int maxPatterns; 00092 int maxFrames; 00093 int setSize; 00094 double gridSize; 00095 int xCount; 00096 int yCount; 00097 00098 int pattType; 00099 int optimizationMethod; 00100 00101 bool undistortImages; 00102 bool rectifyImages; 00103 00104 bool obtainStartingData(ros::NodeHandle& nh); 00105 00106 //HGH 00107 bool useDefinedTopics; 00108 string video_streamLeft, video_sequenceLeft, video_streamRight, video_sequenceRight; 00109 00110 bool useIntrinsicsFile; 00111 string intrinsicsFileLeft; 00112 string intrinsicsFileRight; 00113 00114 //HGH 00115 bool stopCapturing; 00116 string patternDetectionMode; 00117 int usePatternDetectionMode; 00118 bool doOnlineProcessing; 00119 bool useMutex; 00120 00121 //HGH 00122 double contrastLeft; 00123 double brightnessLeft; 00124 double contrastRight; 00125 double brightnessRight; 00126 bool normLeft, normRight; 00127 bool invertLeft, invertRight; 00128 00129 //HGH 00130 bool adjustMSERLeft; 00131 bool adjustMSERRight; 00132 int deltaLeft, deltaRight; 00133 double maxVarLeft, maxVarRight, minDivLeft, minDivRight; 00134 double areaThresholdLeft, areaThresholdRight; 00135 00136 }; 00137 00139 struct validPattern { 00140 int validFrameID; 00141 int patternID; 00142 bool patternFound; 00143 bool isValidFrame; 00144 }; 00145 00146 00147 // example: http://ua-ros-pkg.googlecode.com/svn/trunk/arrg/ua_vision/object_tracking/src/object_tracker_node.cpp 00149 class calibratorNode { 00150 private: 00151 00152 ros::NodeHandle *ref; 00153 00154 Mat prevMat1, prevMat2; 00155 //HGH 00156 Mat prevMat; 00157 Mat grayMat1, grayMat2; 00158 00159 struct timeval cycle_timer; 00160 double elapsedTime; 00161 double avgTime; 00162 00163 Mat map1[MAX_CAMS], map2[MAX_CAMS]; 00164 int topValidHeight, botValidHeight, leftValid[2], rightValid[2]; 00165 00166 ros::Timer timer; 00167 00168 vector<Mat> displayImages_1, displayImages_2; 00169 00170 vector<ros::Time> times_1, times_2; 00171 00172 vector<unsigned int> validPairs[2]; 00173 00174 unsigned int numCams; 00175 00176 bool infoProcessed, infoProcessed_1, infoProcessed_2; 00177 bool stillCollecting; 00178 00179 //HGH 00180 bool stillCollectingSet[2]; 00181 00182 double alpha; 00183 00184 Mat default_R; 00185 00186 Mat cameraMatrices[MAX_CAMS]; 00187 Mat distCoeffVecs[MAX_CAMS]; 00188 00189 Rect roi[2]; 00190 vector<Point2f> rectangleBounds, newRecBounds; 00191 Mat rectCamMat[2]; 00192 vector<Point2f> leftLinePoints, rightLinePoints; 00193 00194 Size imSize[MAX_CAMS]; 00195 Mat newCamMat[MAX_CAMS]; 00196 00197 double reprojectionError_intrinsics[MAX_CAMS]; 00198 double extendedReprojectionError_intrinsics[MAX_CAMS]; 00199 00200 vector<unsigned char> duplicateFlags_1, duplicateFlags_2; 00201 00202 vector<int> subselectedTags_intrinsics[MAX_CAMS]; 00203 00204 string topic, topic_1, topic_2; 00205 string topic_info, topic_info_1, topic_info_2; 00206 00207 int publishCount; 00208 00209 // Extrinsics: 00210 Mat R_[2], P_[2]; 00211 Mat E[2], F[2], Q1, Q2; // Between first camera and all other cameras 00212 Mat R[2], Rv[2], T[2]; // Rotations/translations between first camera and all other cameras 00213 Mat R2[2], T2[2]; // Rotations/translations between all other cameras 00214 00215 double stereoError; 00216 double extendedExtrinsicReprojectionError; 00217 00218 bool alphaChanged; 00219 00220 calibratorData configData; 00221 00222 sensor_msgs::CameraInfo debug_camera_info_1, debug_camera_info_2; 00223 00224 image_transport::CameraPublisher debug_pub_1, debug_pub_2; 00225 00226 image_transport::CameraSubscriber camera_sub, camera_sub_1, camera_sub_2; 00227 00228 //ros::Subscriber info_sub, info_sub_1, info_sub_2; 00229 //image_transport::Subscriber image_sub, image_sub_1, image_sub_2; 00230 00231 cv_bridge::CvImagePtr cv_ptr, cv_ptr_1, cv_ptr_2; 00232 00233 char debug_pub_name[256], debug_pub_name_1[256], debug_pub_name_2[256]; 00234 00235 sensor_msgs::Image msg_debug_1, msg_debug_2; 00236 00237 ros::NodeHandle private_node_handle; 00238 00239 char nodeName[256]; 00240 00241 Mat newImage, lastImage; 00242 00243 unsigned int checkIndex_1, checkIndex_2; 00244 int frameCount, undistortionCount, rectificationCount, frameCount_1, frameCount_2; 00245 //HGH 00246 int totalFrameCount_1, totalFrameCount_2; 00247 00248 cv::vector<Point3f> row; 00249 00250 vector<vector<Point2f> > pointSets[MAX_CAMS], candidateSets[MAX_CAMS], testingSets[MAX_CAMS]; 00251 vector<vector<Point2f> > extrinsicsPointSets[MAX_CAMS], extrinsicCandidateSets[MAX_CAMS], extrinsicTestingSets[MAX_CAMS]; 00252 00253 //HGH 00254 Mat lastMat[MAX_CAMS]; 00255 vector<Point2f> cornerSets[MAX_CAMS]; 00256 vector<int> frameCounts[MAX_CAMS]; 00257 bool doVerify; 00258 00259 00260 boost::shared_mutex _access; 00261 00262 bool readyForOutput; 00263 00264 dynamic_reconfigure::Server<thermalvis::calibratorConfig> server; 00265 dynamic_reconfigure::Server<thermalvis::calibratorConfig>::CallbackType f; 00266 00267 vector<Point2f> cornerSet_1, cornerSet_2; 00268 //HGH 00269 vector<Point2f> cornerSet; 00270 00271 //HGH 00272 void preprocessImage(Mat src, Mat &dst, double a, double b, bool normaliz, bool negative ); 00273 00274 00275 00276 public: 00277 calibratorNode(ros::NodeHandle& nh, calibratorData startupData); 00278 00279 void performIntrinsicCalibration(); 00280 void performExtrinsicCalibration(); 00281 00282 void handle_image(const sensor_msgs::ImageConstPtr& msg_ptr, const sensor_msgs::CameraInfoConstPtr& info_msg); 00283 void handle_image_1(const sensor_msgs::ImageConstPtr& msg_ptr, const sensor_msgs::CameraInfoConstPtr& info_msg); 00284 void handle_image_2(const sensor_msgs::ImageConstPtr& msg_ptr, const sensor_msgs::CameraInfoConstPtr& info_msg); 00285 00286 00287 void assignDebugCameraInfo(); 00288 00289 void imageCallback(const sensor_msgs::ImageConstPtr& msg); 00290 void parameterCallback(const sensor_msgs::CameraInfo& msg); 00291 00292 void publishUndistorted(const ros::TimerEvent& event); 00293 void startUndistortionPublishing(); 00294 00295 void publishRectified(const ros::TimerEvent& event); 00296 void startRectificationPublishing(); 00297 00298 bool isStillCollecting(); 00299 bool wantsToUndistort(); 00300 bool wantsToRectify(); 00301 void writeResults(); 00302 bool wantsIntrinsics(); 00303 bool wantsExtrinsics(); 00304 void updatePairs(); 00305 00306 //HGH 00307 void determineValidPairs(); 00308 void evaluateFrames(); 00309 00310 void getAverageTime(); 00311 void assignIntrinsics(); 00312 00313 void preparePatternSubsets(); 00314 void prepareExtrinsicPatternSubsets(); 00315 00316 void serverCallback(thermalvis::calibratorConfig &config, uint32_t level); 00317 void updateMap(); 00318 00319 bool findPattern(const Mat& im, vector<Point2f>& dst, Mat& prev, const int useMode, const int cameraNumber); 00320 00321 void updateIntrinsicMap(unsigned int idx); 00322 void set_ready_for_output(); 00323 00324 void prepareForTermination(); 00325 00326 //HGH 00327 void trackPattern(int cameraNumber, const Mat& currentImg, const int currentFrame); 00328 bool isVerifying(); 00329 00330 00331 00332 }; 00333 00334 boost::shared_ptr < calibratorNode > *globalNodePtr; 00335 00336 #endif