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.GPL  **
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 HoughLineDetector : public utils::Uncopyable{
00109       public:
00110   
00112 
00121       HoughLineDetector(float dRho, float dR, const utils::Range32f rRange, 
00122                         float rInhibitionRange, float rhoInhibitionRange,
00123                         bool gaussianInhibition=true,
00124                         bool blurHoughSpace=true,
00125                         bool dilateEntries=true,
00126                         bool blurredSampling=false);
00127   
00129       inline void add(const utils::Point &p){ 
00130         add_intern(p.x,p.y); 
00131       }
00132   
00134       inline void add(const std::vector<utils::Point> &ps){
00135         for(unsigned int i=0;i<ps.size();++i) add(ps[i]);
00136       }
00137       
00139       inline void add(const utils::Point32f &p){ 
00140         add_intern(p.x,p.y); 
00141       }
00142   
00144       inline void add(const std::vector<utils::Point32f> &ps){
00145         for(unsigned int i=0;i<ps.size();++i) add(ps[i]);
00146       }
00147   
00149       template<class T>
00150       inline void add(const math::FixedColVector<T,2> &p){ 
00151         add_intern(p[0],p[1]); 
00152       }    
00153       
00155       template<class T>
00156       inline void add(const std::vector<const math::FixedColVector<T,2> > &ps){
00157         for(unsigned int i=0;i<ps.size();++i) add(ps[i]);
00158       }
00159   
00160   
00162       const core::Img32s &getImage() const { return m_image; }
00163   
00165       const core::Img32f &getInhibitionMap() const { return m_inhibitImage; }
00166   
00168       void clear();
00169       
00171       std::vector<math::StraightLine2D> getLines(int max);
00172   
00174 
00175       std::vector<math::StraightLine2D> getLines(int max, std::vector<float> &significances);
00176       
00177       private:
00178   
00180       inline float r(float rho, float x, float y) const{ 
00181         return x*cos(rho) + y*sin(rho);
00182       }
00184       inline int getX(float rho) const {
00185         return round(rho * m_mrho);
00186       }
00188       inline int getY(float r) const {
00189         return round(m_mr * r + m_br); 
00190       }
00192       inline float getRho(int x) const{
00193         return x/m_mrho;
00194       }
00196       inline float getR(int y) const{
00197         return (y-m_br)/m_mr;
00198       }
00200       inline void incLut(float rho, float r){
00201         int x = getX(rho);
00202         int y = getY(r);
00203         if(y >= 0 && y < m_h){
00204           m_lut(x,y)++;
00205         }
00206       }
00208       inline void incLutBlurred(float rho, float r){
00209         int x = getX(rho);
00210         int y = getY(r);
00211         if(y >= 0 && y < m_h){
00212           if(y>0) m_lut(x,y-1)++;
00213           m_lut(x,y)+=2;
00214           if(y<m_w-1) m_lut(x,y+1)++;
00215         }
00216       }
00218       inline int &cyclicLUT(int x, int y){
00219         static int _null = 0;
00220         if(y<0||y>=m_h) return _null;
00221         int dx = x<0 ? m_w : x>=m_w ? -m_w : 0;
00222         if(!m_image.getImageRect().contains(x+dx,y)){
00223           ERROR_LOG("tryed to access " << x+dx << "," << y);
00224           return _null;
00225         }
00226         return m_lut(x+dx,y);
00227       }
00228     
00230       void apply_inhibition(const utils::Point &p);
00231   
00233       void add_intern(float x, float y);
00234   
00236       void add_intern2(float x, float y);
00237   
00239       void blur_hough_space_if_necessary();
00240       
00241   
00242       float m_dRho;
00243       utils::Range32f m_rRange;
00244       int m_w;
00245       int m_h;
00246   
00247       float m_mr;
00248       float m_br;
00249       float m_mrho;
00250   
00251       float m_rInhib;
00252       float m_rhoInhib;
00253   
00254       bool m_gaussianInhibition;
00255       bool m_blurHoughSpace;
00256       bool m_dilateEntries;
00257       bool m_blurredSampling;
00258       
00259       core::Channel32s m_lut;
00260       core::Img32s m_image;
00261       core::Img32f m_inhibitImage;
00262     };
00263     
00264   } // namespace cv
00265 }
00266 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines