Morph.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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_MORPH_H
  7. #define LIMUN_MORPH_H
  8. #include <core/image/ippwrapper.h>
  9. #include <core/vector/VectorT.h>
  10. #include <core/vector/MatrixT.h>
  11. #include <core/image/ImageT.h>
  12. #include <core/image/ColorImageT.h>
  13. #include <core/image/Buffer.h>
  14. #include <core/image/Histogram.h>
  15. #include <core/image/Convert.h>
  16. namespace NICE {
  17. /**
  18. * @name morphological operations with a given structure Element
  19. * \{
  20. */
  21. /**
  22. * Filters Image \c src into the gray Image \c dst using a ranking operation.
  23. * with a given structre element \c structureElement and rank \c rank.
  24. * @param src source gray image
  25. * @param structureElement only pixels that correspond to nonzero mask values are taken into account
  26. * during operation
  27. * @param rank determines which rank is used for the new pixel value
  28. * @param dst Optional buffer to be used as target.<br>
  29. * Create a new Image if \c dst == NULL.<br>
  30. * If \c dst != NULL then size must be equal to \c src 's size!
  31. * @return Pointer to Image
  32. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal
  33. * or if \c rank is smaller 1 or bigger than the number of nonzero mask entries.
  34. */
  35. Image* rank(const Image& src, const CharMatrix& structureElement,
  36. const size_t& rank, Image* dst=NULL);
  37. /**
  38. * Filters Image \c src into the Image \c dst using a median filter for a given \c structureElement .
  39. * @param src source gray image
  40. * @param structureElement only pixels that correspond to nonzero mask values are taken into account
  41. * during operation
  42. * @param dst Optional buffer to be used as target.<br>
  43. * Create a new Image if \c dst == NULL.<br>
  44. * If \c dst != NULL then size must be equal to \c src 's size!
  45. * @return Pointer to Image
  46. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal
  47. * or if \c rank is smaller 1 or bigger than the number of nonzero mask entries.
  48. */
  49. Image* median(const Image& src, const CharMatrix& structureElement, Image* dst=NULL);
  50. /**
  51. * Filters Image \c src into the Image \c dst using a erosion with a given \c structureElement .<br>
  52. * The erosion of an image equals the computation of the minimum of the nonzero Elements of \c structureElement .
  53. * @param src source gray image
  54. * @param structureElement only pixels that correspond to nonzero mask values are taken into account
  55. * during operation
  56. * @param dst Optional buffer to be used as target.<br>
  57. * Create a new Image if \c dst == NULL.<br>
  58. * If \c dst != NULL then size must be equal to \c src 's size!
  59. * @return Pointer to Image
  60. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal
  61. * or if \c rank is smaller 1 or bigger than the number of nonzero mask values.
  62. */
  63. Image* erode(const Image& src, const CharMatrix& structureElement, Image* dst=NULL);
  64. /**
  65. * Filters Image \c src into the Image \c dst using a dilation with a given \c structureElement .<br>
  66. * The dilation of an image equals the computation of the maximum of the nonzero Elements of \c structureElement .
  67. * @param src source gray image
  68. * @param structureElement only pixels that correspond to nonzero mask values are taken into account
  69. * during operation
  70. * @param dst Optional buffer to be used as target.<br>
  71. * Create a new Image if \c dst == NULL.<br>
  72. * If \c dst != NULL then size must be equal to \c src 's size!
  73. * @return Pointer to Image
  74. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal
  75. * or if \c rank is smaller 1 or bigger than the number of nonzero mask values
  76. */
  77. Image* dilate(const Image& src, const CharMatrix& structureElement, Image* dst=NULL);
  78. /**
  79. * Filters Image \c src into Image \c dst using a closing operation with a given \c structureElement .<br>
  80. * In the first step an erosion of \c src is performed, afterwards a dilation.
  81. * @param src source gray image
  82. * @param structureElement only pixels that correspond to nonzero mask values are taken into account
  83. * during operation
  84. * @param dst Optional buffer to be used as target.<br>
  85. * Create a new Image if \c dst == NULL.<br>
  86. * If \c dst != NULL then size must be equal to \c src 's size!
  87. * @return Pointer to Image
  88. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal
  89. * or if \c rank is smaller 1 or bigger than the number of nonzero mask values
  90. */
  91. Image* opening(const Image& src, const CharMatrix& structureElement, Image* dst=NULL);
  92. /**
  93. * Filters Image \c src into Image \c dst using a closing operation with a given \c structureElement .<br>
  94. * In the first step a dilation \c src is performed, afterwards a erosion.
  95. * @param src source gray image
  96. * @param structureElement only pixels that correspond to nonzero mask values are taken into account
  97. * during operation
  98. * @param dst Optional buffer to be used as target.<br>
  99. * Create a new Image if \c dst == NULL.<br>
  100. * If \c dst != NULL then size must be equal to \c src 's size!
  101. * @return Pointer to Image
  102. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal
  103. * or if \c rank is smaller 1 or bigger than the number of nonzero mask values
  104. */
  105. Image* closing(const Image& src, const CharMatrix& structureElement, Image* dst=NULL);
  106. /**
  107. * \}
  108. * @name morphological operations with a quadratic structure element
  109. * \{
  110. */
  111. /**
  112. * Filters Image \c src into the Image \c dst using a ranking operation with a given \c rank
  113. * @param src source gray image
  114. * @param size Area of size (2*\c size +1)x(2*\c size +1) where to perform the ranking operation.
  115. * @param rank determines which rank is used for the new pixel value
  116. * @param dst Optional buffer to be used as target.<br>
  117. * Create a new Image if \c dst == NULL.<br>
  118. * If \c dst != NULL then size must be equal to \c src 's size!
  119. * @return Pointer to Image
  120. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal
  121. * or if \c rank is not in [1,2,...,(2*size+1)x(2*size+1)]
  122. */
  123. Image* rank(const Image& src, const uint& size, const uint& rank, Image* dst=NULL);
  124. /**
  125. * Erode Image \c src into the Image \c dst .<br>
  126. * The erosion of an image equals the computation of the minimum of an area.
  127. * @param src source gray image
  128. * @param dst Optional buffer to be used as target.<br>
  129. * Create a new Image if \c dst == NULL.<br>
  130. * If \c dst != NULL then size must be equal to \c src 's size!
  131. * @param size Area of size (2*\c size +1)x(2*\c size +1) where to perform the erosion.
  132. * @return Pointer to Image
  133. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  134. */
  135. Image* erode(const Image& src, Image* dst=NULL, const size_t& size=1);
  136. /**
  137. * Filters Image \c src into the Image \c dst using a median filter.
  138. * @param src source gray image
  139. * @param dst Optional buffer to be used as target.<br>
  140. * Create a new Image if \c dst == NULL.<br>
  141. * If \c dst != NULL then size must be equal to \c src 's size!
  142. * @param size Area of size (2*\c size +1)x(2*\c size +1) where to compute the median.
  143. * @return Pointer to Image
  144. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and dst are not equal.
  145. */
  146. Image* median(const Image& src, Image* dst=NULL, const size_t& size=1);
  147. /**
  148. * Dilate Image \c src into the Image \c dst .<br>
  149. * The dilation of an image equals the computation of the maximum of an area.
  150. * @param src source gray image
  151. * @param dst Optional buffer to be used as target.<br>
  152. * Create a new Image if \c dst == NULL.<br>
  153. * If \c dst != NULL then size must be equal to \c src 's size!
  154. * @param size (2*\c size +1) x (2*\c size +1) area where to perform the dilations
  155. * default is a 3x3 area
  156. * @return Pointer to Image
  157. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  158. */
  159. Image* dilate(const Image& src, Image* dst=NULL, const size_t& size=1);
  160. /**
  161. * Filters Image \c src into Image \c dst using a opening operation.<br>
  162. * In the first step an erosion of \c src is performed, afterwards a dilation.
  163. * @param src source gray image
  164. * @param dst Optional buffer to be used as target.<br>
  165. * Create a new Image if \c dst == NULL.<br>
  166. * If \c dst != NULL then size must be equal to \c src 's size!
  167. * @param size (2*\c size +1) x (2*\c size +1) area where to perform the opening operation (default 3x3)
  168. * @return Pointer to Image
  169. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  170. */
  171. Image* opening(const Image& src, Image* dst=NULL, const size_t& size=1);
  172. /**
  173. * Filters Image \c src into Image \c dst using a closing operation.<br>
  174. * In the first step a dilation of \c src is performed, afterwards a erosion.
  175. * @param src source gray image
  176. * @param dst Optional buffer to be used as target.<br>
  177. * Create a new Image if \c dst == NULL.<br>
  178. * If \c dst != NULL then size must be equal to \c src 's size!
  179. * @param size (2*\c size +1) x (2*\c size +1) where to perform the closing operation (default 3x3)
  180. * @return Pointer to Image
  181. * @throw ImageException will be thrown if \c dst != NULL and the size of \c src and \c dst is not equal.
  182. */
  183. Image* closing(const Image& src, Image* dst=NULL, const size_t& size=1);
  184. /**
  185. * \}
  186. * @name in-place morphological operations
  187. * \{
  188. */
  189. /**
  190. * Filters Image \c src using a 3x3 ranking operation.
  191. * @param src source gray image
  192. * @param rank Rank for the operation
  193. * @throw ImageException will be thrown if rank is not within [1,9]
  194. */
  195. void rankingIP(Image& src, const uint& rank);
  196. /**
  197. * Filters Image \c src using a 3x3 dilation.
  198. * @param src source gray image
  199. */
  200. void dilateIP(Image& src);
  201. /**
  202. * Filters Image \c src using a 3x3 median.
  203. * @param src source gray image
  204. */
  205. void medianIP(Image& src);
  206. /**
  207. * Filters Image \c src using a 3x3 erosion.
  208. * @param src source gray image
  209. */
  210. void erodeIP(Image& src);
  211. /**
  212. * Filters Image \c src using a 3x3 opening operation.
  213. * @param src source gray image
  214. */
  215. void openingIP(Image& src);
  216. /**
  217. * Filters Image \c src using a 3x3 closing operation.
  218. * @param src source gray image
  219. */
  220. void closingIP(Image& src);
  221. /**
  222. * \}
  223. * @name morphological matching functions
  224. * \{
  225. */
  226. /**
  227. * Performs a Hit-And-Miss operation on \c src by a given \c structureElement .<br>
  228. * The first step is a erosion of \c src with the nonzero elements of \c structureElement. <br>
  229. * In the second step the complement of \c src is eroded by the complement of \c structureElement.
  230. * @param src source gray image
  231. * @param structureElement structure element to be used for the hitAndMiss operation.<br>
  232. * Only pixels that correspond to nonzero mask values are taken
  233. * into account during operation.<br>
  234. * @param dst Optional buffer to be used as target.<br>
  235. * Create a new Image if \c dst == NULL.<br>
  236. * If \c dst != NULL then size must be equal to \c src 's size!
  237. * @return Pointer to Image
  238. */
  239. Image* hitAndMiss(const Image& src, const CharMatrix& structureElement, Image* dst=NULL);
  240. /**
  241. * \}
  242. * @name special median Implementation
  243. * \{
  244. */
  245. /**
  246. * Filters Image \c src by rows into the Image \c dst using medianfilter.
  247. * @param src source gray image
  248. * @param anchorx horizontal offset to the kernelposition if negativ use center position
  249. * @param anchory vertical offset to the kernelposition if negativ use center position
  250. * @param maskx horizontal size of the median mask
  251. * @param masky vertical size of the median mask
  252. * @param dst Optional buffer to be used as target.<br>
  253. * Create a new Image if dst == NULL.<br>
  254. * If dst != NULL then size must be equal to src's size!
  255. * @return Image
  256. */
  257. template<class P>
  258. ImageT<P>* median(const ImageT<P>& src, int anchorx=-1, int anchory=-1,
  259. uint maskx=3, uint masky=3, ImageT<P>* dst=NULL);
  260. /**
  261. * Filters ColorImage \c src by rows into the ColorImage \c dst using a 5x5 medianfilter.
  262. * @param src source gray image
  263. * @param mask switch between 3x3 masksize (false) and 5x5 masksize (true)
  264. * @param dst Optional buffer to be used as target.<br>
  265. * Create a new Image if dst == NULL.<br>
  266. * If dst != NULL then size must be equal to src's size!
  267. * @return Image
  268. */
  269. template<class P>
  270. ColorImageT<P>* median(const ColorImageT<P>& src,bool mask=false, ColorImageT<P>* dst=NULL);
  271. /**
  272. * \}
  273. */
  274. } // namespace
  275. //#ifdef __GNUC__
  276. #include <core/image/Morph.tcc>
  277. //#endif
  278. #endif //LIMUN_RANKING_H