dilate.cc

 1 const char *help = "\
 2 progname: dilate.cc\n\
 3 code2html: This program reads an image and apply dilate image morphology operator.\n\
 4 version: Torch3 vision2.0, 2003-2005\n\
 5 (c) Sebastien Marcel (marcel@idiap.ch)\n";
 6 
 7 #include "ImageGray.h"
 8 #include "ipDilate.h"
 9 #include "ipDilateXY.h"
10 #include "DiskXFile.h"
11 #include "CmdLine.h"
12 
13 using namespace Torch;
14 
15 int main(int argc, char **argv)
16 {
17 	char *image_filename;
18 	int iteration;
19 	int radius;
20 	int threshold;
21 	bool xy;
22 	bool verbose;
23   
24 	
25   	CmdLine cmd;
26 	cmd.setBOption("write log", false);
27   	cmd.info(help);
28   	cmd.addSCmdArg("image filename", &image_filename, "image filename");
29   	cmd.addText("\nOptions:");
30   	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
31   	cmd.addBCmdOption("-xy", &xy, false, "dilate only in XY direction");
32   	cmd.addICmdOption("-iteration", &iteration, 1, "iteration");
33   	cmd.addICmdOption("-radius", &radius, 1, "radius");
34   	cmd.addICmdOption("-threshold", &threshold, 100, "threshold");
35 	cmd.read(argc, argv);
36 
37 
38   	DiskXFile *image_file = NULL;
39 	Image *image_in = NULL;
40 	Image *image_out = NULL;
41 
42 	image_in = new ImageGray();
43 	image_in->setBOption("verbose", verbose);
44 	
45 	image_file = new DiskXFile(image_filename, "r");
46 	image_in->loadXFile(image_file);
47 	delete image_file;
48 
49 	if(verbose)
50 	{
51 		print("Image info:\n");
52 		print("   width = %d\n", image_in->width);
53 		print("   height = %d\n", image_in->height);
54 		print("   format = %s (%d)\n", image_in->coding, image_in->n_planes);
55 	}
56 
57 	ipCore *morph = NULL;
58 
59 	if(xy) morph = new ipDilateXY(image_in->width, image_in->height, "gray", iteration, radius, radius, threshold);
60 	else morph = new ipDilate(image_in->width, image_in->height, "gray", iteration, radius, threshold);
61 	morph->setBOption("verbose", verbose);
62 	morph->process(image_in);
63 	
64 	image_out = new ImageGray();
65 	image_out->setBOption("verbose", verbose);
66 
67 	image_out->copyFrom(image_in->width, image_in->height, morph->seq_out->frames[0], "gray");
68 	
69 	if(xy) image_file = new DiskXFile("dilateXY.pgm", "w");
70 	else image_file = new DiskXFile("dilate.pgm", "w");
71 	image_out->saveXFile(image_file);
72 	delete image_file;
73 
74 	delete morph;
75 	delete image_out;
76 	delete image_in;
77 
78 	return(0);
79 }