segmentml.cc

  1 const char *help = "\
  2 progname: segmentml.cc\n\
  3 code2html: This program reads a ppm image and segment it using diagonal GMM via ML.\n\
  4 version: Torch3 vision2.0, 2005\n\
  5 (c) Sebastien Marcel (marcel@idiap.ch)\n";
  6 
  7 #include "ImageRgb.h"
  8 #include "ImageGray.h"
  9 #include "ipSegmentDiagGMM.h"
 10 #include "DiskXFile.h"
 11 #include "xtprobeImageDiskXFile.h"
 12 //#include "Rectangle2D.h"
 13 //#include "Octon2D.h"
 14 #include "CmdLine.h"
 15 
 16 using namespace Torch;
 17 
 18 int main(int argc, char **argv)
 19 {
 20 	char *image_filename;
 21 	int n_gaussians;
 22 	//bool octon;
 23 	bool gray;
 24 	bool xy;
 25 	bool norm;
 26 	bool verbose;
 27   
 28 	// Construct the command line
 29   	CmdLine cmd;
 30 	cmd.setBOption("write log", false);
 31 	
 32   	// Put the help line at the beginning
 33   	cmd.info(help);
 34 
 35   	cmd.addText("\nArguments:");
 36   	cmd.addSCmdArg("image filename", &image_filename, "image filename");
 37   	cmd.addText("\nOptions:");
 38   	cmd.addBCmdOption("-gray", &gray, false, "gray");
 39   	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 40   	cmd.addBCmdOption("-xy", &xy, false, "use also pixel coordinates for the segmentation");
 41   	cmd.addBCmdOption("-norm", &norm, false, "use normalized pixels when using color");
 42   	cmd.addICmdOption("-g", &n_gaussians, 3, "number of gaussians");
 43   	//cmd.addBCmdOption("-octon", &octon, false, "uses octon instead of rectangles");
 44 
 45 	cmd.read(argc, argv);
 46 
 47 
 48 	Image *image_in = NULL;
 49 
 50 	if(gray) image_in = new ImageGray();
 51 	else image_in = new ImageRgb();
 52 	image_in->setBOption("verbose", verbose);
 53 	
 54 	ImageDiskXFile *image_loader = new xtprobeImageDiskXFile(image_filename, "r");
 55 	image_in->loadImageXFile(image_loader);
 56 	delete image_loader;
 57 
 58 	if(verbose)
 59 	{
 60 		print("Image info:\n");
 61 		print("   width = %d\n", image_in->width);
 62 		print("   height = %d\n", image_in->height);
 63 		print("   format = %s (%d)\n", image_in->coding, image_in->n_planes);
 64 	}
 65 
 66 	ipCore *imachine = NULL;
 67 
 68 	if(gray) imachine = new ipSegmentDiagGMM(image_in->width, image_in->height, "gray", n_gaussians, xy);
 69 	else imachine = new ipSegmentDiagGMM(image_in->width, image_in->height, "rgb", n_gaussians, xy, norm);
 70 	imachine->setBOption("verbose", verbose);
 71 	imachine->process(image_in);
 72 
 73 	print("done.\n");
 74 	   
 75 	{
 76 		ImageRgb *blobimage = new ImageRgb(image_in->width, image_in->height);
 77 		
 78 		for(int y = 0; y < image_in->height; y++)
 79 			for(int x = 0; x < image_in->width; x++)
 80 			{
 81 		   		int label_ = (int) imachine->seq_out->frames[0][y*image_in->width+x];
 82 			
 83 				Color c;
 84 				
 85 				if(label_ == 0) c = white;
 86 				else if(label_ == 1) c = red;
 87 				else if(label_ == 2) c = green;
 88 				else if(label_ == 3) c = blue;
 89 				else if(label_ == 4) c = yellow;
 90 				else if(label_ == 5) c = cyan;
 91 				else if(label_ == 6) c = pink;
 92 				else if(label_ == 7) c = orange;
 93 				else c = black;
 94 
 95 				blobimage->drawpixel(x, y, c);
 96 			}
 97 
 98 		blobimage->save("blob.ppm");
 99 		
100 		delete blobimage;
101 	}
102 	
103 	delete imachine;
104 	delete image_in;
105 
106 	return(0);
107 }