Leptonica  1.77.0
Image processing and image analysis suite
ccthin.c
Go to the documentation of this file.
1 /*====================================================================*
2  - Copyright (C) 2001 Leptonica. All rights reserved.
3  -
4  - Redistribution and use in source and binary forms, with or without
5  - modification, are permitted provided that the following conditions
6  - are met:
7  - 1. Redistributions of source code must retain the above copyright
8  - notice, this list of conditions and the following disclaimer.
9  - 2. Redistributions in binary form must reproduce the above
10  - copyright notice, this list of conditions and the following
11  - disclaimer in the documentation and/or other materials
12  - provided with the distribution.
13  -
14  - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18  - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *====================================================================*/
26 
38 #include "allheaders.h"
39 
40  /* ------------------------------------------------------------
41  * The sels used here (and their rotated counterparts) are the
42  * useful 3x3 Sels for thinning. They are defined in sel2.c,
43  * and the sets are constructed in selaMakeThinSets().
44  * The notation is based on "Connectivity-preserving morphological
45  * image transformations", a version of which can be found at
46  * http://www.leptonica.com/papers/conn.pdf
47  * ------------------------------------------------------------ */
48 
49 /*----------------------------------------------------------------*
50  * CC-preserving thinning *
51  *----------------------------------------------------------------*/
67 PIXA *
69  l_int32 type,
70  l_int32 connectivity,
71  l_int32 maxiters)
72 {
73 l_int32 i, n, d, same;
74 PIX *pix1, *pix2;
75 PIXA *pixad;
76 SELA *sela;
77 
78  PROCNAME("pixaThinConnected");
79 
80  if (!pixas)
81  return (PIXA *)ERROR_PTR("pixas not defined", procName, NULL);
82  if (type != L_THIN_FG && type != L_THIN_BG)
83  return (PIXA *)ERROR_PTR("invalid fg/bg type", procName, NULL);
84  if (connectivity != 4 && connectivity != 8)
85  return (PIXA *)ERROR_PTR("connectivity not 4 or 8", procName, NULL);
86  if (maxiters == 0) maxiters = 10000;
87 
88  pixaVerifyDepth(pixas, &same, &d);
89  if (d != 1)
90  return (PIXA *)ERROR_PTR("pix are not all 1 bpp", procName, NULL);
91 
92  if (connectivity == 4)
93  sela = selaMakeThinSets(1, 0);
94  else /* connectivity == 8 */
95  sela = selaMakeThinSets(5, 0);
96 
97  n = pixaGetCount(pixas);
98  pixad = pixaCreate(n);
99  for (i = 0; i < n; i++) {
100  pix1 = pixaGetPix(pixas, i, L_CLONE);
101  pix2 = pixThinConnectedBySet(pix1, type, sela, maxiters);
102  pixaAddPix(pixad, pix2, L_INSERT);
103  pixDestroy(&pix1);
104  }
105 
106  selaDestroy(&sela);
107  return pixad;
108 }
109 
110 
157 PIX *
159  l_int32 type,
160  l_int32 connectivity,
161  l_int32 maxiters)
162 {
163 PIX *pixd;
164 SELA *sela;
165 
166  PROCNAME("pixThinConnected");
167 
168  if (!pixs)
169  return (PIX *)ERROR_PTR("pixs not defined", procName, NULL);
170  if (pixGetDepth(pixs) != 1)
171  return (PIX *)ERROR_PTR("pixs not 1 bpp", procName, NULL);
172  if (type != L_THIN_FG && type != L_THIN_BG)
173  return (PIX *)ERROR_PTR("invalid fg/bg type", procName, NULL);
174  if (connectivity != 4 && connectivity != 8)
175  return (PIX *)ERROR_PTR("connectivity not 4 or 8", procName, NULL);
176  if (maxiters == 0) maxiters = 10000;
177 
178  if (connectivity == 4)
179  sela = selaMakeThinSets(1, 0);
180  else /* connectivity == 8 */
181  sela = selaMakeThinSets(5, 0);
182 
183  pixd = pixThinConnectedBySet(pixs, type, sela, maxiters);
184 
185  selaDestroy(&sela);
186  return pixd;
187 }
188 
189 
219 PIX *
221  l_int32 type,
222  SELA *sela,
223  l_int32 maxiters)
224 {
225 l_int32 i, j, r, nsels, same;
226 PIXA *pixahmt;
227 PIX **pixhmt; /* array owned by pixahmt; do not destroy! */
228 PIX *pix1, *pix2, *pixd;
229 SEL *sel, *selr;
230 
231  PROCNAME("pixThinConnectedBySet");
232 
233  if (!pixs)
234  return (PIX *)ERROR_PTR("pixs not defined", procName, NULL);
235  if (pixGetDepth(pixs) != 1)
236  return (PIX *)ERROR_PTR("pixs not 1 bpp", procName, NULL);
237  if (type != L_THIN_FG && type != L_THIN_BG)
238  return (PIX *)ERROR_PTR("invalid fg/bg type", procName, NULL);
239  if (!sela)
240  return (PIX *)ERROR_PTR("sela not defined", procName, NULL);
241  if (maxiters == 0) maxiters = 10000;
242 
243  /* Set up array of temp pix to hold hmts */
244  nsels = selaGetCount(sela);
245  pixahmt = pixaCreate(nsels);
246  for (i = 0; i < nsels; i++) {
247  pix1 = pixCreateTemplate(pixs);
248  pixaAddPix(pixahmt, pix1, L_INSERT);
249  }
250  pixhmt = pixaGetPixArray(pixahmt);
251  if (!pixhmt) {
252  pixaDestroy(&pixahmt);
253  return (PIX *)ERROR_PTR("pixhmt array not made", procName, NULL);
254  }
255 
256  /* Set up initial image for fg thinning */
257  if (type == L_THIN_FG)
258  pixd = pixCopy(NULL, pixs);
259  else /* bg thinning */
260  pixd = pixInvert(NULL, pixs);
261 
262  /* Thin the fg, with up to maxiters iterations */
263  for (i = 0; i < maxiters; i++) {
264  pix1 = pixCopy(NULL, pixd); /* test for completion */
265  for (r = 0; r < 4; r++) { /* over 90 degree rotations of Sels */
266  for (j = 0; j < nsels; j++) { /* over individual sels in sela */
267  sel = selaGetSel(sela, j); /* not a copy */
268  selr = selRotateOrth(sel, r);
269  pixHMT(pixhmt[j], pixd, selr);
270  selDestroy(&selr);
271  if (j > 0)
272  pixOr(pixhmt[0], pixhmt[0], pixhmt[j]); /* accum result */
273  }
274  pixSubtract(pixd, pixd, pixhmt[0]); /* remove result */
275  }
276  pixEqual(pixd, pix1, &same);
277  pixDestroy(&pix1);
278  if (same) {
279 /* L_INFO("%d iterations to completion\n", procName, i); */
280  break;
281  }
282  }
283 
284  /* This is a bit tricky. If we're thickening the foreground, then
285  * we get a fg border of thickness equal to the number of
286  * iterations. This border is connected to all components that
287  * were initially touching the border, but as it grows, it does
288  * not touch other growing components -- it leaves a 1 pixel wide
289  * background between it and the growing components, and that
290  * thin background prevents the components from growing further.
291  * This border can be entirely removed as follows:
292  * (1) Subtract the original (unthickened) image pixs from the
293  * thickened image. This removes the pixels that were originally
294  * touching the border.
295  * (2) Get all remaining pixels that are connected to the border.
296  * (3) Remove those pixels from the thickened image. */
297  if (type == L_THIN_BG) {
298  pixInvert(pixd, pixd); /* finish with duality */
299  pix1 = pixSubtract(NULL, pixd, pixs);
300  pix2 = pixExtractBorderConnComps(pix1, 4);
301  pixSubtract(pixd, pixd, pix2);
302  pixDestroy(&pix1);
303  pixDestroy(&pix2);
304  }
305 
306  pixaDestroy(&pixahmt);
307  return pixd;
308 }
309 
310 
340 SELA *
341 selaMakeThinSets(l_int32 index,
342  l_int32 debug)
343 {
344 SEL *sel;
345 SELA *sela1, *sela2, *sela3;
346 
347  PROCNAME("selaMakeThinSets");
348 
349  if (index < 1 || index > 11)
350  return (SELA *)ERROR_PTR("invalid index", procName, NULL);
351 
352  sela2 = selaCreate(4);
353  switch(index)
354  {
355  case 1:
356  sela1 = sela4ccThin(NULL);
357  selaFindSelByName(sela1, "sel_4_1", NULL, &sel);
358  selaAddSel(sela2, sel, NULL, L_COPY);
359  selaFindSelByName(sela1, "sel_4_2", NULL, &sel);
360  selaAddSel(sela2, sel, NULL, L_COPY);
361  selaFindSelByName(sela1, "sel_4_3", NULL, &sel);
362  selaAddSel(sela2, sel, NULL, L_COPY);
363  break;
364  case 2:
365  sela1 = sela4ccThin(NULL);
366  selaFindSelByName(sela1, "sel_4_1", NULL, &sel);
367  selaAddSel(sela2, sel, NULL, L_COPY);
368  selaFindSelByName(sela1, "sel_4_5", NULL, &sel);
369  selaAddSel(sela2, sel, NULL, L_COPY);
370  selaFindSelByName(sela1, "sel_4_6", NULL, &sel);
371  selaAddSel(sela2, sel, NULL, L_COPY);
372  break;
373  case 3:
374  sela1 = sela4ccThin(NULL);
375  selaFindSelByName(sela1, "sel_4_1", NULL, &sel);
376  selaAddSel(sela2, sel, NULL, L_COPY);
377  selaFindSelByName(sela1, "sel_4_7", NULL, &sel);
378  selaAddSel(sela2, sel, NULL, L_COPY);
379  sel = selRotateOrth(sel, 1);
380  selaAddSel(sela2, sel, "sel_4_7_rot", L_INSERT);
381  break;
382  case 4:
383  sela1 = sela4and8ccThin(NULL);
384  selaFindSelByName(sela1, "sel_48_1", NULL, &sel);
385  selaAddSel(sela2, sel, NULL, L_COPY);
386  sel = selRotateOrth(sel, 1);
387  selaAddSel(sela2, sel, "sel_48_1_rot", L_INSERT);
388  selaFindSelByName(sela1, "sel_48_2", NULL, &sel);
389  selaAddSel(sela2, sel, NULL, L_COPY);
390  break;
391  case 5:
392  sela1 = sela8ccThin(NULL);
393  selaFindSelByName(sela1, "sel_8_2", NULL, &sel);
394  selaAddSel(sela2, sel, NULL, L_COPY);
395  selaFindSelByName(sela1, "sel_8_3", NULL, &sel);
396  selaAddSel(sela2, sel, NULL, L_COPY);
397  selaFindSelByName(sela1, "sel_8_5", NULL, &sel);
398  selaAddSel(sela2, sel, NULL, L_COPY);
399  selaFindSelByName(sela1, "sel_8_6", NULL, &sel);
400  selaAddSel(sela2, sel, NULL, L_COPY);
401  break;
402  case 6:
403  sela1 = sela8ccThin(NULL);
404  sela3 = sela4and8ccThin(NULL);
405  selaFindSelByName(sela1, "sel_8_2", NULL, &sel);
406  selaAddSel(sela2, sel, NULL, L_COPY);
407  selaFindSelByName(sela1, "sel_8_3", NULL, &sel);
408  selaAddSel(sela2, sel, NULL, L_COPY);
409  selaFindSelByName(sela3, "sel_48_2", NULL, &sel);
410  selaAddSel(sela2, sel, NULL, L_COPY);
411  selaDestroy(&sela3);
412  break;
413  case 7:
414  sela1 = sela8ccThin(NULL);
415  selaFindSelByName(sela1, "sel_8_1", NULL, &sel);
416  selaAddSel(sela2, sel, NULL, L_COPY);
417  selaFindSelByName(sela1, "sel_8_5", NULL, &sel);
418  selaAddSel(sela2, sel, NULL, L_COPY);
419  selaFindSelByName(sela1, "sel_8_6", NULL, &sel);
420  selaAddSel(sela2, sel, NULL, L_COPY);
421  break;
422  case 8:
423  sela1 = sela8ccThin(NULL);
424  selaFindSelByName(sela1, "sel_8_2", NULL, &sel);
425  selaAddSel(sela2, sel, NULL, L_COPY);
426  selaFindSelByName(sela1, "sel_8_3", NULL, &sel);
427  selaAddSel(sela2, sel, NULL, L_COPY);
428  selaFindSelByName(sela1, "sel_8_8", NULL, &sel);
429  selaAddSel(sela2, sel, NULL, L_COPY);
430  selaFindSelByName(sela1, "sel_8_9", NULL, &sel);
431  selaAddSel(sela2, sel, NULL, L_COPY);
432  break;
433  case 9:
434  sela1 = sela8ccThin(NULL);
435  selaFindSelByName(sela1, "sel_8_5", NULL, &sel);
436  selaAddSel(sela2, sel, NULL, L_COPY);
437  selaFindSelByName(sela1, "sel_8_6", NULL, &sel);
438  selaAddSel(sela2, sel, NULL, L_COPY);
439  selaFindSelByName(sela1, "sel_8_7", NULL, &sel);
440  selaAddSel(sela2, sel, NULL, L_COPY);
441  sel = selRotateOrth(sel, 1);
442  selaAddSel(sela2, sel, "sel_8_7_rot", L_INSERT);
443  break;
444  case 10: /* thicken for this one; use just a few iterations */
445  sela1 = sela4ccThin(NULL);
446  selaFindSelByName(sela1, "sel_4_2", NULL, &sel);
447  selaAddSel(sela2, sel, NULL, L_COPY);
448  selaFindSelByName(sela1, "sel_4_3", NULL, &sel);
449  selaAddSel(sela2, sel, NULL, L_COPY);
450  break;
451  case 11: /* thicken for this one; use just a few iterations */
452  sela1 = sela8ccThin(NULL);
453  selaFindSelByName(sela1, "sel_8_4", NULL, &sel);
454  selaAddSel(sela2, sel, NULL, L_COPY);
455  break;
456  }
457 
458  /* Optionally display the sel set */
459  if (debug) {
460  PIX *pix1;
461  char buf[32];
462  lept_mkdir("/lept/sels");
463  pix1 = selaDisplayInPix(sela2, 35, 3, 15, 4);
464  snprintf(buf, sizeof(buf), "/tmp/lept/sels/set%d.png", index);
465  pixWrite(buf, pix1, IFF_PNG);
466  pixDisplay(pix1, 100, 100);
467  pixDestroy(&pix1);
468  }
469 
470  selaDestroy(&sela1);
471  return sela2;
472 }
473 
PIXA * pixaThinConnected(PIXA *pixas, l_int32 type, l_int32 connectivity, l_int32 maxiters)
pixaThinConnected()
Definition: ccthin.c:68
l_int32 lept_mkdir(const char *subdir)
lept_mkdir()
Definition: utils2.c:1944
Definition: pix.h:717
l_ok pixaVerifyDepth(PIXA *pixa, l_int32 *psame, l_int32 *pmaxd)
pixaVerifyDepth()
Definition: pixabasic.c:941
PIXA * pixaCreate(l_int32 n)
pixaCreate()
Definition: pixabasic.c:163
l_int32 n
Definition: ccbord.h:111
SELA * sela4ccThin(SELA *sela)
sela4ccThin()
Definition: sel2.c:749
PIX * pixExtractBorderConnComps(PIX *pixs, l_int32 connectivity)
pixExtractBorderConnComps()
Definition: seedfill.c:694
l_int32 selaGetCount(SELA *sela)
selaGetCount()
Definition: sel1.c:639
PIX * pixInvert(PIX *pixd, PIX *pixs)
pixInvert()
Definition: pix3.c:1395
PIX * pixCreateTemplate(PIX *pixs)
pixCreateTemplate()
Definition: pix1.c:367
SELA * selaMakeThinSets(l_int32 index, l_int32 debug)
selaMakeThinSets()
Definition: ccthin.c:341
PIX ** pixaGetPixArray(PIXA *pixa)
pixaGetPixArray()
Definition: pixabasic.c:916
void selaDestroy(SELA **psela)
selaDestroy()
Definition: sel1.c:272
SELA * sela8ccThin(SELA *sela)
sela8ccThin()
Definition: sel2.c:790
PIX * pixThinConnectedBySet(PIX *pixs, l_int32 type, SELA *sela, l_int32 maxiters)
pixThinConnectedBySet()
Definition: ccthin.c:220
PIX * pixThinConnected(PIX *pixs, l_int32 type, l_int32 connectivity, l_int32 maxiters)
pixThinConnected()
Definition: ccthin.c:158
l_ok pixaAddPix(PIXA *pixa, PIX *pix, l_int32 copyflag)
pixaAddPix()
Definition: pixabasic.c:503
void selDestroy(SEL **psel)
selDestroy()
Definition: sel1.c:337
l_ok selaFindSelByName(SELA *sela, const char *name, l_int32 *pindex, SEL **psel)
selaFindSelByName()
Definition: sel1.c:732
PIX * pixSubtract(PIX *pixd, PIX *pixs1, PIX *pixs2)
pixSubtract()
Definition: pix3.c:1639
void pixDestroy(PIX **ppix)
pixDestroy()
Definition: pix1.c:543
Definition: pix.h:454
PIX * pixOr(PIX *pixd, PIX *pixs1, PIX *pixs2)
pixOr()
Definition: pix3.c:1446
SELA * selaCreate(l_int32 n)
selaCreate()
Definition: sel1.c:239
l_ok selaAddSel(SELA *sela, SEL *sel, const char *selname, l_int32 copyflag)
selaAddSel()
Definition: sel1.c:566
PIX * pixHMT(PIX *pixd, PIX *pixs, SEL *sel)
pixHMT()
Definition: morph.c:338
PIX * pixaGetPix(PIXA *pixa, l_int32 index, l_int32 accesstype)
pixaGetPix()
Definition: pixabasic.c:672
Definition: pix.h:718
SELA * sela4and8ccThin(SELA *sela)
sela4and8ccThin()
Definition: sel2.c:831
Definition: pix.h:134
Definition: pix.h:719
PIX * pixCopy(PIX *pixd, PIX *pixs)
pixCopy()
Definition: pix1.c:628
l_ok pixEqual(PIX *pix1, PIX *pix2, l_int32 *psame)
pixEqual()
Definition: compare.c:150
SEL * selRotateOrth(SEL *sel, l_int32 quads)
selRotateOrth()
Definition: sel1.c:1243
SEL * selaGetSel(SELA *sela, l_int32 i)
selaGetSel()
Definition: sel1.c:664
Definition: morph.h:74
void pixaDestroy(PIXA **ppixa)
pixaDestroy()
Definition: pixabasic.c:408
l_int32 pixaGetCount(PIXA *pixa)
pixaGetCount()
Definition: pixabasic.c:631
PIX * selaDisplayInPix(SELA *sela, l_int32 size, l_int32 gthick, l_int32 spacing, l_int32 ncols)
selaDisplayInPix()
Definition: sel1.c:2318