filtercolorcube.cc

  1 const char *help = "\
  2 progname: filtercolorcube.cc\n\
  3 code2html: This program reads a ppm image and filter it using a colorcube.\n\
  4 version: Torch3 vision2.0, 2004-2005\n\
  5 (c) Sebastien Marcel (marcel@idiap.ch)\n";
  6 
  7 #include "ImageRgb.h"
  8 #include "ipColorCube.h"
  9 #include "ipConnectedComponents.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 	char *colorcube_filename;
 22 	bool cc;
 23 	int min_size;
 24 	int max_size;
 25 	int cn;
 26 	bool octon;
 27 	bool verbose;
 28   
 29 	// Construct the command line
 30   	CmdLine cmd;
 31 	cmd.setBOption("write log", false);
 32 	
 33   	// Put the help line at the beginning
 34   	cmd.info(help);
 35 
 36   	cmd.addText("\nArguments:");
 37   	cmd.addSCmdArg("image filename", &image_filename, "image filename");
 38   	cmd.addSCmdArg("colorcube filename", &colorcube_filename, "colorcube filename");
 39   	cmd.addText("\nOptions:");
 40   	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 41   	cmd.addBCmdOption("-cc", &cc, false, "computes connected components");
 42   	cmd.addICmdOption("-minsize", &min_size, 500, "minimum size");
 43   	cmd.addICmdOption("-maxsize", &max_size, 100000, "maximum size");
 44   	cmd.addICmdOption("-cn", &cn, 4, "connected neighbourhood (4 or 8)");
 45   	cmd.addBCmdOption("-octon", &octon, false, "uses octon instead of rectangles");
 46 
 47 	cmd.read(argc, argv);
 48 
 49 
 50 	Image *image_in = NULL;
 51 	Image *image_out = NULL;
 52 
 53 	image_in = new ImageRgb();
 54 	image_in->setBOption("verbose", verbose);
 55 	
 56 	ImageDiskXFile *image_loader = new xtprobeImageDiskXFile(image_filename, "r");
 57 	image_in->loadImageXFile(image_loader);
 58 	delete image_loader;
 59 
 60 	if(verbose)
 61 	{
 62 		print("Image info:\n");
 63 		print("   width = %d\n", image_in->width);
 64 		print("   height = %d\n", image_in->height);
 65 		print("   format = %s (%d)\n", image_in->coding, image_in->n_planes);
 66 	}
 67 
 68 	ipColorCube *imachine = NULL;
 69 
 70 	imachine = new ipColorCube(image_in->width, image_in->height, "rgb");
 71 	imachine->setBOption("verbose", verbose);
 72 	imachine->load(colorcube_filename);
 73 	imachine->process(image_in);
 74 	
 75 	image_out = new ImageRgb();
 76 	image_out->setBOption("verbose", verbose);
 77 
 78 	image_out->copyFrom(image_in->width, image_in->height, imachine->seq_out->frames[0], "rgb");
 79 	
 80 	if(cc)
 81 	{
 82 	   	Sequence *seq_in = new Sequence(&imachine->realbinaryimage, 1, image_in->width * image_in->height);
 83 		
 84 		roiCollector *collector = NULL;
 85 
 86 		if(octon) collector = new octonRoiCollector();
 87 		else collector = new rectRoiCollector();
 88 
 89 		ipCore *ipcc = new ipConnectedComponents(image_in->width, image_in->height, "gray", collector, min_size, max_size, cn);
 90 		ipcc->setBOption("verbose", verbose);
 91 		ipcc->process(seq_in);
 92 
 93 		if(verbose) print("n_roi = %d\n", collector->n_roi);
 94 
 95 		if(octon)
 96 		{
 97 			sOcton *oroi = (sOcton *) collector->roi;
 98 			for(int i = 0 ; i < collector->n_roi ; i++)
 99 			{
100 				if(verbose) print("   x=%4d   y=%5d   w=%4d   h=%4d\n", oroi[i].x_min, oroi[i].y_min, oroi[i].width, oroi[i].height);
101 
102 				Octon2D octon(oroi[i]);
103 
104 				octon.draw(image_out, green);
105 			}
106 		}
107 		else
108 		{
109 			sRect2D *rroi = (sRect2D *) collector->roi;
110 			for(int i = 0 ; i < collector->n_roi ; i++)
111 			{
112 				if(verbose) print("   x=%4d   y=%5d   w=%4d   h=%4d\n", rroi[i].x, rroi[i].y, rroi[i].w, rroi[i].h);
113 
114 				Rectangle2D rect(rroi[i]);
115 
116 				rect.draw(image_out, green);
117 			}
118 		}
119 	
120 		ImageRgb *blobimage = new ImageRgb(image_in->width, image_in->height);
121 		
122 		for(int y = 0; y < image_in->height; y++)
123 			for(int x = 0; x < image_in->width; x++)
124 			{
125 		   		int label_ = (int) ipcc->seq_out->frames[0][y*image_in->width+x];
126 			
127 				Color c;
128 				
129 				if(label_ == 0) c = white;
130 				else if(label_ == 1) c = red;
131 				else if(label_ == 2) c = green;
132 				else if(label_ == 3) c = blue;
133 				else if(label_ == 4) c = yellow;
134 				else if(label_ == 5) c = cyan;
135 				else if(label_ == 6) c = pink;
136 				else if(label_ == 7) c = orange;
137 				else c = black;
138 
139 				blobimage->drawpixel(x, y, c);
140 			}
141 
142 		blobimage->save("blob.ppm");
143 		
144 		delete blobimage;
145 		delete ipcc;
146 		delete collector;
147 		delete seq_in;
148 	}
149 	
150 	DiskXFile *image_file = new DiskXFile("filter.ppm", "w");
151 	image_out->saveXFile(image_file);
152 	delete image_file;
153 
154 	delete imachine;
155 	delete image_out;
156 	delete image_in;
157 
158 	return(0);
159 }