image_warp.cc

Go to the documentation of this file.
00001 /*
00002 
00003     This file is part of the FAST-ER machine learning system.
00004     Copyright (C) 2008  Edward Rosten and Los Alamos National Laboratory
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License along
00017     with this program; if not, write to the Free Software Foundation, Inc.,
00018     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00019 */
00020 /**
00021 \file image_warp.cc Main file for the image_warp executable.
00022 
00023 \section wpUsage Usage
00024 
00025 <code> image_warp [--num NUM_IMAGES] [--dir DIR] [--type TYPE] [--out OUT_DIR] [--stub OUT_STUB]</code>
00026 
00027 \section Description
00028 
00029 Loads a dataset (NUM, DIR and TYPE specify the dataset according to  ::load_data)
00030 and warp every image to look like every other image. The output is placed in
00031 the image <code>./dir/warp_TO_FROM.jpg</code>. You need to create the output 
00032 directory yourself.
00033 
00034 By flipping through the images with the same value of TO, you can see the
00035 quality of alignment within a dataset.
00036 
00037 */
00038 
00039 
00040 #include <iostream>
00041 #include <cvd/image_io.h>
00042 #include <cvd/image_interpolate.h>
00043 #include <gvars3/instances.h>
00044 #include <tag/printf.h>
00045 #include <tag/stdpp.h>
00046 
00047 #include "load_data.h"
00048 #include "utility.h"
00049 
00050 using namespace std;
00051 using namespace CVD;
00052 using namespace tag;
00053 using namespace GVars3;
00054 using namespace TooN;
00055 
00056 ///Warp one image to look like another, using bilinear interpolation
00057 ///@param in The image to warp
00058 ///@param warp  The warp to use to warp the image
00059 ///@return The warped image
00060 Image<byte> warp_image(const Image<byte>& in, const Image<array<float, 2> >& warp)
00061 {
00062     Image<byte> ret(in.size(), 0);
00063 
00064     image_interpolate<Interpolate::Bilinear, byte> interp(in);
00065 
00066     for(int y=0; y < ret.size().y; y++)
00067         for(int x=0; x < ret.size().x; x++)
00068         {
00069             if(warp[y][x][0] != -1 && interp.in_image(Vec(warp[y][x])))
00070                 ret[y][x] = interp[Vec(warp[y][x])];
00071         }
00072 
00073     return ret;
00074 }
00075 
00076 ///Driving function
00077 ///@param argc Number of command line arguments
00078 ///@param argv Commandline argument list
00079 int main(int argc, char** argv)
00080 {
00081     try
00082     {
00083         //Load command line arguments
00084         GUI.parseArguments(argc, argv);
00085 
00086         vector<Image<byte> > images;
00087         vector<vector<Image<array<float, 2> > > > warps;
00088 
00089         //Extract arguments relavent to loading a dataset
00090         int n = GV3::get<int>("num", 2, 1);
00091         string dir = GV3::get<string>("dir", "./", 1);
00092         string format = GV3::get<string>("type", "cambridge", 1);
00093 
00094         //Load the dataset
00095         rpair(images, warps) = load_data(dir, n, format);
00096 
00097         //Generate the output printf string 
00098         string out = GV3::get<string>("out", "./out/", 1) + "/" + GV3::get<string>("stub", "warped_%i_%i.jpg", 1);
00099 
00100         //Warp every image to look like every other image
00101         //where this makes sense.
00102         for(int to = 0; to < n; to++)
00103             for(int from=0; from < n; from ++)
00104                 if(from != to)
00105                 {
00106                     Image<byte> w = warp_image(images[from], warps[to][from]);
00107                     img_save(w, sPrintf(out, to, from));
00108 
00109                     cout << "Done " << from << " -> " << to << endl;
00110                 }
00111                 else
00112                 {
00113                     img_save(images[from], sPrintf(out, to, from));
00114                 }
00115     }
00116     catch(Exceptions::All e)
00117     {
00118         cerr << "Error: " << e.what << endl;
00119     }   
00120 }

Generated on Mon Mar 2 12:47:12 2009 for FAST-ER by  doxygen 1.5.3