00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 00005 | | 00006 | Copyright (C) 2005-2010 University of Malaga | 00007 | | 00008 | This software was written by the Machine Perception and Intelligent | 00009 | Robotics Lab, University of Malaga (Spain). | 00010 | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> | 00011 | | 00012 | This file is part of the MRPT project. | 00013 | | 00014 | MRPT is free software: you can redistribute it and/or modify | 00015 | it under the terms of the GNU General Public License as published by | 00016 | the Free Software Foundation, either version 3 of the License, or | 00017 | (at your option) any later version. | 00018 | | 00019 | MRPT is distributed in the hope that it will be useful, | 00020 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00021 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 00022 | GNU General Public License for more details. | 00023 | | 00024 | You should have received a copy of the GNU General Public License | 00025 | along with MRPT. If not, see <http://www.gnu.org/licenses/>. | 00026 | | 00027 +---------------------------------------------------------------------------+ */ 00028 00029 #ifndef CCameraSensor_H 00030 #define CCameraSensor_H 00031 00032 #include <mrpt/poses/CPose3D.h> 00033 #include <mrpt/slam/CObservation.h> 00034 #include <mrpt/utils/CDebugOutputCapable.h> 00035 #include <mrpt/utils/CConfigFileBase.h> 00036 #include <mrpt/hwdrivers/CGenericSensor.h> 00037 00038 #include <mrpt/hwdrivers/CFFMPEG_InputStream.h> 00039 #include <mrpt/hwdrivers/CImageGrabber_OpenCV.h> 00040 #include <mrpt/hwdrivers/CImageGrabber_dc1394.h> 00041 #include <mrpt/hwdrivers/CStereoGrabber_Bumblebee.h> 00042 #include <mrpt/hwdrivers/CSwissRanger3DCamera.h> 00043 00044 #include <mrpt/utils/CFileGZInputStream.h> 00045 #include <mrpt/hwdrivers/CStereoGrabber_SVS.h> 00046 00047 #include <mrpt/gui/CDisplayWindow.h> 00048 00049 namespace mrpt 00050 { 00051 namespace hwdrivers 00052 { 00053 /** The central class for camera grabbers in MRPT, implementing the "generic sensor" interface. 00054 * This class provides the user with a uniform interface to a variety of other classes which manage only one specific camera "driver" (opencv, ffmpeg, bumblebee,...) 00055 * 00056 * Following the "generic sensor" interface, all the parameters must be passed int the form of a configuration file, which may be also formed on the fly (without being a real config file) as in this example: 00057 * 00058 * \code 00059 * CCameraSensor myCam; 00060 * const string str = 00061 * "[CONFIG]\n" 00062 * "grabber_type=opencv\n"; 00063 * 00064 * CConfigFileMemory cfg(str); 00065 * myCam.loadConfig(cfg,"CONFIG"); 00066 * myCam.initialize(); 00067 * CObservationPtr obs = myCam.getNextFrame(); 00068 * \endcode 00069 * 00070 * Images can be retrieved through the normal "doProcess()" interface, or the specific method "getNextFrame()". 00071 * 00072 * Some notes: 00073 * - "grabber_type" determines the class to use internally for image capturing (see below). 00074 * - For the meaning of cv_camera_type and other parameters, refer to mrpt::hwdrivers::CImageGrabber_OpenCV 00075 * - For the parameters of dc1394 parameters, refer to generic IEEE1394 documentation, and to mrpt::hwdrivers::TCaptureOptions_dc1394. 00076 * - If all the existing parameter annoy you, try the function prepareVideoSourceFromUserSelection(), which displays a GUI dialog to the user so he/she can choose the desired camera & its parameters. 00077 * 00078 * Images can be saved in the "external storage" mode. See setPathForExternalImages and setExternalImageFormat. These methods 00079 * are called automatically from rawlog-grabber. 00080 * 00081 * These is the list of all accepted parameters: 00082 * 00083 * \code 00084 * PARAMETERS IN THE ".INI"-LIKE CONFIGURATION STRINGS: 00085 * ------------------------------------------------------- 00086 * [supplied_section_name] 00087 * // Select one of the grabber implementations ----------------------- 00088 * grabber_type = opencv | dc1394 | bumblebee | ffmpeg | rawlog | swissranger 00089 * 00090 * // Options for any grabber_type ------------------------------------ 00091 * preview_decimation = 0 // N<=0 (or not present): No preview; N>0, display 1 out of N captured frames. 00092 * preview_reduction = 0 // 0 or 1 (or not present): The preview shows the actual image. For 2,3,..., reduces the size of the image by that factor, only for the preview window. 00093 * capture_grayscale = 0 // 1:capture in grayscale, whenever the driver allows it. Default=0 00094 * // For externaly stored images, the format of image files (default=jpg) 00095 * //external_images_format = jpg 00096 * 00097 * // For externaly stored images: whether to spawn independent threads to save the image files. 00098 * //external_images_own_thread = 1 // 0 or 1 00099 * 00100 * // If external_images_own_thread=1, this changes the number of threads to launch 00101 * // to save image files. The default is determined from mrpt::system::getNumberOfProcessors() 00102 * // and should be OK unless you want to save processor time for other things. 00103 * //external_images_own_thread_count = 2 // >=1 00104 * 00105 * // (Only when external_images_format=jpg): Optional parameter to set the JPEG compression quality: 00106 * //external_images_jpeg_quality = 95 // [1-100]. Default: 95 00107 * 00108 * // Pose of the sensor on the robot: 00109 * pose_x=0 ; (meters) 00110 * pose_y=0 00111 * pose_z=0 00112 * pose_yaw=0 ; (Angles in degrees) 00113 * pose_pitch=0 00114 * pose_roll=0 00115 * 00116 * // Options for grabber_type= opencv ------------------------------------ 00117 * cv_camera_index = 0 // [opencv] Number of camera to open 00118 * cv_camera_type = CAMERA_CV_AUTODETECT 00119 * cv_frame_width = 640 // [opencv] Capture width (not present or set to 0 for default) 00120 * cv_frame_height = 480 // [opencv] Capture height (not present or set to 0 for default) 00121 * cv_fps = 15 // [opencv] IEEE1394 cams only: Capture FPS (not present or 0 for default) 00122 * cv_gain = 0 // [opencv] Camera gain, if available (nor present or set to 0 for default). 00123 * 00124 * // Options for grabber_type= dc1394 ------------------------------------- 00125 * dc1394_camera_guid = 0 | 0x11223344 // 0 (or not present): the first camera; A hexadecimal number: The GUID of the camera to open 00126 * dc1394_camera_unit = 0 // 0 (or not present): the first camera; 0,1,2,...: The unit number (within the given GUID) of the camera to open (Stereo cameras: 0 or 1) 00127 * dc1394_frame_width = 640 00128 * dc1394_frame_height = 480 00129 * dc1394_framerate = 15 // eg: 7.5, 15, 30, 60, etc... For posibilities see mrpt::hwdrivers::TCaptureOptions_dc1394 00130 * dc1394_mode7 = -1 // -1: Ignore, i>=0, set to MODE7_i 00131 * dc1394_color_coding = COLOR_CODING_YUV422 // For posibilities see mrpt::hwdrivers::TCaptureOptions_dc1394 00132 * dc1394_shutter = -1 // A value, or -1 (or not present) for not to change this parameter in the camera 00133 * dc1394_gain = -1 // A value, or -1 (or not present) for not to change this parameter in the camera 00134 * dc1394_gamma = -1 // A value, or -1 (or not present) for not to change this parameter in the camera 00135 * dc1394_brightness = -1 // A value, or -1 (or not present) for not to change this parameter in the camera 00136 * dc1394_exposure = -1 // A value, or -1 (or not present) for not to change this parameter in the camera 00137 * dc1394_sharpness = -1 // A value, or -1 (or not present) for not to change this parameter in the camera 00138 * dc1394_white_balance = -1 // A value, or -1 (or not present) for not to change this parameter in the camera 00139 * 00140 * // Options for grabber_type= bumblebee ---------------------------------- 00141 * bumblebee_camera_index = 0 // [bumblebee] Number of camera within the firewire bus to open (typically = 0) 00142 * bumblebee_frame_width = 640 // [bumblebee] Capture width (not present or set to 0 for default) 00143 * bumblebee_frame_height = 480 // [bumblebee] Capture height (not present or set to 0 for default) 00144 * bumblebee_fps = 15 // [bumblebee] Capture FPS (not present or 0 for default) 00145 * bumblebee_mono = 0|1 // [bumblebee] OPTIONAL: If this parameter is present, monocular (0:left, 1:right) images will be grabbed instead of stereo pairs. 00146 * bumblebee_get_rectified = 0|1 // [bumblebee] Determines if the camera should grab rectified or raw images (1 is the default) 00147 * 00148 * // Options for grabber_type= ffmpeg ------------------------------------- 00149 * ffmpeg_url = rtsp://127.0.0.1 // [ffmpeg] The video file or IP camera to open 00150 * 00151 * // Options for grabber_type= rawlog ------------------------------------- 00152 * rawlog_file = mylog.rawlog // [rawlog] This can be used to simulate the capture of images already grabbed in the past in the form of a MRPT rawlog. 00153 * rawlog_camera_sensor_label = CAMERA1 // [rawlog] If this field is not present, all images found in the rawlog will be retrieved. Otherwise, only those observations with a matching sensor label. 00154 * 00155 * // Options for grabber_type= swissranger ------------------------------------- 00156 * sr_use_usb = true // True: use USB, false: use ethernet 00157 * sr_IP = 192.168.2.14 // If sr_use_usb=false, the camera IP 00158 * sr_grab_grayscale = true // whether to save the intensity channel 00159 * sr_grab_3d = true // whether to save the 3D points 00160 * sr_grab_range = true // whether to save the range image 00161 * sr_grab_confidence = true // whether to save the confidence image 00162 * 00163 * \endcode 00164 * 00165 * \note The execution rate (in rawlog-grabber) should be greater than the required capture FPS. 00166 * \note In Linux you may need to execute "chmod 666 /dev/video1394/ * " and "chmod 666 /dev/raw1394" for allowing any user R/W access to firewire cameras. 00167 * \sa mrpt::hwdrivers::CImageGrabber_OpenCV, mrpt::hwdrivers::CImageGrabber_dc1394, CGenericSensor, prepareVideoSourceFromUserSelection 00168 */ 00169 class HWDRIVERS_IMPEXP CCameraSensor : public utils::CDebugOutputCapable, public CGenericSensor 00170 { 00171 DEFINE_GENERIC_SENSOR(CCameraSensor) 00172 00173 public: 00174 /** Constructor 00175 * The camera is not open until "initialize" is called. 00176 */ 00177 CCameraSensor(); 00178 00179 /** Destructor 00180 */ 00181 virtual ~CCameraSensor(); 00182 00183 /** This method should be called periodically (at least at 1Hz to capture ALL the real-time data) 00184 * It is thread safe, i.e. you can call this from one thread, then to other methods from other threads. 00185 */ 00186 void doProcess(); 00187 00188 /** Retrieves the next frame from the video source, raising an exception on any error. 00189 * Note: The returned observations can be of one of these classes (you can use IS_CLASS(obs,CObservationXXX) to determine it): 00190 * - mrpt::slam::CObservationImage (For normal cameras or video sources) 00191 * - mrpt::slam::CObservationStereoImages (For stereo cameras) 00192 * - mrpt::slam::CObservation3DRangeScan (For 3D cameras) 00193 */ 00194 mrpt::slam::CObservationPtr getNextFrame(); 00195 00196 /** Tries to open the camera, after setting all the parameters with a call to loadConfig. 00197 * \exception This method must throw an exception with a descriptive message if some critical error is found. 00198 */ 00199 virtual void initialize(); 00200 00201 /** Close the camera (if open). 00202 * This method is called automatically on destruction. 00203 */ 00204 void close(); 00205 00206 /** Set the path where to save off-rawlog image files (this class DOES take into account this path). 00207 * An empty string (the default value at construction) means to save images embedded in the rawlog, instead of on separate files. 00208 * \exception std::exception If the directory doesn't exists and cannot be created. 00209 */ 00210 virtual void setPathForExternalImages( const std::string &directory ); 00211 00212 /** This must be called before initialize() */ 00213 void enableLaunchOwnThreadForSavingImages(bool enable=true) { m_external_images_own_thread = enable; }; 00214 00215 protected: 00216 poses::CPose3D m_sensorPose; 00217 00218 std::string m_grabber_type; //!< Can be "opencv",... 00219 bool m_capture_grayscale; 00220 int m_cv_camera_index; 00221 std::string m_cv_camera_type; 00222 mrpt::hwdrivers::TCaptureCVOptions m_cv_options; 00223 00224 uint64_t m_dc1394_camera_guid; 00225 int m_dc1394_camera_unit; 00226 mrpt::hwdrivers::TCaptureOptions_dc1394 m_dc1394_options; 00227 int m_preview_decimation; 00228 int m_preview_reduction; 00229 00230 int m_bumblebee_camera_index; 00231 mrpt::hwdrivers::TCaptureOptions_bumblebee m_bumblebee_options; 00232 int m_bumblebee_monocam; // 0:Left, 1: Right, <0,>1 -> Stereo 00233 00234 int m_svs_camera_index; 00235 mrpt::hwdrivers::TCaptureOptions_SVS m_svs_options; 00236 00237 std::string m_ffmpeg_url; 00238 00239 std::string m_rawlog_file; 00240 std::string m_rawlog_camera_sensor_label; 00241 std::string m_rawlog_detected_images_dir; 00242 00243 bool m_sr_open_from_usb; //!< true: USB, false: ETH 00244 std::string m_sr_ip_address; 00245 bool m_sr_save_3d; //!< Save the 3D point cloud (default: true) 00246 bool m_sr_save_range_img; //!< Save the 2D range image (default: true) 00247 bool m_sr_save_intensity_img; //!< Save the 2D intensity image (default: true) 00248 bool m_sr_save_confidence; //!< Save the estimated confidence 2D image (default: false) 00249 00250 bool m_external_images_own_thread; //!< Whether to launch independent thread 00251 00252 /** Loads specific configuration for the device from a given source of configuration parameters, for example, an ".ini" file, loading from the section "[iniSection]" (see utils::CConfigFileBase and derived classes) 00253 * See hwdrivers::CCameraSensor for the possible parameters 00254 */ 00255 void loadConfig_sensorSpecific( 00256 const mrpt::utils::CConfigFileBase &configSource, 00257 const std::string &iniSection ); 00258 00259 private: 00260 // Only one of these will be !=NULL at a time =========== 00261 CImageGrabber_OpenCV *m_cap_cv; //!< The OpenCV capture object. 00262 CImageGrabber_dc1394 *m_cap_dc1394; //!< The dc1394 capture object. 00263 CStereoGrabber_Bumblebee *m_cap_bumblebee; //!< The bumblebee capture object. 00264 mrpt::hwdrivers::CStereoGrabber_SVS *m_cap_svs; //!< The svs capture object. 00265 CFFMPEG_InputStream *m_cap_ffmpeg; //!< The FFMPEG capture object 00266 mrpt::utils::CFileGZInputStream *m_cap_rawlog; //!< The input file for rawlogs 00267 CSwissRanger3DCamera *m_cap_swissranger; //!< SR 3D camera object. 00268 // ========================= 00269 00270 int m_camera_grab_decimator; 00271 int m_camera_grab_decimator_counter; 00272 00273 int m_preview_counter; 00274 mrpt::gui::CDisplayWindowPtr m_preview_win1,m_preview_win2; //!< Normally we'll use only one window, but for stereo images we'll use two of them. 00275 00276 /** @name Stuff related to working threads to save images to disk 00277 @{ */ 00278 unsigned int m_external_image_saver_count; //!< Number of working threads. Default:1, set to 2 in quad cores. 00279 std::vector<mrpt::system::TThreadHandle> m_threadImagesSaver; 00280 00281 bool m_threadImagesSaverShouldEnd; 00282 mrpt::synch::CCriticalSection m_csToSaveList; //!< The critical section for m_toSaveList 00283 std::vector<TListObservations> m_toSaveList; //!< The queues of objects to be returned by getObservations, one for each working thread. 00284 void thread_save_images(unsigned int my_working_thread_index); //!< Thread to save images to files. 00285 /** @} */ 00286 00287 }; // end class 00288 00289 typedef stlplus::smart_ptr<CCameraSensor> CCameraSensorPtr; //!< A smart pointer to a CCameraSensor 00290 00291 /** Used only from MRPT apps: Use with caution since "panel" MUST be a "mrpt::gui::CPanelCameraSelection *" 00292 */ 00293 CCameraSensorPtr HWDRIVERS_IMPEXP prepareVideoSourceFromPanel(void *panel); 00294 00295 /** Parse the user options in the wxWidgets "panel" and write the configuration into the given section of the given configuration file. 00296 * Use with caution since "panel" MUST be a "mrpt::gui::CPanelCameraSelection *" 00297 * \sa prepareVideoSourceFromUserSelection, prepareVideoSourceFromPanel, readConfigIntoVideoSourcePanel 00298 */ 00299 void HWDRIVERS_IMPEXP writeConfigFromVideoSourcePanel( 00300 void *panel, 00301 const std::string &in_cfgfile_section_name, 00302 mrpt::utils::CConfigFileBase *out_cfgfile 00303 ); 00304 00305 /** Parse the given section of the given configuration file and set accordingly the controls of the wxWidgets "panel". 00306 * Use with caution since "panel" MUST be a "mrpt::gui::CPanelCameraSelection *" 00307 * \sa prepareVideoSourceFromUserSelection, prepareVideoSourceFromPanel, writeConfigFromVideoSourcePanel 00308 */ 00309 void HWDRIVERS_IMPEXP readConfigIntoVideoSourcePanel( 00310 void *panel, 00311 const std::string &in_cfgfile_section_name, 00312 const mrpt::utils::CConfigFileBase *in_cfgfile 00313 ); 00314 00315 /** Show to the user a list of possible camera drivers and creates and open the selected camera. 00316 */ 00317 CCameraSensorPtr HWDRIVERS_IMPEXP prepareVideoSourceFromUserSelection(); 00318 00319 00320 } // end namespace 00321 } // end namespace 00322 00323 #endif
| Page generated by Doxygen 1.7.1 for MRPT 0.9.1 SVN:exported at Fri Aug 6 10:31:35 UTC 2010 |
