warp.cc

  1 const char *help = "\
  2 progname: warp.cc\n\
  3 code2html: This program reads a ppm image and 2 sets of landmark points and warp the image.\n\
  4 version: Torch3 vision2.0, 2005\n\
  5 (c) Sebastien Marcel (marcel@idiap.ch)\n";
  6 
  7 #include "ImageGray.h"
  8 #include "xtprobeImageDiskXFile.h"
  9 #include "ipWarp.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 	char *ldm_in_filename;
 19 	char *ldm_out_filename;
 20 	bool verbose;
 21   
 22   	
 23 	CmdLine cmd;
 24 	cmd.setBOption("write log", false);
 25   	cmd.info(help);
 26   	cmd.addText("\nArguments:");
 27   	cmd.addSCmdArg("image filename", &image_filename, "image filename");
 28   	cmd.addSCmdArg("input landmark filename", &ldm_in_filename, "input landmark filename");
 29   	cmd.addSCmdArg("output landmark filename", &ldm_out_filename, "output landmark filename");
 30   	cmd.addText("\nOptions:");
 31   	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 32 	cmd.read(argc, argv);
 33 	
 34 	Image *image_in = NULL;
 35 	Image *image_out = NULL;
 36 	ImageDiskXFile *image_file = NULL;
 37 
 38 	image_in = new ImageGray();
 39 	image_file = new xtprobeImageDiskXFile(image_filename, "r");
 40 
 41 	if(!image_in->loadImageXFile(image_file))
 42 		error("Impossible to load the image");
 43 
 44 	delete image_file;
 45 
 46 	if(verbose)
 47 	{
 48 		print("Image info:\n");
 49 		print("   width = %d\n", image_in->width);
 50 		print("   height = %d\n", image_in->height);
 51 		print("   format = %s (%d)\n", image_in->coding, image_in->n_planes);
 52 	}
 53 
 54 
 55 	DiskXFile *file_in = new DiskXFile(ldm_in_filename, "r");
 56 	DiskXFile *file_out = new DiskXFile(ldm_out_filename, "r");
 57 
 58 	int n_ldm;
 59 
 60 	file_in->scanf("%d", &n_ldm);
 61 	sPoint2D *ldm_in = new sPoint2D [n_ldm];
 62 	//sPoint2D *ldm_in = new sPoint2D [n_ldm+image_in->width*2+(image_in->height-2)*2];
 63 	
 64 	if(verbose) print("n_ldm in = %d\n", n_ldm);
 65 	
 66 	for(int i = 0 ; i < n_ldm ; i++)
 67 	{
 68 	   	int x, y;
 69 		
 70 		file_in->scanf("%d", &x);
 71 		file_in->scanf("%d", &y);
 72 
 73 		ldm_in[i].x = x;
 74 		ldm_in[i].y = y;
 75 
 76 		if(verbose) print("   ldm[%4d] = %g %g\n", i, ldm_in[i].x, ldm_in[i].y);
 77 	}
 78 
 79 	/*
 80 	for(int i = 0 ; i < image_in->width ; i++)
 81 	{
 82 		ldm_in[n_ldm].x = i;
 83 		ldm_in[n_ldm].y = 0;
 84 		
 85 		n_ldm++;
 86 
 87 		ldm_in[n_ldm].x = i;
 88 		ldm_in[n_ldm].y = image_in->height-1;
 89 		
 90 		n_ldm++;
 91 	}
 92 	   
 93 	for(int i = 1 ; i < image_in->height-1 ; i++)
 94 	{
 95 		ldm_in[n_ldm].x = 0;
 96 		ldm_in[n_ldm].y = i;
 97 		
 98 		n_ldm++;
 99 		
100 		ldm_in[n_ldm].x = image_in->width-1;
101 		ldm_in[n_ldm].y = i;
102 		
103 		n_ldm++;
104 	}
105 	*/
106 
107 	file_out->scanf("%d", &n_ldm);
108 	sPoint2D *ldm_out = new sPoint2D [n_ldm];
109 	//sPoint2D *ldm_out = new sPoint2D [n_ldm+image_in->width*2+(image_in->height-2)*2];
110 	
111 	if(verbose) print("n_ldm out = %d\n", n_ldm);
112 
113 	for(int i = 0 ; i < n_ldm ; i++)
114 	{
115 	   	int x, y;
116 		
117 		file_out->scanf("%d", &x);
118 		file_out->scanf("%d", &y);
119 
120 		ldm_out[i].x = x;
121 		ldm_out[i].y = y;
122 
123 		if(verbose) print("   ldm[%4d] = %g %g\n", i, ldm_out[i].x, ldm_out[i].y);
124 	}
125 	
126    	/*
127 	for(int i = 0 ; i < image_in->width ; i++)
128 	{
129 		ldm_out[n_ldm].x = i;
130 		ldm_out[n_ldm].y = 0;
131 		
132 		n_ldm++;
133 
134 		ldm_out[n_ldm].x = i;
135 		ldm_out[n_ldm].y = image_in->height-1;
136 		
137 		n_ldm++;
138 	}
139 	   
140 	for(int i = 1 ; i < image_in->height-1 ; i++)
141 	{
142 		ldm_out[n_ldm].x = 0;
143 		ldm_out[n_ldm].y = i;
144 		
145 		n_ldm++;
146 		
147 		ldm_out[n_ldm].x = image_in->width-1;
148 		ldm_out[n_ldm].y = i;
149 		
150 		n_ldm++;
151 	}
152 	*/
153 
154 	ipCore *warp = NULL;
155 
156 	warp = new ipWarp(image_in->width, image_in->height, "gray", n_ldm, ldm_in, ldm_out);
157 	warp->setBOption("verbose", verbose);
158 	warp->process(image_in);
159 	
160 	image_out = new ImageGray();
161 	image_out->setBOption("verbose", verbose);
162 
163 	image_out->copyFrom(image_in->width, image_in->height, warp->seq_out->frames[0], "gray");
164 	image_out->save("warp.pgm");
165 
166 	delete [] ldm_out;
167 	delete [] ldm_in;
168 	delete file_in;
169 	delete file_out;
170 	delete warp;
171 	delete image_out;
172 	delete image_in;
173 
174 	return(0);
175 }