Image Component Library (ICL)
|
00001 /******************************************************************** 00002 ** Image Component Library (ICL) ** 00003 ** ** 00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld ** 00005 ** Neuroinformatics Group ** 00006 ** Website: www.iclcv.org and ** 00007 ** http://opensource.cit-ec.de/projects/icl ** 00008 ** ** 00009 ** File : ICLCore/src/ICLCore/OpenCV.h ** 00010 ** Module : ICLCore ** 00011 ** Authors: Christof Elbrechter, Christian Groszewski ** 00012 ** ** 00013 ** ** 00014 ** GNU LESSER GENERAL PUBLIC LICENSE ** 00015 ** This file may be used under the terms of the GNU Lesser General ** 00016 ** Public License version 3.0 as published by the ** 00017 ** ** 00018 ** Free Software Foundation and appearing in the file LICENSE.LGPL ** 00019 ** included in the packaging of this file. Please review the ** 00020 ** following information to ensure the license requirements will ** 00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt ** 00022 ** ** 00023 ** The development of this software was supported by the ** 00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. ** 00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche ** 00026 ** Forschungsgemeinschaft (DFG) in the context of the German ** 00027 ** Excellence Initiative. ** 00028 ** ** 00029 ********************************************************************/ 00030 00031 #pragma once 00032 00033 #include <opencv/cxcore.h> 00034 00035 #include <ICLCore/CCFunctions.h> 00036 #include <ICLCore/Img.h> 00037 #include <ICLCore/ImgBase.h> 00038 00039 #include <ICLUtils/Uncopyable.h> 00040 00041 namespace icl{ 00042 namespace core{ 00043 00045 enum DepthPreference{ 00046 PREFERE_SRC_DEPTH, 00047 PREFERE_DST_DEPTH 00048 }; 00049 00051 00056 ICLCore_API ImgBase *ipl_to_img(CvArr *src,ImgBase **dst=0,DepthPreference e=PREFERE_SRC_DEPTH) throw (utils::ICLException); 00057 00059 00064 ICLCore_API IplImage *img_to_ipl(const ImgBase *src, IplImage **dst = 0, DepthPreference e = PREFERE_SRC_DEPTH)throw (utils::ICLException); 00065 00067 00072 ICLCore_API CvMat* img_to_cvmat(const ImgBase *src, CvMat *dst = 0, int channel = 0) throw (utils::ICLException); 00073 00075 00081 ICLCore_API IplImage *img_to_ipl_shallow(ImgBase *src, IplImage *dst = 0)throw (utils::ICLException); 00082 00084 00089 ICLCore_API CvMat *img_to_cvmat_shallow(const ImgBase *src, CvMat *dst = 0) throw (utils::ICLException); 00090 00091 00092 00094 00095 template<class T> 00096 inline int icl_get_cv_mat_type() { 00097 throw utils::ICLException("icl_get_cv_mat_type: invalid type"); 00098 return 0; 00099 } 00100 00102 template<> inline int icl_get_cv_mat_type<int>() { return CV_32SC1; } 00103 00105 template<> inline int icl_get_cv_mat_type<float>() { return CV_32FC1; } 00108 00109 struct CvMatDelOp : public utils::DelOpBase{ 00110 static void delete_func(CvMat *m){ 00111 if(m) cvReleaseMat(&m); 00112 } 00113 }; 00114 00116 00118 template<class T> 00119 class CvMatWrapper : public utils::Uncopyable{ 00120 utils::Size size; 00121 utils::SmartPtrBase<CvMat,CvMatDelOp> m; 00122 00123 public: 00125 CvMatWrapper(int nrows=0, int ncols=0): 00126 size(ncols, nrows), 00127 m( nrows * ncols > 0 ? cvCreateMat(nrows, ncols, icl_get_cv_mat_type<T>()) : 0, true){} 00128 00129 CvMatWrapper(const utils::Size &size): 00130 size(size), 00131 m( size.getDim() > 0 ? cvCreateMat(size.height, size.width, icl_get_cv_mat_type<T>()) : 0, true){} 00132 00134 00135 CvMatWrapper(CvMat *other, bool takeOwnerShip=false): 00136 size(0,0), m(other, takeOwnerShip){ 00137 00138 if(other){ 00139 CvSize size = cvGetSize(other); 00140 this->size = utils::Size(size.width,size.height); 00141 } 00142 } 00143 00145 CvMat *get() { return m.get(); } 00146 00148 const CvMat *get() const { return m.get(); } 00149 00150 00152 00153 inline void setSize(const utils::Size &size){ 00154 if(size == this->size) return; 00155 this->size = size; 00156 if(size.getDim() > 0){ 00157 CvMat *m = cvCreateMat(size.height, size.width, icl_get_cv_mat_type<T>()); 00158 this->m = utils::SmartPtrBase<CvMat,CvMatDelOp>(m,true); 00159 }else{ 00160 this->m = utils::SmartPtrBase<CvMat,CvMatDelOp>(); 00161 } 00162 } 00163 00165 inline bool isNull() const { return m.get() == 0; } 00166 00168 inline const utils::Size &getSize() const { 00169 return size; 00170 } 00171 00173 T &operator()(int y, int x) { 00174 return CV_MAT_ELEM(*m,T,y,x); 00175 } 00176 00178 const T &operator()(int y, int x) const{ 00179 return CV_MAT_ELEM((const_cast<CvMat&>(*m)),T,y,x); 00180 } 00181 00182 }; 00183 00185 template<class T> 00186 inline std::ostream &operator<<(std::ostream &str, const CvMatWrapper<T> &m){ 00187 utils::Size s = m.getSize(); 00188 std::cout << "CvMatWrapper of a " << s.height << " x " << s.width << " matrix:" << "\n"; 00189 00190 for(int y=0;y<s.height;++y){ 00191 for(int x=0;x<s.width;++x){ 00192 str << m(y,x) << ((x == s.width-1) ? "\n" : " "); 00193 } 00194 } 00195 return str; 00196 } 00197 00198 } // namespace core 00199 } 00200 00201