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 #include <mrpt/hwdrivers/CFFMPEG_InputStream.h> 00038 #include <mrpt/hwdrivers/CImageGrabber_OpenCV.h> 00039 #include <mrpt/hwdrivers/CImageGrabber_dc1394.h> 00040 #include <mrpt/hwdrivers/CStereoGrabber_Bumblebee.h> 00041 #include <mrpt/utils/CFileGZInputStream.h> 00042 00043 #include <mrpt/gui/CDisplayWindow.h> 00044 00045 namespace mrpt 00046 { 00047 namespace hwdrivers 00048 { 00049 /** The central class for camera grabbers in MRPT, implementing the "generic sensor" interface. 00050 * 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,...) 00051 * 00052 * 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: 00053 * 00054 * \code 00055 * CCameraSensor myCam; 00056 * string str = "[CONFIG]\n grabber_type=opencv \n"; 00057 * CConfigFileMemory cfg(str); 00058 * myCam.loadConfig(cfg,"CONFIG"); 00059 * myCam.initialize(); 00060 * CObservationPtr obs = myCam.getNextFrame(); 00061 * \endcode 00062 * 00063 * Images can be retrieves through the normal "doProcess()" interface, or the specific method "getNextFrame()". 00064 * 00065 * These is the list of all the accepted parameters: 00066 * 00067 * \code 00068 * PARAMETERS IN THE ".INI"-LIKE CONFIGURATION STRINGS: 00069 * ------------------------------------------------------- 00070 * [supplied_section_name] 00071 * // Select one of the grabber implementations: 00072 * grabber_type = opencv | dc1394 | bumblebee | ffmpeg | rawlog 00073 * preview_decimation = 0 // N<=0 (or not present): No preview; N>0, display 1 out of N captured frames. 00074 * 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. 00075 * capture_grayscale = 0 // 1:capture in grayscale, whenever the driver allows it. Default=0 00076 * 00077 * // Options for grabber_type= opencv 00078 * cv_camera_index = 0 // [opencv] Number of camera to open 00079 * cv_camera_type = CAMERA_CV_AUTODETECT 00080 * cv_frame_width = 640 // [opencv] Capture width (not present or set to 0 for default) 00081 * cv_frame_height = 480 // [opencv] Capture height (not present or set to 0 for default) 00082 * cv_fps = 15 // [opencv] IEEE1394 cams only: Capture FPS (not present or 0 for default) 00083 * cv_gain = 0 // [opencv] Camera gain, if available (nor present or set to 0 for default). 00084 * 00085 * // Options for grabber_type= dc1394 00086 * dc1394_camera_guid = 0 | 0x11223344 // 0 (or not present): the first camera; A hexadecimal number: The GUID of the camera to open 00087 * 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) 00088 * dc1394_frame_width = 640 00089 * dc1394_frame_height = 480 00090 * dc1394_framerate = 15 // eg: 7.5, 15, 30, 60, etc... For posibilities see mrpt::hwdrivers::TCaptureOptions_dc1394 00091 * dc1394_mode7 = -1 // -1: Ignore, i>=0, set to MODE7_i 00092 * dc1394_color_coding = COLOR_CODING_YUV422 // For posibilities see mrpt::hwdrivers::TCaptureOptions_dc1394 00093 * dc1394_shutter = -1 // A value, or -1 (or not present) for not to change this parameter in the camera 00094 * dc1394_gain = -1 // A value, or -1 (or not present) for not to change this parameter in the camera 00095 * dc1394_gamma = -1 // A value, or -1 (or not present) for not to change this parameter in the camera 00096 * dc1394_brightness = -1 // A value, or -1 (or not present) for not to change this parameter in the camera 00097 * dc1394_exposure = -1 // A value, or -1 (or not present) for not to change this parameter in the camera 00098 * dc1394_sharpness = -1 // A value, or -1 (or not present) for not to change this parameter in the camera 00099 * dc1394_white_balance = -1 // A value, or -1 (or not present) for not to change this parameter in the camera 00100 * 00101 * // Options for grabber_type= bumblebee 00102 * bumblebee_camera_index = 0 // [bumblebee] Number of camera within the firewire bus to open (typically = 0) 00103 * bumblebee_frame_width = 640 // [bumblebee] Capture width (not present or set to 0 for default) 00104 * bumblebee_frame_height = 480 // [bumblebee] Capture height (not present or set to 0 for default) 00105 * bumblebee_fps = 15 // [bumblebee] Capture FPS (not present or 0 for default) 00106 * bumblebee_mono = 0|1 // [bumblebee] OPTIONAL: If this parameter is present, monocular (0:left, 1:right) images will be grabbed instead of stereo pairs. 00107 * bumblebee_get_rectified = 0|1 // [bumblebee] Determines if the camera should grab rectified or raw images (1 is the default) 00108 * 00109 * // Options for grabber_type= ffmpeg 00110 * ffmpeg_url = rtsp://127.0.0.1 // [ffmpeg] The video file or IP camera to open 00111 * 00112 * // Options for grabber_type= rawlog 00113 * 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. 00114 * 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. 00115 * 00116 * // For externaly stored images, the format of image files (default=jpg) 00117 * //external_images_format = jpg 00118 * 00119 * // For externaly stored images: whether to spawn independent threads to save the image files. 00120 * //external_images_own_thread = 1 // 0 or 1 00121 * 00122 * // If external_images_own_thread=1, this changes the number of threads to launch 00123 * // to save image files. The default is determined from mrpt::system::getNumberOfProcessors() 00124 * // and should be OK unless you want to save processor time for other things. 00125 * //external_images_own_thread_count = 2 // >=1 00126 * 00127 * // (Only when external_images_format=jpg): Optional parameter to set the JPEG compression quality: 00128 * //external_images_jpeg_quality = 95 // [1-100]. Default: 95 00129 * 00130 * // Pose of the sensor on the robot: 00131 * pose_x=0 ; (meters) 00132 * pose_y=0 00133 * pose_z=0 00134 * pose_yaw=0 ; (Angles in degrees) 00135 * pose_pitch=0 00136 * pose_roll=0 00137 * 00138 * \endcode 00139 * 00140 * - "grabber_type" is the class to use internally for image capturing. Choices are "opencv" (Windows & Linux) or "dc1394" (Linux only for now, requires libdc1394-2). 00141 * - For the meaning of cv_camera_type and other parameters, refer to mrpt::hwdrivers::CImageGrabber_OpenCV 00142 * - For the parameters of dc1394 parameters, refer to generic IEEE1394 documentation, and to mrpt::hwdrivers::TCaptureOptions_dc1394. 00143 * 00144 * Images can be saved in the "external storage" mode. See setPathForExternalImages and setExternalImageFormat. These methods 00145 * are called automatically from rawlog-grabber. 00146 * 00147 * \note The execution rate (in rawlog-grabber) should be greater than the required capture FPS. 00148 * \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. 00149 * \sa mrpt::hwdrivers::CImageGrabber_OpenCV, mrpt::hwdrivers::CImageGrabber_dc1394, CGenericSensor, prepareVideoSourceFromUserSelection 00150 */ 00151 class HWDRIVERS_IMPEXP CCameraSensor : public utils::CDebugOutputCapable, public CGenericSensor 00152 { 00153 DEFINE_GENERIC_SENSOR(CCameraSensor) 00154 00155 public: 00156 /** Constructor 00157 * The camera is not open until "initialize" is called. 00158 */ 00159 CCameraSensor(); 00160 00161 /** Destructor 00162 */ 00163 virtual ~CCameraSensor(); 00164 00165 /** This method should be called periodically (at least at 1Hz to capture ALL the real-time data) 00166 * It is thread safe, i.e. you can call this from one thread, then to other methods from other threads. 00167 */ 00168 void doProcess(); 00169 00170 /** Retrieves the next frame from the video source, raising an exception on any error. 00171 * \note The observations can be of the classes CObservationImage or CObservationStereoImages 00172 */ 00173 mrpt::slam::CObservationPtr getNextFrame(); 00174 00175 /** Tries to open the camera, after setting all the parameters with a call to loadConfig. 00176 * \exception This method must throw an exception with a descriptive message if some critical error is found. 00177 */ 00178 virtual void initialize(); 00179 00180 /** Close the camera (if open). 00181 * This method is called automatically on destruction. 00182 */ 00183 void close(); 00184 00185 /** Set the path where to save off-rawlog images: empty (default) means save images embedded in the rawlog. 00186 * \exception std::exception If the directory cannot be created 00187 */ 00188 void setPathForExternalImages( const std::string &directory ); 00189 00190 /** Set the extension ("jpg","gif","png",...) that determines the format of images saved externally 00191 * The default is "jpg". 00192 * \sa setPathForExternalImages, setExternalImageJPEGQuality 00193 */ 00194 void setExternalImageFormat( const std::string &ext ) { 00195 m_external_images_format = ext; 00196 } 00197 00198 /** The quality of JPEG compression, when external images is enabled and the format is "jpg". \sa setExternalImageFormat */ 00199 void setExternalImageJPEGQuality(const unsigned int quality) { 00200 m_external_images_jpeg_quality = quality; 00201 } 00202 unsigned int getExternalImageJPEGQuality()const { 00203 return m_external_images_jpeg_quality; 00204 } 00205 00206 /** This must be called before initialize() */ 00207 void enableLaunchOwnThreadForSavingImages(bool enable=true) { m_external_images_own_thread = enable; }; 00208 00209 protected: 00210 poses::CPose3D m_sensorPose; 00211 std::string m_sensorLabel; 00212 00213 std::string m_grabber_type; //!< Can be "opencv",... 00214 bool m_capture_grayscale; 00215 int m_cv_camera_index; 00216 std::string m_cv_camera_type; 00217 mrpt::hwdrivers::TCaptureCVOptions m_cv_options; 00218 00219 uint64_t m_dc1394_camera_guid; 00220 int m_dc1394_camera_unit; 00221 mrpt::hwdrivers::TCaptureOptions_dc1394 m_dc1394_options; 00222 int m_preview_decimation; 00223 int m_preview_reduction; 00224 00225 int m_bumblebee_camera_index; 00226 mrpt::hwdrivers::TCaptureOptions_bumblebee m_bumblebee_options; 00227 int m_bumblebee_monocam; // 0:Left, 1: Right, <0,>1 -> Stereo 00228 00229 std::string m_ffmpeg_url; 00230 00231 std::string m_rawlog_file; 00232 std::string m_rawlog_camera_sensor_label; 00233 std::string m_rawlog_detected_images_dir; 00234 00235 std::string m_path_for_external_images; //!< The path where to save off-rawlog images: empty means save images embedded in the rawlog. 00236 std::string m_external_images_format; //!< The extension ("jpg","gif","png",...) that determines the format of images saved externally \sa setPathForExternalImages 00237 unsigned int m_external_images_jpeg_quality; //!< For JPEG images, the quality (default=95%). 00238 00239 bool m_external_images_own_thread; //!< Whether to launch independent thread 00240 unsigned int m_external_image_saver_count; //!< Number of working threads. Default:1, set to 2 in quad cores. 00241 std::vector<mrpt::system::TThreadHandle> m_threadImagesSaver; 00242 00243 bool m_threadImagesSaverShouldEnd; 00244 00245 /** 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) 00246 * See hwdrivers::CCameraSensor for the possible parameters 00247 */ 00248 void loadConfig_sensorSpecific( 00249 const mrpt::utils::CConfigFileBase &configSource, 00250 const std::string &iniSection ); 00251 00252 private: 00253 /** The OpenCV capture object. */ 00254 mrpt::hwdrivers::CImageGrabber_OpenCV *m_cap_cv; 00255 00256 /** The dc1394 capture object. */ 00257 mrpt::hwdrivers::CImageGrabber_dc1394 *m_cap_dc1394; 00258 00259 /** The bumblebee capture object. */ 00260 mrpt::hwdrivers::CStereoGrabber_Bumblebee *m_cap_bumblebee; 00261 00262 /** The FFMPEG capture object */ 00263 CFFMPEG_InputStream *m_cap_ffmpeg; 00264 00265 /** The input file for rawlogs */ 00266 mrpt::utils::CFileGZInputStream *m_cap_rawlog; 00267 00268 int m_camera_grab_decimator; 00269 int m_camera_grab_decimator_counter; 00270 00271 int m_preview_counter; 00272 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. 00273 00274 mrpt::synch::CCriticalSection m_csToSaveList; //!< The critical section for m_toSaveList 00275 std::vector<TListObservations> m_toSaveList; //!< The queues of objects to be returned by getObservations, one for each working thread. 00276 00277 void thread_save_images(unsigned int my_working_thread_index); //!< Thread to save images to files. 00278 00279 }; // end class 00280 00281 typedef stlplus::smart_ptr<CCameraSensor> CCameraSensorPtr; //!< A smart pointer to a CCameraSensor 00282 00283 /** Used only from MRPT apps: Use with caution since "panel" MUST be a "mrpt::gui::CPanelCameraSelection *" 00284 */ 00285 CCameraSensorPtr HWDRIVERS_IMPEXP prepareVideoSourceFromPanel(void *panel); 00286 00287 /** Parse the user options in the wxWidgets "panel" and write the configuration into the given section of the given configuration file. 00288 * Use with caution since "panel" MUST be a "mrpt::gui::CPanelCameraSelection *" 00289 * \sa prepareVideoSourceFromUserSelection, prepareVideoSourceFromPanel, readConfigIntoVideoSourcePanel 00290 */ 00291 void HWDRIVERS_IMPEXP writeConfigFromVideoSourcePanel( 00292 void *panel, 00293 const std::string &in_cfgfile_section_name, 00294 mrpt::utils::CConfigFileBase *out_cfgfile 00295 ); 00296 00297 /** Parse the given section of the given configuration file and set accordingly the controls of the wxWidgets "panel". 00298 * Use with caution since "panel" MUST be a "mrpt::gui::CPanelCameraSelection *" 00299 * \sa prepareVideoSourceFromUserSelection, prepareVideoSourceFromPanel, writeConfigFromVideoSourcePanel 00300 */ 00301 void HWDRIVERS_IMPEXP readConfigIntoVideoSourcePanel( 00302 void *panel, 00303 const std::string &in_cfgfile_section_name, 00304 const mrpt::utils::CConfigFileBase *in_cfgfile 00305 ); 00306 00307 /** Show to the user a list of possible camera drivers and creates and open the selected camera. 00308 */ 00309 CCameraSensorPtr HWDRIVERS_IMPEXP prepareVideoSourceFromUserSelection(); 00310 00311 00312 } // end namespace 00313 } // end namespace 00314 00315 #endif
| Page generated by Doxygen 1.6.1 for MRPT 0.9.0 SVN: at Mon Jun 7 06:47:58 UTC 2010 |
