_imagingmorph.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * The Python Imaging Library
  3. *
  4. * A binary morphology add-on for the Python Imaging Library
  5. *
  6. * History:
  7. * 2014-06-04 Initial version.
  8. *
  9. * Copyright (c) 2014 Dov Grobgeld <dov.grobgeld@gmail.com>
  10. *
  11. * See the README file for information on usage and redistribution.
  12. */
  13. #include "Python.h"
  14. #include "libImaging/Imaging.h"
  15. #define LUT_SIZE (1 << 9)
  16. /* Apply a morphologic LUT to a binary image. Outputs a
  17. a new binary image.
  18. Expected parameters:
  19. 1. a LUT - a 512 byte size lookup table.
  20. 2. an input Imaging image id.
  21. 3. an output Imaging image id
  22. Returns number of changed pixels.
  23. */
  24. static PyObject *
  25. apply(PyObject *self, PyObject *args) {
  26. const char *lut;
  27. PyObject *py_lut;
  28. Py_ssize_t lut_len, i0, i1;
  29. Imaging imgin, imgout;
  30. int width, height;
  31. int row_idx, col_idx;
  32. UINT8 **inrows, **outrows;
  33. int num_changed_pixels = 0;
  34. if (!PyArg_ParseTuple(args, "Onn", &py_lut, &i0, &i1)) {
  35. PyErr_SetString(PyExc_RuntimeError, "Argument parsing problem");
  36. return NULL;
  37. }
  38. if (!PyBytes_Check(py_lut)) {
  39. PyErr_SetString(PyExc_RuntimeError, "The morphology LUT is not a bytes object");
  40. return NULL;
  41. }
  42. lut_len = PyBytes_Size(py_lut);
  43. if (lut_len < LUT_SIZE) {
  44. PyErr_SetString(PyExc_RuntimeError, "The morphology LUT has the wrong size");
  45. return NULL;
  46. }
  47. lut = PyBytes_AsString(py_lut);
  48. imgin = (Imaging)i0;
  49. imgout = (Imaging)i1;
  50. width = imgin->xsize;
  51. height = imgin->ysize;
  52. if (imgin->type != IMAGING_TYPE_UINT8 || imgin->bands != 1) {
  53. PyErr_SetString(PyExc_RuntimeError, "Unsupported image type");
  54. return NULL;
  55. }
  56. if (imgout->type != IMAGING_TYPE_UINT8 || imgout->bands != 1) {
  57. PyErr_SetString(PyExc_RuntimeError, "Unsupported image type");
  58. return NULL;
  59. }
  60. inrows = imgin->image8;
  61. outrows = imgout->image8;
  62. for (row_idx = 0; row_idx < height; row_idx++) {
  63. UINT8 *outrow = outrows[row_idx];
  64. UINT8 *inrow = inrows[row_idx];
  65. UINT8 *prow, *nrow; /* Previous and next row */
  66. /* zero boundary conditions. TBD support other modes */
  67. outrow[0] = outrow[width - 1] = 0;
  68. if (row_idx == 0 || row_idx == height - 1) {
  69. for (col_idx = 0; col_idx < width; col_idx++) {
  70. outrow[col_idx] = 0;
  71. }
  72. continue;
  73. }
  74. prow = inrows[row_idx - 1];
  75. nrow = inrows[row_idx + 1];
  76. for (col_idx = 1; col_idx < width - 1; col_idx++) {
  77. int cim = col_idx - 1;
  78. int cip = col_idx + 1;
  79. unsigned char b0 = prow[cim] & 1;
  80. unsigned char b1 = prow[col_idx] & 1;
  81. unsigned char b2 = prow[cip] & 1;
  82. unsigned char b3 = inrow[cim] & 1;
  83. unsigned char b4 = inrow[col_idx] & 1;
  84. unsigned char b5 = inrow[cip] & 1;
  85. unsigned char b6 = nrow[cim] & 1;
  86. unsigned char b7 = nrow[col_idx] & 1;
  87. unsigned char b8 = nrow[cip] & 1;
  88. int lut_idx =
  89. (b0 | (b1 << 1) | (b2 << 2) | (b3 << 3) | (b4 << 4) | (b5 << 5) |
  90. (b6 << 6) | (b7 << 7) | (b8 << 8));
  91. outrow[col_idx] = 255 * (lut[lut_idx] & 1);
  92. num_changed_pixels += ((b4 & 1) != (outrow[col_idx] & 1));
  93. }
  94. }
  95. return Py_BuildValue("i", num_changed_pixels);
  96. }
  97. /* Match a morphologic LUT to a binary image and return a list
  98. of the coordinates of all matching pixels.
  99. Expected parameters:
  100. 1. a LUT - a 512 byte size lookup table.
  101. 2. an input Imaging image id.
  102. Returns list of matching pixels.
  103. */
  104. static PyObject *
  105. match(PyObject *self, PyObject *args) {
  106. const char *lut;
  107. PyObject *py_lut;
  108. Py_ssize_t lut_len, i0;
  109. Imaging imgin;
  110. int width, height;
  111. int row_idx, col_idx;
  112. UINT8 **inrows;
  113. PyObject *ret = PyList_New(0);
  114. if (ret == NULL) {
  115. return NULL;
  116. }
  117. if (!PyArg_ParseTuple(args, "On", &py_lut, &i0)) {
  118. Py_DECREF(ret);
  119. PyErr_SetString(PyExc_RuntimeError, "Argument parsing problem");
  120. return NULL;
  121. }
  122. if (!PyBytes_Check(py_lut)) {
  123. Py_DECREF(ret);
  124. PyErr_SetString(PyExc_RuntimeError, "The morphology LUT is not a bytes object");
  125. return NULL;
  126. }
  127. lut_len = PyBytes_Size(py_lut);
  128. if (lut_len < LUT_SIZE) {
  129. Py_DECREF(ret);
  130. PyErr_SetString(PyExc_RuntimeError, "The morphology LUT has the wrong size");
  131. return NULL;
  132. }
  133. lut = PyBytes_AsString(py_lut);
  134. imgin = (Imaging)i0;
  135. if (imgin->type != IMAGING_TYPE_UINT8 || imgin->bands != 1) {
  136. Py_DECREF(ret);
  137. PyErr_SetString(PyExc_RuntimeError, "Unsupported image type");
  138. return NULL;
  139. }
  140. inrows = imgin->image8;
  141. width = imgin->xsize;
  142. height = imgin->ysize;
  143. for (row_idx = 1; row_idx < height - 1; row_idx++) {
  144. UINT8 *inrow = inrows[row_idx];
  145. UINT8 *prow, *nrow;
  146. prow = inrows[row_idx - 1];
  147. nrow = inrows[row_idx + 1];
  148. for (col_idx = 1; col_idx < width - 1; col_idx++) {
  149. int cim = col_idx - 1;
  150. int cip = col_idx + 1;
  151. unsigned char b0 = prow[cim] & 1;
  152. unsigned char b1 = prow[col_idx] & 1;
  153. unsigned char b2 = prow[cip] & 1;
  154. unsigned char b3 = inrow[cim] & 1;
  155. unsigned char b4 = inrow[col_idx] & 1;
  156. unsigned char b5 = inrow[cip] & 1;
  157. unsigned char b6 = nrow[cim] & 1;
  158. unsigned char b7 = nrow[col_idx] & 1;
  159. unsigned char b8 = nrow[cip] & 1;
  160. int lut_idx =
  161. (b0 | (b1 << 1) | (b2 << 2) | (b3 << 3) | (b4 << 4) | (b5 << 5) |
  162. (b6 << 6) | (b7 << 7) | (b8 << 8));
  163. if (lut[lut_idx]) {
  164. PyObject *coordObj = Py_BuildValue("(nn)", col_idx, row_idx);
  165. PyList_Append(ret, coordObj);
  166. Py_XDECREF(coordObj);
  167. }
  168. }
  169. }
  170. return ret;
  171. }
  172. /* Return a list of the coordinates of all turned on pixels in an image.
  173. May be used to extract features after a sequence of MorphOps were applied.
  174. This is faster than match as only 1x1 lookup is made.
  175. */
  176. static PyObject *
  177. get_on_pixels(PyObject *self, PyObject *args) {
  178. Py_ssize_t i0;
  179. Imaging img;
  180. UINT8 **rows;
  181. int row_idx, col_idx;
  182. int width, height;
  183. PyObject *ret = PyList_New(0);
  184. if (ret == NULL) {
  185. return NULL;
  186. }
  187. if (!PyArg_ParseTuple(args, "n", &i0)) {
  188. Py_DECREF(ret);
  189. PyErr_SetString(PyExc_RuntimeError, "Argument parsing problem");
  190. return NULL;
  191. }
  192. img = (Imaging)i0;
  193. rows = img->image8;
  194. width = img->xsize;
  195. height = img->ysize;
  196. for (row_idx = 0; row_idx < height; row_idx++) {
  197. UINT8 *row = rows[row_idx];
  198. for (col_idx = 0; col_idx < width; col_idx++) {
  199. if (row[col_idx]) {
  200. PyObject *coordObj = Py_BuildValue("(nn)", col_idx, row_idx);
  201. PyList_Append(ret, coordObj);
  202. Py_XDECREF(coordObj);
  203. }
  204. }
  205. }
  206. return ret;
  207. }
  208. static PyMethodDef functions[] = {
  209. /* Functions */
  210. {"apply", (PyCFunction)apply, METH_VARARGS, NULL},
  211. {"get_on_pixels", (PyCFunction)get_on_pixels, METH_VARARGS, NULL},
  212. {"match", (PyCFunction)match, METH_VARARGS, NULL},
  213. {NULL, NULL, 0, NULL}};
  214. PyMODINIT_FUNC
  215. PyInit__imagingmorph(void) {
  216. PyObject *m;
  217. static PyModuleDef module_def = {
  218. PyModuleDef_HEAD_INIT,
  219. "_imagingmorph", /* m_name */
  220. "A module for doing image morphology", /* m_doc */
  221. -1, /* m_size */
  222. functions, /* m_methods */
  223. };
  224. m = PyModule_Create(&module_def);
  225. return m;
  226. }