morph.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*******************************************************************************
  2. License:
  3. This software and/or related materials was developed at the National Institute
  4. of Standards and Technology (NIST) by employees of the Federal Government
  5. in the course of their official duties. Pursuant to title 17 Section 105
  6. of the United States Code, this software is not subject to copyright
  7. protection and is in the public domain.
  8. This software and/or related materials have been determined to be not subject
  9. to the EAR (see Part 734.3 of the EAR for exact details) because it is
  10. a publicly available technology and software, and is freely distributed
  11. to any interested party with no licensing requirements. Therefore, it is
  12. permissible to distribute this software as a free download from the internet.
  13. Disclaimer:
  14. This software and/or related materials was developed to promote biometric
  15. standards and biometric technology testing for the Federal Government
  16. in accordance with the USA PATRIOT Act and the Enhanced Border Security
  17. and Visa Entry Reform Act. Specific hardware and software products identified
  18. in this software were used in order to perform the software development.
  19. In no case does such identification imply recommendation or endorsement
  20. by the National Institute of Standards and Technology, nor does it imply that
  21. the products and equipment identified are necessarily the best available
  22. for the purpose.
  23. This software and/or related materials are provided "AS-IS" without warranty
  24. of any kind including NO WARRANTY OF PERFORMANCE, MERCHANTABILITY,
  25. NO WARRANTY OF NON-INFRINGEMENT OF ANY 3RD PARTY INTELLECTUAL PROPERTY
  26. or FITNESS FOR A PARTICULAR PURPOSE or for any purpose whatsoever, for the
  27. licensed product, however used. In no event shall NIST be liable for any
  28. damages and/or costs, including but not limited to incidental or consequential
  29. damages of any kind, including economic damage or injury to property and lost
  30. profits, regardless of whether NIST shall be advised, have reason to know,
  31. or in fact shall know of the possibility.
  32. By using this software, you agree to bear all risk relating to quality,
  33. use and performance of the software and/or related materials. You agree
  34. to hold the Government harmless from any claim arising from your use
  35. of the software.
  36. *******************************************************************************/
  37. /***********************************************************************
  38. LIBRARY: LFS - NIST Latent Fingerprint System
  39. FILE: MORPH.C
  40. AUTHOR: Michael D. Garris
  41. DATE: 10/04/1999
  42. UPDATED: 10/26/1999 by MDG
  43. To avoid indisciminate erosion of pixels along
  44. the edge of the binary image.
  45. UPDATED: 03/16/2005 by MDG
  46. Contains general support image morphology routines required by
  47. the NIST Latent Fingerprint System (LFS).
  48. ***********************************************************************
  49. ROUTINES:
  50. erode_charimage_2()
  51. dilate_charimage_2()
  52. get_south8_2()
  53. get_north8_2()
  54. get_east8_2()
  55. get_west8_2()
  56. ***********************************************************************/
  57. #include "morph.h"
  58. #include <string.h>
  59. /*************************************************************************
  60. **************************************************************************
  61. #cat: erode_charimage_2 - Erodes an 8-bit image by setting true pixels to zero
  62. #cat: if any of their 4 neighbors is zero. Allocation of the
  63. #cat: output image is the responsibility of the caller. The
  64. #cat: input image remains unchanged. This routine will NOT
  65. #cat: erode pixels indiscriminately along the image border.
  66. Input:
  67. inp - input 8-bit image to be eroded
  68. iw - width (in pixels) of image
  69. ih - height (in pixels) of image
  70. Output:
  71. out - contains to the resulting eroded image
  72. **************************************************************************/
  73. void erode_charimage_2(unsigned char *inp, unsigned char *out,
  74. const int iw, const int ih)
  75. {
  76. int row, col;
  77. unsigned char *itr = inp, *otr = out;
  78. memcpy(out, inp, iw*ih);
  79. /* for true pixels. kill pixel if there is at least one false neighbor */
  80. for ( row = 0 ; row < ih ; row++ )
  81. for ( col = 0 ; col < iw ; col++ )
  82. {
  83. if (*itr) /* erode only operates on true pixels */
  84. {
  85. /* more efficient with C's left to right evaluation of */
  86. /* conjuctions. E N S functions not executed if W is false */
  87. if (!(get_west8_2 ((char *)itr, col , 1 ) &&
  88. get_east8_2 ((char *)itr, col, iw , 1 ) &&
  89. get_north8_2((char *)itr, row, iw , 1 ) &&
  90. get_south8_2((char *)itr, row, iw, ih, 1)))
  91. *otr = 0;
  92. }
  93. itr++ ; otr++;
  94. }
  95. }
  96. /*************************************************************************
  97. **************************************************************************
  98. #cat: dilate_charimage_2 - Dilates an 8-bit image by setting false pixels to
  99. #cat: one if any of their 4 neighbors is non-zero. Allocation
  100. #cat: of the output image is the responsibility of the caller.
  101. #cat: The input image remains unchanged.
  102. Input:
  103. inp - input 8-bit image to be dilated
  104. iw - width (in pixels) of image
  105. ih - height (in pixels) of image
  106. Output:
  107. out - contains to the resulting dilated image
  108. **************************************************************************/
  109. void dilate_charimage_2(unsigned char *inp, unsigned char *out,
  110. const int iw, const int ih)
  111. {
  112. int row, col;
  113. unsigned char *itr = inp, *otr = out;
  114. memcpy(out, inp, iw*ih);
  115. /* for all pixels. set pixel if there is at least one true neighbor */
  116. for ( row = 0 ; row < ih ; row++ )
  117. for ( col = 0 ; col < iw ; col++ )
  118. {
  119. if (!*itr) /* pixel is already true, neighbors irrelevant */
  120. {
  121. /* more efficient with C's left to right evaluation of */
  122. /* conjuctions. E N S functions not executed if W is false */
  123. if (get_west8_2 ((char *)itr, col , 0) ||
  124. get_east8_2 ((char *)itr, col, iw , 0) ||
  125. get_north8_2((char *)itr, row, iw , 0) ||
  126. get_south8_2((char *)itr, row, iw, ih, 0))
  127. *otr = 1;
  128. }
  129. itr++ ; otr++;
  130. }
  131. }
  132. /*************************************************************************
  133. **************************************************************************
  134. #cat: get_south8_2 - Returns the value of the 8-bit image pixel 1 below the
  135. #cat: current pixel if defined else it returns (char)0.
  136. Input:
  137. ptr - points to current pixel in image
  138. row - y-coord of current pixel
  139. iw - width (in pixels) of image
  140. ih - height (in pixels) of image
  141. failcode - return value if desired pixel does not exist
  142. Return Code:
  143. Zero - if neighboring pixel is undefined
  144. (outside of image boundaries)
  145. Pixel - otherwise, value of neighboring pixel
  146. **************************************************************************/
  147. char get_south8_2(char *ptr, const int row, const int iw, const int ih,
  148. const int failcode)
  149. {
  150. if (row >= ih-1) /* catch case where image is undefined southwards */
  151. return failcode; /* use plane geometry and return code. */
  152. return *(ptr+iw);
  153. }
  154. /*************************************************************************
  155. **************************************************************************
  156. #cat: get_north8_2 - Returns the value of the 8-bit image pixel 1 above the
  157. #cat: current pixel if defined else it returns (char)0.
  158. Input:
  159. ptr - points to current pixel in image
  160. row - y-coord of current pixel
  161. iw - width (in pixels) of image
  162. failcode - return value if desired pixel does not exist
  163. Return Code:
  164. Zero - if neighboring pixel is undefined
  165. (outside of image boundaries)
  166. Pixel - otherwise, value of neighboring pixel
  167. **************************************************************************/
  168. char get_north8_2(char *ptr, const int row, const int iw,
  169. const int failcode)
  170. {
  171. if (row < 1) /* catch case where image is undefined northwards */
  172. return failcode; /* use plane geometry and return code. */
  173. return *(ptr-iw);
  174. }
  175. /*************************************************************************
  176. **************************************************************************
  177. #cat: get_east8_2 - Returns the value of the 8-bit image pixel 1 right of the
  178. #cat: current pixel if defined else it returns (char)0.
  179. Input:
  180. ptr - points to current pixel in image
  181. col - x-coord of current pixel
  182. iw - width (in pixels) of image
  183. failcode - return value if desired pixel does not exist
  184. Return Code:
  185. Zero - if neighboring pixel is undefined
  186. (outside of image boundaries)
  187. Pixel - otherwise, value of neighboring pixel
  188. **************************************************************************/
  189. char get_east8_2(char *ptr, const int col, const int iw,
  190. const int failcode)
  191. {
  192. if (col >= iw-1) /* catch case where image is undefined eastwards */
  193. return failcode; /* use plane geometry and return code. */
  194. return *(ptr+ 1);
  195. }
  196. /*************************************************************************
  197. **************************************************************************
  198. #cat: get_west8_2 - Returns the value of the 8-bit image pixel 1 left of the
  199. #cat: current pixel if defined else it returns (char)0.
  200. Input:
  201. ptr - points to current pixel in image
  202. col - x-coord of current pixel
  203. failcode - return value if desired pixel does not exist
  204. Return Code:
  205. Zero - if neighboring pixel is undefined
  206. (outside of image boundaries)
  207. Pixel - otherwise, value of neighboring pixel
  208. **************************************************************************/
  209. char get_west8_2(char *ptr, const int col, const int failcode)
  210. {
  211. if (col < 1) /* catch case where image is undefined westwards */
  212. return failcode; /* use plane geometry and return code. */
  213. return *(ptr- 1);
  214. }