imagediff.cc

  1 const char *help = "\
  2 progname: imagediff.cc\n\
  3 code2html: This program computes the difference of 2 images.\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 "CmdLine.h"
 10 
 11 using namespace Torch;
 12 
 13 int main(int argc, char **argv)
 14 {
 15 	char *image_filename1;
 16 	char *image_filename2;
 17 	bool verbose;
 18 
 19 	// Construct the command line
 20   	CmdLine cmd;
 21 	cmd.setBOption("write log", false);
 22 	
 23   	// Put the help line at the beginning
 24   	cmd.info(help);
 25 
 26   	cmd.addText("\nArguments:");
 27   	cmd.addSCmdArg("image filename 1", &image_filename1, "image filename 1");
 28   	cmd.addSCmdArg("image filename 2", &image_filename2, "image filename 2");
 29   	cmd.addText("\nOptions:");
 30   	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
 31 
 32 	cmd.read(argc, argv);
 33 
 34   	ImageDiskXFile *image_file = NULL;
 35 	Image *grayimage1 = NULL;
 36 	Image *grayimage2 = NULL;
 37 
 38 	grayimage1 = new ImageGray();
 39 	grayimage1->setBOption("verbose", verbose);
 40 
 41 	grayimage2 = new ImageGray();
 42 	grayimage2->setBOption("verbose", verbose);
 43 
 44 	image_file = new xtprobeImageDiskXFile(image_filename1, "r");
 45 	grayimage1->loadImageXFile(image_file);
 46 	delete image_file;
 47 
 48 	image_file = new xtprobeImageDiskXFile(image_filename2, "r");
 49 	grayimage2->loadImageXFile(image_file);
 50 	delete image_file;
 51 
 52 	if(verbose)
 53 	{
 54 		print("Image 1 info:\n");
 55 		print("   width = %d\n", grayimage1->width);
 56 		print("   height = %d\n", grayimage1->height);
 57 		print("   format = %s (%d)\n", grayimage1->coding, grayimage1->n_planes);
 58 		print("Image 2 info:\n");
 59 		print("   width = %d\n", grayimage2->width);
 60 		print("   height = %d\n", grayimage2->height);
 61 		print("   format = %s (%d)\n", grayimage2->coding, grayimage2->n_planes);
 62 	}
 63 
 64 	if(grayimage1->width != grayimage2->width)
 65 		error("Images of different width");
 66 	
 67 	if(grayimage1->height != grayimage2->height)
 68 		error("Images of different height");
 69 
 70 	int n = grayimage1->width * grayimage1->height;
 71 	
 72 	real mse = 0.0;
 73 	
 74 	for(int i = 0 ; i < n ; i++)
 75 	{
 76 		real z = (real) (grayimage1->data[i] - grayimage2->data[i]) / 255.0;
 77 		mse += z*z;	
 78 	}
 79 	mse /= (real) n;
 80 
 81 	printf("MSE = %g\n", mse);
 82 	
 83 	mse = 0.0;
 84 
 85 	for(int i = 0 ; i < grayimage1->width ; i++)
 86 		for(int j = 0 ; j < grayimage1->height ; j++)
 87 		{
 88 			grayimage1->get(j,i)[0] = grayimage2->get(j,i)[0];
 89 			real z = (real) (grayimage1->get(j,i)[0] - grayimage2->get(j,i)[0]) / 255.0;
 90 			mse += z*z;	
 91 		}
 92 	mse /= (real) n;
 93 
 94 	printf("MSE = %g\n", mse);
 95 	
 96 	delete grayimage1;
 97 	delete grayimage2;
 98 
 99 	return(0);
100 }