Histogram.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libimage - An image library
  4. * See file License for license information.
  5. */
  6. #ifndef LIMUN_HISTOGRAM_H
  7. #define LIMUN_HISTOGRAM_H
  8. #include "core/image/ippwrapper.h"
  9. #include "core/image/RectT.h"
  10. #include "core/image/ImageT.h"
  11. #include "core/image/ColorImageT.h"
  12. #include "core/vector/VectorT.h"
  13. namespace NICE {
  14. /**
  15. * A class for histograms
  16. */
  17. class Histogram {
  18. public:
  19. //! STL-like typedef for type of elements
  20. typedef Ipp32s value_type;
  21. //! STL-like typedef for element reference
  22. typedef value_type& reference;
  23. //! STL-like typedef for const element reference
  24. typedef const value_type& const_reference;
  25. //! STL-like typedef for iterator
  26. typedef value_type* iterator;
  27. //! STL-like typedef for const iterator
  28. typedef const value_type* const_iterator;
  29. /**
  30. * @name Constructors, factory methods and destructor
  31. * \{
  32. */
  33. /**
  34. * \}
  35. * @name Histogram
  36. * \{
  37. */
  38. /**
  39. * Default constructor.
  40. */
  41. Histogram();
  42. /**
  43. * Create a empty histogram with \c bins bins.
  44. * @param bins number of histogram bins
  45. */
  46. Histogram(const Ipp32s& bins);
  47. /**
  48. * Create a empty histogram with values in the area of \c min to \c max with \c bins equal bins.
  49. * @param min lower boundary (included)
  50. * @param max upper boundary (not included)
  51. * @param bins number of histogram bins (if negativ, (\c max - \c min) bins will be used)
  52. */
  53. Histogram(const Ipp32s& min, const Ipp32s& max, const Ipp32s& bins=-1);
  54. /**
  55. * Calculate a histogram from the source gray image \c src with \c bins equal bins.<br>
  56. * Only values in the area of \c min to \c max are taken into account during calculation.
  57. * @param src source image
  58. * @param min lower boundary (included)
  59. * @param max upper boundary (not included)
  60. * @param bins number of histogram bins (if negativ, (\c max - \c min) bins will be used)
  61. */
  62. Histogram(const Image& src, const Ipp32s& min, const Ipp32s& max, const Ipp32s& bins=-1);
  63. /**
  64. * Calculate a histogram from target \c rect of the source gray image \c src with \c bins equal bins.<br>
  65. * Only values in the area of \c min to \c max are taken into account during calculation.
  66. * @param src source image
  67. * @param min lower boundary (included)
  68. * @param max upper boundary (not included)
  69. * @param rect target rect
  70. * @param bins number of histogram bins (if negativ, (\c max - \c min) bins will be used)
  71. */
  72. Histogram(const Image& src, const Ipp32s& min, const Ipp32s& max,
  73. const Rect& rect, const Ipp32s& bins=-1);
  74. /**
  75. * Calculate a histogram from the source color image \c src with \c bins equal bins.<br>
  76. * The resulting histogram is of size = 3*\c bins .<br>
  77. * Only values in the area of \c min to \c max are taken into account during calculation.
  78. * @param src source image
  79. * @param min lower boundary (included)
  80. * @param max upper boundary (not included)
  81. * @param bins number of histogram bins (if negativ, (\c max - \c min) bins will be used)
  82. */
  83. Histogram(const ColorImage& src, const Ipp32s& min, const Ipp32s& max, const Ipp32s& bins=-1);
  84. /**
  85. * Destructor.
  86. */
  87. virtual ~Histogram() {
  88. delete _data;
  89. }
  90. /**
  91. * \}
  92. * @name Combined Color Histogram
  93. * \{
  94. */
  95. /**
  96. * Calculate a combined histogram for the source color image \c src with \c bins equal bins per colorchannel.<br>
  97. * The resulting histogram is of size (\c bins^2 + \c bins ) if \c noLum == true or<br>
  98. * (\c bins^3 + \c bins^2 + \c bins) if \c noLum == false
  99. * @param src source color image
  100. * @param min lower boundary (included)
  101. * @param max upper boundary (not included)
  102. * @param bins number of histogram bins (if negativ, (\c max - \c min) bins will be used)
  103. * @param noLum specifies if the first color channel will be ignored, set to \b true to ignore
  104. */
  105. Histogram(const ColorImage& src, const Ipp32s& min, const Ipp32s& max,
  106. const Ipp32s& bins, const bool noLum);
  107. /**
  108. * If a combined binned histogram was created, reproject will retrieve the index
  109. * of the color (\c c1 , \c c2 , \c c3 ) .<br>
  110. * If the histogram was created with \c noLum == \b true, value \c c1 will be ignored.
  111. * @param c1 value for the first color channel
  112. * @param c2 value for the second color channel
  113. * @param c3 value for the third color channel
  114. * @return Ipp32u
  115. * @throws ImageException if no combined histogram is used
  116. */
  117. Ipp32u reproject(const Ipp8u& c1, const Ipp8u& c2, const Ipp8u& c3);
  118. /**
  119. * \}
  120. * @name Access Data
  121. * \{
  122. */
  123. /**
  124. * Access to elements.
  125. * @param i Index
  126. * @return reference
  127. */
  128. inline reference operator[](const ptrdiff_t i) {
  129. return (*_data)[i];
  130. }
  131. /**
  132. * Read access to elements.
  133. * @param i Index
  134. * @return const_reference
  135. */
  136. inline const_reference operator[](const ptrdiff_t i) const {
  137. return (*_data)[i];
  138. }
  139. /**
  140. * Access to range checked elements.
  141. * @param i Index
  142. * @return reference
  143. */
  144. inline reference operator()(const ptrdiff_t i) {
  145. if(isWithin(i))
  146. return (*_data)[i];
  147. else
  148. fthrow(ImageException, "Index out of range.");
  149. }
  150. /**
  151. * Range checked access to histogram bins.
  152. * @param i Index
  153. * @return const_reference
  154. * @throws ImageException if \c i is out of range (\b minIndex() to \b maxIndex() ).
  155. */
  156. inline const_reference operator()(const ptrdiff_t i) const {
  157. if(isWithin(i))
  158. return (*_data)[i];
  159. else
  160. fthrow(ImageException, "Index out of range.");
  161. }
  162. /**
  163. * Get an iterator pointing to the first element.
  164. * @return iterator
  165. */
  166. inline iterator begin() {
  167. return _data->begin();
  168. }
  169. /**
  170. * Get a const_iterator pointing to the first element.
  171. * @return const_iterator
  172. */
  173. inline const_iterator begin() const {
  174. return _data->begin();
  175. }
  176. /**
  177. * Get an iterator pointing beyond the last element.
  178. * @return iterator
  179. */
  180. inline iterator end() {
  181. return _data->end();
  182. }
  183. /**
  184. * Get an iterator pointing beyond the last element.
  185. * @return const_iterator
  186. */
  187. inline const_iterator end() const {
  188. return _data->end();
  189. }
  190. /**
  191. * Get a data pointer to the internal memory.
  192. * @return iterator
  193. */
  194. inline iterator getDataPointer() {
  195. return _data->getDataPointer();
  196. }
  197. /**
  198. * Get a const pointer to the internal memory.
  199. */
  200. inline const_iterator getDataPointer() const {
  201. return _data->getDataPointer();
  202. }
  203. /**
  204. * Assign \c v to all bins of the histogram.
  205. * @param v value to assign
  206. */
  207. inline void set(const value_type v) {
  208. *_data = v;
  209. }
  210. /**
  211. * Increase bin \c index by 1.
  212. */
  213. inline void increaseBin(const value_type index) {
  214. (*_data)[index]++;
  215. }
  216. /**
  217. * \}
  218. * @name Operators
  219. * \{
  220. */
  221. /**
  222. * Copy data from \c v to \c this.
  223. * @param v new data
  224. * @return \c *this
  225. */
  226. inline Histogram& operator=(const Histogram& v);
  227. /**
  228. * Set all bins to value \c element
  229. * @param element new value of all bins
  230. */
  231. inline Histogram& operator=(const_reference element);
  232. /**
  233. * \}
  234. * @name Tools
  235. * \{
  236. */
  237. /**
  238. * Check if the value is within the border \b minBorder() to \b maxBorder() .
  239. * @return true if \c val is within the border from \b minBorder() to \b maxBorder() .
  240. */
  241. bool isWithin(const ptrdiff_t i) const {
  242. return i>=_min && i<_max;
  243. }
  244. /**
  245. * Calculate the sum of all bins.
  246. * @return value_type
  247. */
  248. value_type sum() const;
  249. /**
  250. * \}
  251. * @name Basic Properties
  252. * \{
  253. */
  254. /**
  255. * Return the maximum value of all bins.
  256. * @return value_type
  257. */
  258. value_type min() const {
  259. return _data->Min();
  260. }
  261. /**
  262. * Return the minimum value of all bins.
  263. * @return value_type
  264. */
  265. value_type max() const {
  266. return _data->Max();
  267. }
  268. /**
  269. * Return the index of the minimum value of all bins.
  270. * @return Ipp32u
  271. */
  272. Ipp32u minIndex() const;
  273. /**
  274. * Return the index of the maximum value of all bins.
  275. * @return Ipp32u
  276. */
  277. Ipp32u maxIndex() const;
  278. /**
  279. * Return the number of bins per channel
  280. * @return Ipp32u
  281. */
  282. Ipp32u bins() const {
  283. return _nobins;
  284. }
  285. /**
  286. * Return the number of color channels the histogram was created from
  287. * @return Ipp32u
  288. */
  289. Ipp32u channels() const {
  290. return _channels;
  291. }
  292. /**
  293. * Return the minimum border of the histogram value range.
  294. * @return Ipp32s
  295. */
  296. Ipp32s minBorder() const {
  297. return _min;
  298. }
  299. /**
  300. * Return the maximum border of the histogram value range.
  301. * @return Ipp32s
  302. */
  303. Ipp32s maxBorder() const {
  304. return _max;
  305. }
  306. /**
  307. * Return the size of the histogram.
  308. * @return Ipp32s
  309. */
  310. Ipp32u size() const {
  311. return _data->size();
  312. }
  313. /**
  314. * \}
  315. * @name Transformations
  316. * \{
  317. */
  318. /**
  319. * Calculate a cumulative histogram.
  320. * @return Pointer to IntVector
  321. */
  322. IntVector* cumulative();
  323. /**
  324. * Calculate a normalized histogram.
  325. * @return Pointer to FloatVector
  326. */
  327. FloatVector* normalized();
  328. /**
  329. * \}
  330. * @name IO Functions
  331. * \{
  332. */
  333. /**
  334. * Read a histogram from a file \c filename .
  335. * @param filename source file where to read from
  336. * @return true if the histogram could be read, false if an error occoured
  337. */
  338. bool read(const std::string &filename);
  339. /**
  340. * Write a histogram to a file \c filename .
  341. * @param filename source file where to write the histogram
  342. * @return true if the histogram could be writed, false if an error occoured
  343. */
  344. bool write(const std::string &filename);
  345. /**
  346. * \}
  347. */
  348. private:
  349. VectorT<value_type>* _data;
  350. Ipp32u _channels, _nobins;
  351. Ipp32s _min, _max;
  352. // needed for faster reprojection
  353. Ipp32u _b[3];
  354. Ipp32u _diff;
  355. // little helperfunction to init b
  356. void init_b() {
  357. _b[2] = _nobins;
  358. _b[1] = _nobins*_b[2];
  359. _b[0] = _nobins*_b[1];
  360. }
  361. };
  362. inline Histogram& Histogram::operator=(const Histogram& v) {
  363. _min = v.min();
  364. _max = v.max();
  365. _nobins = v.bins();
  366. _channels = v.channels();
  367. _diff = _max-_min;
  368. // delete old data
  369. if(_data==NULL)
  370. _data = new VectorT<value_type>(v.size());
  371. else
  372. if(_data->size()!=v.size())
  373. _data->resize(v.size());
  374. #ifdef NICE_USELIB_IPP
  375. ippsCopy(v.getDataPointer(), _data->getDataPointer(), _data->size());
  376. #else
  377. memcpy(_data->getDataPointer(), v.getDataPointer(), _data->size()*sizeof(value_type));
  378. #endif
  379. return *this;
  380. }
  381. inline Histogram& Histogram::operator=(const_reference element) {
  382. *_data = element;
  383. return *this;
  384. }
  385. /**
  386. * @name Operators
  387. * \{
  388. */
  389. /**
  390. * Add histograms \c a and \c b .
  391. * @return Histogram
  392. * @throws ImageException will be thrown if the size of \c a and \c b are not equal.
  393. */
  394. inline const Histogram operator+(const Histogram& a, const Histogram& b)
  395. {
  396. if(a.size()!=b.size())
  397. fthrow(ImageException,"Histogram size is not equal.");
  398. Histogram result(a.minBorder(), a.maxBorder(), a.bins());
  399. Histogram::iterator r = result.begin();
  400. Histogram::const_iterator j = b.begin();
  401. for(Histogram::const_iterator i=a.begin(); i!=a.end(); ++i,++j,++r)
  402. *r = *i+*j;
  403. return result;
  404. }
  405. /**
  406. * Add histogram \c a to \c this and overwrite \c this .
  407. * @return Histogram
  408. * @throws ImageException will be thrown if the size of \c a and \c b are not equal.
  409. */
  410. inline Histogram& operator+=(Histogram& a, const Histogram& b)
  411. {
  412. if(a.size()!=b.size())
  413. fthrow(ImageException,"Histogram size is not equal.");
  414. Histogram::const_iterator j = b.begin();
  415. for(Histogram::iterator i=a.begin(); i!=a.end(); ++i,++j)
  416. *i += *j;
  417. return a;
  418. }
  419. /**
  420. * Compare \c a with \c b .
  421. * @param a source histogram
  422. * @param b histogram to compare with
  423. * @return \b true if \c a and \c b are equal, \b false if not or if the size of \c a and \c b are not equal.
  424. */
  425. inline bool operator==(const Histogram& a, const Histogram& b)
  426. {
  427. if(a.size()!=b.size())
  428. return false;
  429. Histogram::const_iterator j = b.begin();
  430. for(Histogram::const_iterator i=a.begin(); i!=a.end(); ++i,++j)
  431. if(*i != *j)
  432. return false;
  433. return true;
  434. }
  435. /**
  436. * Compare Histogram \c a with \c b .
  437. * @param a source histogram
  438. * @param b histogram to compare with
  439. * @return true if \c a and \c b are not equal or if the size of a and b is not equal
  440. */
  441. inline bool operator!=(const Histogram& a, const Histogram& b)
  442. {
  443. if(a.size()!=b.size())
  444. return true;
  445. Histogram::const_iterator j = b.begin();
  446. for(Histogram::const_iterator i=a.begin(); i!=a.end(); ++i,++j)
  447. if(*i == *j)
  448. return false;
  449. return true;
  450. }
  451. /**
  452. * \}
  453. */
  454. } // namespace
  455. #endif // LIMUN_HISTOGRAM_H