GHough.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libimage - An image/template for new NICE libraries
  4. * See file License for license information.
  5. */
  6. /*****************************************************************************/
  7. /*! \file GHough.cpp
  8. \brief GHough class definitions
  9. */
  10. /*****************************************************************************/
  11. /*
  12. * $Author: bajramov $
  13. * $Date: 2009/05/28 11:36:30 $
  14. * $Revision: 1.2 $
  15. */
  16. /*****************************************************************************/
  17. #include "core/image/GHough.h"
  18. namespace NICE {
  19. // Constructors:
  20. // -------------
  21. GHough::GHough()
  22. {
  23. isInitialized = false;
  24. }
  25. GHough::GHough(IppiSize imageSize,IppiSize transformSize, NeighborType addMode)
  26. {
  27. isInitialized = false;
  28. initialize(imageSize, transformSize, addMode);
  29. }
  30. GHough::GHough(const GHough& ex)
  31. {
  32. *this=ex;
  33. }
  34. // Operators:
  35. // ----------
  36. GHough& GHough::operator=(const GHough& ex)
  37. {
  38. houghmap=ex.houghmap;
  39. imageSize=ex.imageSize;
  40. transformSize=ex.transformSize;
  41. isInitialized=ex.isInitialized;
  42. addMode=ex.addMode;
  43. return *this;
  44. }
  45. void GHough::initialize(IppiSize imageSize,IppiSize transformSize, NeighborType addMode)
  46. {
  47. this->imageSize = imageSize;
  48. this->transformSize = transformSize;
  49. this->addMode = addMode;
  50. //! Allocates memory for data
  51. houghmap.resize(transformSize.width,transformSize.height);
  52. clear();
  53. //! Indicates that initialization has been performed
  54. isInitialized = true;
  55. }
  56. void GHough::initialize(int width, int height, float scalefactor, NeighborType addMode)
  57. {
  58. imageSize.width=width;
  59. imageSize.height=height;
  60. transformSize.width = int(width*scalefactor);
  61. transformSize.height = int(height*scalefactor);
  62. this->addMode = addMode;
  63. //! Allocates memory for data
  64. houghmap.resize(transformSize.width,transformSize.height);
  65. clear();
  66. //! Indicates that initialization has been performed
  67. isInitialized = true;
  68. }
  69. template<class IMAGE>
  70. void GHough::initialize(const IMAGE &image, float scalefactor, NeighborType addMode)
  71. {
  72. imageSize.width=image.width();
  73. imageSize.height=image.height();
  74. transformSize.width = image.width()*scalefactor;
  75. transformSize.height = image.height()*scalefactor;
  76. this->addMode = addMode;
  77. //! Allocates memory for data
  78. houghmap.resize(transformSize.width,transformSize.height);
  79. clear();
  80. //! Indicates that initialization has been performed
  81. isInitialized = true;
  82. }
  83. void GHough::addPoint(float x, float y, float activation)
  84. {
  85. Coord c;
  86. c.x=(int)(x+0.5);
  87. c.y=(int)(y+0.5);
  88. addPoint(c,activation);
  89. }
  90. void GHough::addPoint(const Coord &pt, float activation)
  91. {
  92. if(!isInitialized) {
  93. fthrow(ImageException,"Hough map must be initialized first!");
  94. }
  95. int h,v;
  96. Coord transformLoc = transformPoint(pt);
  97. Coord loc;
  98. switch(addMode) {
  99. case EIGHT_NEIGHBORS:
  100. loc = Coord(transformLoc.x-1,transformLoc.y-1);
  101. for(h = -1; h < 2; h++, loc.y++) {
  102. loc.x=transformLoc.x-1;
  103. for(v = -1; v < 2; v++, loc.x++) {
  104. if(boundsOkay(loc,transformSize))
  105. houghmap(loc)+=activation;
  106. }
  107. }
  108. break;
  109. case FOUR_NEIGHBORS:
  110. loc = Coord(transformLoc.x-1,transformLoc.y);
  111. for(v = -1; v < 2; v++, loc.x++) {
  112. if(boundsOkay(loc,transformSize))
  113. houghmap(loc)+=activation;
  114. }
  115. loc = Coord(transformLoc.x,transformLoc.y-1);
  116. if(boundsOkay(loc,transformSize))
  117. houghmap(loc)+=activation;
  118. loc.y+=2;
  119. if(boundsOkay(loc,transformSize))
  120. houghmap(loc)+=activation;
  121. break;
  122. case SINGLE_BIN:
  123. if(boundsOkay(transformLoc,transformSize))
  124. houghmap(loc)+=activation;
  125. break;
  126. case HALF_OVERLAP:
  127. loc = transformLoc;
  128. for(h = 0; h < 2; h++, loc.y++) {
  129. loc.x=transformLoc.x;
  130. for(v = 0; v < 2; v++, loc.x++) {
  131. if(boundsOkay(loc,transformSize)) {
  132. houghmap(loc)+=activation;
  133. }
  134. }
  135. }
  136. }
  137. }
  138. //! Returns true if the point pt is within zero and the bounds
  139. bool GHough::boundsOkay(const Coord &pt,const IppiSize &bounds) const
  140. {
  141. if( pt.y >= 0 && pt.x >= 0 && pt.y < bounds.height && pt.x < bounds.width)
  142. return true;
  143. else {
  144. return false;
  145. }
  146. }
  147. Coord GHough::transformPoint(const Coord &pt) const
  148. {
  149. double xBinPerPix;
  150. double yBinPerPix;
  151. if(addMode == HALF_OVERLAP) {
  152. //gets top-left bin of 4 overlapping at transform pt if HALF_OVERLAP
  153. xBinPerPix = (transformSize.width + 1) / static_cast<double>(imageSize.width);
  154. yBinPerPix = (transformSize.height + 1) / static_cast<double>(imageSize.height);
  155. } else {
  156. xBinPerPix = transformSize.width / static_cast<double>(imageSize.width);
  157. yBinPerPix = transformSize.height / static_cast<double>(imageSize.height);
  158. }
  159. return Coord(static_cast<int>(round(pt.x * xBinPerPix - .5)), static_cast<int>(round(pt.y * yBinPerPix - .5)));
  160. }
  161. Coord GHough::nearestTransformPoint(const Coord &pt) const
  162. {
  163. if(addMode == HALF_OVERLAP) {
  164. //gets closest bin of 4 overlapping at transform pt if HALF_OVERLAP
  165. double xBinPerPix = (transformSize.width + 1) / static_cast<double>(imageSize.width);
  166. double yBinPerPix = (transformSize.height + 1) / static_cast<double>(imageSize.height);
  167. return Coord(static_cast<int>(round(pt.x * xBinPerPix)), static_cast<int>(round(pt.y * yBinPerPix)));
  168. } else {
  169. return transformPoint(pt);
  170. }
  171. }
  172. Coord GHough::invTransformPoint(const Coord &pt) const
  173. {
  174. double xPixPerBin;
  175. double yPixPerBin;
  176. if(addMode == HALF_OVERLAP) {
  177. xPixPerBin = static_cast<double>(imageSize.width) / (transformSize.width + 1);
  178. yPixPerBin = static_cast<double>(imageSize.height) / (transformSize.height + 1);
  179. return Coord(static_cast<int>(round(xPixPerBin * (pt.x + 1))), static_cast<int>(round(yPixPerBin * (pt.y + 1))));
  180. } else {
  181. xPixPerBin = static_cast<double>(imageSize.width) / transformSize.width;
  182. yPixPerBin = static_cast<double>(imageSize.height) / transformSize.height;
  183. return Coord(static_cast<int>(round(xPixPerBin * (pt.x + 0.5))), static_cast<int>(round(yPixPerBin * (pt.y + 0.5))));
  184. }
  185. }
  186. //! Finds the location with the maximum activation
  187. Coord GHough::getMaxLoc() const
  188. {
  189. if(!isInitialized)
  190. fthrow(ImageException,"Hough map must be initialized first!");
  191. return houghmap.maxIndex();
  192. }
  193. float GHough::getMax() const
  194. {
  195. if(!isInitialized)
  196. fthrow(ImageException,"Hough map must be initialized first!");
  197. return houghmap.max();
  198. }
  199. float GHough::getVal(int x, int y) const
  200. {
  201. if(!isInitialized) {
  202. fthrow(ImageException,"Hough map must be initialized first!");
  203. }
  204. return houghmap.getPixel(transformPoint(Coord(x,y)));
  205. }
  206. float GHough::getVal(const Coord &pt) const
  207. {
  208. if(!isInitialized) {
  209. fthrow(ImageException,"Hough map must be initialized first!");
  210. }
  211. return houghmap.getPixel(transformPoint(pt));
  212. }
  213. const FloatImage &GHough::getMap() const
  214. {
  215. if(!isInitialized) {
  216. fthrow(ImageException,"Hough map must be initialized first!");
  217. }
  218. return houghmap;
  219. }
  220. void GHough::clear()
  221. {
  222. houghmap=0;
  223. }
  224. } // namespace NICE
  225. /*****************************************************************************/
  226. /*
  227. * $Log: GHough.cpp,v $
  228. * Revision 1.2 2009/05/28 11:36:30 bajramov
  229. * renamed a few things for consistency
  230. *
  231. * Revision 1.1.1.1 2007/05/22 19:26:35 bajramov
  232. * limun2
  233. *
  234. * Revision 1.3 2007/02/07 19:09:42 zimmermann
  235. * * fixed documentation warnings
  236. *
  237. * Revision 1.2 2006/09/21 14:03:54 mattern
  238. * .
  239. *
  240. * Revision 1.1 2006/09/15 15:23:58 mattern
  241. * general hough transformation
  242. *
  243. */