/* * The Npic library and tools * * Copyright (C) 2003 Edouard Thiel * * This program is free software under the terms of the * GNU Lesser General Public License (LGPL) version 2.1. */ /* * npic-dtma.c - 10/01/2011 * */ #include void ShowUsage () { printf ( "npic-dtma - Compute Distance Transform and extract Medial Axis from it.\n" "Usage:\n" " npic-dtma -h | -help | --help : print help\n" " npic-dtma Mdist Mlut in1 out1 : compute DT and MA\n" "\n" " Mdist : a weighted distance mask, or a Euclidean distance mask,\n" " or \"sed\" for Squared Euclidean Distance\n" " Mlut : a medial axis neighbourhood test mask corresponding to\n" " the distance (will be created or updated) or \"none\"\n" " in1 : a binary image\n" " out1 : the medial axis of in1\n" "\n" "Mdist, Mlut : mask file in " NPIC_KNOWN_MASK_EXT " format (see npic-mask -h)\n" "in1, out1 : image in " NPIC_KNOWN_IMAGE_EXT " format\n" "\n" "Example: compute and show Medial Axis with distance <3,4,5> and with SED\n" " ./npic-dtma sed none ../images/K1b_2c.pgm.gz tmp1.pbm\n" " gimp tmp1.pbm &\n" "\n" " ./npic-dtma ../masks/d-3-4-5_3l.nmask none ../images/klette_3c.pan.gz tmp2.npz\n" " ./npic-geomv tmp2.npz tmp2.geom -show &\n" "\n" " ./npic-dtma sed ../masks/t-sed_3l.nmask ../images/klette_3c.pan.gz tmp3.npz\n" " ./npic-geomv tmp3.npz tmp3.geom -show &\n" "\n" ); } void ArgcExit (int argc, int n) { if (argc < n) { fprintf (stderr, "ERROR: %d argument(s) missing, " "type \"npic-dtma -h\" to get help.\n", n-argc); exit (1); } } int main (int argc, char *argv[]) { char *in1, *out1, *mdist, *mlut; Npic_image *np1, *np2; Npic_mask *nMdist = NULL, *nMlut = NULL; Npic_l Rverif1 = 0, Rverif2 = 0; ArgcExit (argc, 2); if (strcmp (argv[1], "-h") == 0 || strcmp (argv[1], "-help") == 0 || strcmp (argv[1], "--help") == 0) { ShowUsage (); exit (0); } ArgcExit (argc, 5); mdist = argv[1]; mlut = argv[2]; in1 = argv[3]; out1 = argv[4]; /* First load in1 to get the dimension */ printf ("Loading image \"%s\"\n", in1); np1 = NpicReadImage (in1); if (np1 == NULL) exit (1); /* Convert image type if necessary */ if (NpicImagePixelType (np1->type) == NPIC_C) NpicConvertImage_l (np1); /* Create np2 */ np2 = NpicDupImage (np1); if (np2 == NULL) exit (1); /* Load or create nMdist */ if (strcmp (mdist, "sed") == 0) { nMdist = NpicCreateMaskDP (np1->gen.dim, NPIC_L); if (nMdist == NULL) exit (1); NpicMaskAddProp (nMdist, "Nature", "Distance"); NpicMaskAddProp (nMdist, "DistanceType", "SquaredEuclidean"); } else { printf ("Loading distance \"%s\"\n", mdist); nMdist = NpicReadDistanceMask (mdist); if (nMdist == NULL) exit (1); } /* Load nMlut */ if (strcmp (mlut, "none") != 0) { printf ("Loading Mlut \"%s\"\n", mlut); nMlut = NpicReadMedialAxisMask (mlut); if (nMlut == NULL) { printf ("Load failed, creating Mlut ...\n"); nMlut = NpicCreateMaskDP (np1->gen.dim, NPIC_L); if (nMlut == NULL) exit (1); NpicMaskAddProp (nMlut, "Nature", "MedialAxis"); } } if (nMlut != NULL) Rverif1 = atoi (NpicMaskGetPropD (nMlut, "RVerified", "0")); /* Now compute DT and MA */ NpicExtractMA_bin (np1, np2, nMdist, nMlut); if (np2->gen.ok != NPIC_SUCCESS) exit(1); if (nMlut != NULL) Rverif2 = atoi (NpicMaskGetPropD (nMlut, "RVerified", "0")); /* Save results */ printf ("Saving image \"%s\"\n", out1); if (NpicWriteImage (np2, out1) != NPIC_SUCCESS) exit(1); if (strcmp (mlut, "none") != 0 && Rverif2 > Rverif1) { printf ("Creating or updating Mlut for Rverified = %" NPIC_PL " ...\n", Rverif2); NpicWriteMask (nMlut, mlut); } NpicDestroyImage (np1); NpicDestroyImage (np2); NpicDestroyMask (nMdist); NpicDestroyMask (nMlut); exit (0); }