UtiLite
0.3.1
A lite utilities library
|
00001 /* 00002 * utilite is a cross-platform library with 00003 * useful utilities for fast and small developing. 00004 * Copyright (C) 2010 Mathieu Labbe 00005 * 00006 * utilite is free library: you can redistribute it and/or modify 00007 * it under the terms of the GNU Lesser General Public License as published by 00008 * the Free Software Foundation, either version 3 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * utilite is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public License 00017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #pragma once 00021 00022 #include "utilite/UtiLiteCvExp.h" // DLL export/import defines 00023 00024 #include <opencv2/highgui/highgui.hpp> 00025 #include <opencv2/features2d/features2d.hpp> 00026 #include <utilite/UThreadNode.h> 00027 #include <utilite/UEventsHandler.h> 00028 #include <utilite/UEvent.h> 00029 #include <utilite/UDirectory.h> 00030 #include <utilite/UTimer.h> 00031 #include <utilite/UImageEvent.h> 00032 #include <set> 00033 #include <stack> 00034 #include <list> 00035 #include <vector> 00036 00041 class UTILITECV_EXP UAbstractImageCapture : 00042 public UThread 00043 { 00044 public: 00045 virtual ~UAbstractImageCapture(); 00046 cv::Mat takeImage(); 00047 virtual bool init() = 0; 00048 00049 //getters 00050 bool isPaused() const {return !this->isRunning();} 00051 bool isCapturing() const {return this->isRunning();} 00052 void getImageSize(unsigned int & width, unsigned int & height); 00053 float getImageRate() const {return _imageRate;} 00054 00055 //setters 00056 void setImageRate(float imageRate) {_imageRate = imageRate;} 00057 void setAutoRestart(bool autoRestart) {_autoRestart = autoRestart;} 00058 void setImageSize(unsigned int width, unsigned int height); 00059 00060 00061 int id() const {return _id;} 00062 00063 protected: 00069 UAbstractImageCapture(float imageRate = 0, bool autoRestart = false, unsigned int imageWidth = 0, unsigned int imageHeight = 0, unsigned int framesDropped = 0, int id = 0); 00070 00071 virtual cv::Mat captureImage() = 0; 00072 00073 private: 00074 virtual void mainLoopBegin(); 00075 virtual void mainLoop(); 00076 void process(); 00077 00078 private: 00079 float _imageRate; 00080 int _id; 00081 bool _autoRestart; 00082 unsigned int _imageWidth; 00083 unsigned int _imageHeight; 00084 unsigned int _framesDropped; 00085 UTimer _frameRateTimer; 00086 UMutex _imageSizeMutex; 00087 }; 00088 00089 00090 00094 class UTILITECV_EXP UImageFolderCapture : 00095 public UAbstractImageCapture 00096 { 00097 public: 00098 UImageFolderCapture(const std::string & path, 00099 int startAt = 1, 00100 bool refreshDir = false, 00101 float imageRate = 0, 00102 bool autoRestart = false, 00103 unsigned int imageWidth = 0, 00104 unsigned int imageHeight = 0, 00105 unsigned int framesDropped = 0, 00106 int id = 0); 00107 virtual ~UImageFolderCapture(); 00108 00109 virtual bool init(); 00110 std::string getPath() const {return _path;} 00111 00112 protected: 00113 virtual cv::Mat captureImage(); 00114 00115 private: 00116 std::string _path; 00117 int _startAt; 00118 // If the list of files in the directory is refreshed 00119 // on each call of takeImage() 00120 bool _refreshDir; 00121 UDirectory _dir; 00122 int _count; 00123 std::string _lastFileName; 00124 00125 00126 00127 }; 00128 00129 00130 00131 00135 class UTILITECV_EXP UVideoCapture : 00136 public UAbstractImageCapture 00137 { 00138 public: 00139 enum Source{kVideoFile, kUsbDevice}; 00140 00141 public: 00145 UVideoCapture(int usbDevice = 0, 00146 float imageRate = 0, 00147 bool autoRestart = false, 00148 unsigned int imageWidth = 0, 00149 unsigned int imageHeight = 0, 00150 unsigned int framesDropped = 0, 00151 int id = 0); 00155 UVideoCapture(const std::string & filePath, 00156 float imageRate = 0, 00157 bool autoRestart = false, 00158 unsigned int imageWidth = 0, 00159 unsigned int imageHeight = 0, 00160 unsigned int framesDropped = 0, 00161 int id = 0); 00162 virtual ~UVideoCapture(); 00163 00164 virtual bool init(); 00165 int getUsbDevice() const {return _usbDevice;} 00166 const std::string & getFilePath() const {return _filePath;} 00167 00168 protected: 00169 virtual cv::Mat captureImage(); 00170 00171 private: 00172 // File type 00173 std::string _filePath; 00174 00175 cv::VideoCapture _capture; 00176 Source _src; 00177 00178 // Usb camera 00179 int _usbDevice; 00180 };