Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
HoughLineDetector.h
Go to the documentation of this file.
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   : ICLCV/src/ICLCV/HoughLineDetector.h                    **
00010 ** Module : ICLCV                                                  **
00011 ** Authors: Christof Elbrechter                                    **
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 
00034 #include <cmath>
00035 #include <vector>
00036 #include <ICLMath/FixedVector.h>
00037 #include <ICLUtils/Range.h>
00038 #include <ICLUtils/Point.h>
00039 #include <ICLUtils/Point32f.h>
00040 #include <ICLMath/DynMatrix.h>
00041 #include <ICLMath/StraightLine2D.h>
00042 #include <ICLCore/Img.h>
00043 #include <ICLUtils/Uncopyable.h>
00044 
00045 namespace icl{
00046   namespace cv{
00047     
00108     class ICLCV_API HoughLineDetector : public utils::Uncopyable{
00109       public:
00110   
00112 
00121       HoughLineDetector(float dRho=0.1, float dR=10, 
00122                         const utils::Range32f &rRange=utils::Range32f(0,::sqrt(640*640+480*480)), 
00123                         float rInhibitionRange=10, 
00124                         float rhoInhibitionRange=0.3,
00125                         bool gaussianInhibition=true,
00126                         bool blurHoughSpace=true,
00127                         bool dilateEntries=true,
00128                         bool blurredSampling=false);
00129   
00131       void init(float dRho, float dR, const utils::Range32f &rRange, 
00132                 float rInhibitionRange, float rhoInhibitionRange,
00133                 bool gaussianInhibition=true,
00134                 bool blurHoughSpace=true,
00135                 bool dilateEntries=true,
00136                 bool blurredSampling=false);
00137       
00139       inline void add(const utils::Point &p){ 
00140         add_intern(p.x,p.y); 
00141       }
00142   
00144       inline void add(const std::vector<utils::Point> &ps){
00145         for(unsigned int i=0;i<ps.size();++i) add(ps[i]);
00146       }
00147       
00149       inline void add(const utils::Point32f &p){ 
00150         add_intern(p.x,p.y); 
00151       }
00152   
00154       inline void add(const std::vector<utils::Point32f> &ps){
00155         for(unsigned int i=0;i<ps.size();++i) add(ps[i]);
00156       }
00157   
00159       template<class T>
00160       inline void add(const math::FixedColVector<T,2> &p){ 
00161         add_intern(p[0],p[1]); 
00162       }    
00163       
00165       template<class T>
00166       inline void add(const std::vector<const math::FixedColVector<T,2> > &ps){
00167         for(unsigned int i=0;i<ps.size();++i) add(ps[i]);
00168       }
00169   
00171       void add(const core::Img8u &binaryImage);
00172   
00174       const core::Img32s &getImage() const { return m_image; }
00175   
00177       const core::Img32f &getInhibitionMap() const { return m_inhibitImage; }
00178   
00180       void clear();
00181       
00183       std::vector<math::StraightLine2D> getLines(int max);
00184   
00186 
00187       std::vector<math::StraightLine2D> getLines(int max, std::vector<float> &significances);
00188       
00189       private:
00190   
00192       inline float r(float rho, float x, float y) const{ 
00193         return x*cos(rho) + y*sin(rho);
00194       }
00196       inline int getX(float rho) const {
00197         return round(rho * m_mrho);
00198       }
00200       inline int getY(float r) const {
00201         return round(m_mr * r + m_br); 
00202       }
00204       inline float getRho(int x) const{
00205         return x/m_mrho;
00206       }
00208       inline float getR(int y) const{
00209         return (y-m_br)/m_mr;
00210       }
00212       inline void incLut(float rho, float r){
00213         int x = getX(rho);
00214         int y = getY(r);
00215         if(y >= 0 && y < m_h){
00216           m_lut(x,y)++;
00217         }
00218       }
00220       inline void incLutBlurred(float rho, float r){
00221         int x = getX(rho);
00222         int y = getY(r);
00223         if(y >= 0 && y < m_h){
00224           if(y>0) m_lut(x,y-1)++;
00225           m_lut(x,y)+=2;
00226           if(y<m_w-1) m_lut(x,y+1)++;
00227         }
00228       }
00230       inline int &cyclicLUT(int x, int y){
00231         static int _null = 0;
00232         if(y<0||y>=m_h) return _null;
00233         int dx = x<0 ? m_w : x>=m_w ? -m_w : 0;
00234         if(!m_image.getImageRect().contains(x+dx,y)){
00235           ERROR_LOG("tryed to access " << x+dx << "," << y);
00236           return _null;
00237         }
00238         return m_lut(x+dx,y);
00239       }
00240     
00242       void apply_inhibition(const utils::Point &p);
00243   
00245       void add_intern(float x, float y);
00246   
00248       void add_intern2(float x, float y);
00249   
00251       void blur_hough_space_if_necessary();
00252       
00253   
00254       float m_dRho;
00255       utils::Range32f m_rRange;
00256       int m_w;
00257       int m_h;
00258   
00259       float m_mr;
00260       float m_br;
00261       float m_mrho;
00262   
00263       float m_rInhib;
00264       float m_rhoInhib;
00265   
00266       bool m_gaussianInhibition;
00267       bool m_blurHoughSpace;
00268       bool m_dilateEntries;
00269       bool m_blurredSampling;
00270       
00271       core::Channel32s m_lut;
00272       core::Img32s m_image;
00273       core::Img32f m_inhibitImage;
00274     };
00275     
00276   } // namespace cv
00277 }
00278 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines