sobel.cc

 1 const char *help = "\
 2 progname: sobel.cc\n\
 3 code2html: This program reads a ppm image and apply Sobel edge detection.\n\
 4 version: Torch3 vision2.0, 2004-2005\n\
 5 (c) Sebastien Marcel (marcel@idiap.ch)\n";
 6 
 7 #include "ImageGray.h"
 8 #include "xtprobeImageDiskXFile.h"
 9 #include "ipSobel.h"
10 #include "CmdLine.h"
11 
12 using namespace Torch;
13 
14 int main(int argc, char **argv)
15 {
16 	char *image_filename;
17 	real threshold;
18 	bool verbose;
19   
20   	
21 	CmdLine cmd;
22 	cmd.setBOption("write log", false);
23   	cmd.info(help);
24   	cmd.addText("\nArguments:");
25   	cmd.addSCmdArg("image filename", &image_filename, "image filename");
26   	cmd.addText("\nOptions:");
27   	cmd.addRCmdOption("-threshold", &threshold, 10, "threshold");
28   	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
29 	cmd.read(argc, argv);
30 
31 	
32 	Image *image_in = NULL;
33 	Image *image_out = NULL;
34 	ImageDiskXFile *image_file = NULL;
35 
36 	image_in = new ImageGray();
37 	image_file = new xtprobeImageDiskXFile(image_filename, "r");
38 
39 	if(!image_in->loadImageXFile(image_file))
40 		error("Impossible to load the image");
41 
42 	delete image_file;
43 
44 	if(verbose)
45 	{
46 		print("Image info:\n");
47 		print("   width = %d\n", image_in->width);
48 		print("   height = %d\n", image_in->height);
49 		print("   format = %s (%d)\n", image_in->coding, image_in->n_planes);
50 	}
51 
52 	ipCore *sobel = NULL;
53 
54 	sobel = new ipSobel(image_in->width, image_in->height, "gray");
55 	sobel->setBOption("verbose", verbose);
56 	sobel->setROption("threshold", threshold);
57 	sobel->init();
58 	sobel->process(image_in);
59 	
60 	image_out = new ImageGray();
61 	image_out->setBOption("verbose", verbose);
62 
63 	image_out->copyFrom(image_in->width, image_in->height, sobel->seq_out->frames[0], "gray");
64 	image_out->save("gradientx.pgm");
65 
66 	image_out->copyFrom(image_in->width, image_in->height, sobel->seq_out->frames[1], "gray");
67 	image_out->save("gradienty.pgm");
68 
69 	image_out->copyFrom(image_in->width, image_in->height, sobel->seq_out->frames[2], "gray");
70 	image_out->save("gradient.pgm");
71 
72 	image_out->copyFrom(image_in->width, image_in->height, sobel->seq_out->frames[3], "gray");
73 	image_out->save("sobel.pgm");
74 
75 	delete sobel;
76 	delete image_out;
77 	delete image_in;
78 
79 	return(0);
80 }