histopgm.cc

 1 const char *help = "\
 2 progname: histopgm.cc\n\
 3 code2html: This program reads a pgm image and computes the histogram of pixels.\n\
 4 version: Torch3 vision2.0, 2003-2005\n\
 5 (c) Sebastien Marcel (marcel@idiap.ch)\n";
 6 
 7 #include "ImageGray.h"
 8 #include "ipHisto.h"
 9 #include "DiskXFile.h"
10 #include "CmdLine.h"
11 
12 using namespace Torch;
13 
14 int main(int argc, char **argv)
15 {
16 	char *image_filename;
17 	bool verbose;
18 	bool normalise;
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.addBCmdOption("-verbose", &verbose, false, "verbose");
28   	cmd.addBCmdOption("-normalise", &normalise, false, "normalise");
29 	cmd.read(argc, argv);
30 
31 
32   	DiskXFile *image_file = NULL;
33 	Image *image_in = NULL;
34 
35 	image_in = new ImageGray();
36 	image_in->setBOption("verbose", verbose);
37 	
38 	image_file = new DiskXFile(image_filename, "r");
39 	image_in->loadXFile(image_file);
40 	delete image_file;
41 
42 	if(verbose)
43 	{
44 		print("Image info:\n");
45 		print("   width = %d\n", image_in->width);
46 		print("   height = %d\n", image_in->height);
47 		print("   format = %s (%d)\n", image_in->coding, image_in->n_planes);
48 	}
49 
50 	ipCore *histo = NULL;
51 
52 	histo = new ipHisto(image_in->width, image_in->height, "gray");
53 	histo->setBOption("verbose", verbose);
54 	histo->setBOption("normalise", normalise);
55 	histo->process(image_in);
56 
57 	real *h = histo->seq_out->frames[0];
58 	
59 	XFile *histo_file = new DiskXFile("histo.txt", "w");
60 	int output_size = 256;
61 	for(int i = 0; i < output_size; i++)
62 	{
63 	   	int h_ = (int) (h[i] * image_in->width * image_in->height);
64 		
65 		if(h_ != 0) histo_file->printf("%d %g\n", i, h[i]);
66 	}
67 	delete histo_file;
68 	
69 	delete histo;
70 	delete image_in;
71 
72 	return(0);
73 }