TestCode.cpp

Go to the documentation of this file.
00001 /*
00002 * Copyright (C) 2007-2008 Anael Orlinski
00003 *
00004 * This file is part of Panomatic.
00005 *
00006 * Panomatic 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 * Panomatic 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
00017 * along with Panomatic; if not, write to the Free Software
00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 */
00020 
00021 #include "ImageImport.h"
00022 #include "TestCode.h"
00023 #include <localfeatures/RansacFiltering.h>
00024 
00025 #include <boost/foreach.hpp>
00026 
00027 
00028 
00029 using namespace vigra;
00030 using namespace lfeat;
00031 
00032 // bresenham
00033 
00034 static int gen127()
00035 {
00036     return (int)((double)rand()*127/(double)RAND_MAX);
00037 }
00038 
00039 
00040 void drawLine(vigra::DRGBImage& img, int x0, int y0, int x1, int y1, vigra::RGBValue<int>& color)
00041 {
00042     bool steep = (abs(y1 - y0) > abs(x1 - x0));
00043     if (steep)
00044     {
00045         std::swap(x0,y0);
00046         std::swap(x1,y1);
00047     }
00048 
00049     if (x0 > x1)
00050     {
00051         std::swap(x0, x1);
00052         std::swap(y0, y1);
00053     }
00054 
00055     int deltax = x1 - x0;
00056     int deltay = abs(y1 - y0);
00057     int error = -(deltax + 1) / 2;
00058     int ystep;
00059     int y = y0;
00060 
00061     if (y0 < y1)
00062     {
00063         ystep = 1;
00064     }
00065     else
00066     {
00067         ystep = -1;
00068     }
00069 
00070     for(int x=x0; x<=x1; ++x)
00071     {
00072         if (steep)
00073         {
00074             img(y,x) = color;
00075         }
00076         else
00077         {
00078             img(x,y) = color;
00079         }
00080         error += deltay;
00081         if (error >=0)
00082         {
00083             y += ystep;
00084             error -= deltax;
00085         }
00086     }
00087 }
00088 
00089 void TestCode::drawRansacMatches(std::string& i1, std::string& i2,
00090                                  lfeat::PointMatchVector_t& iOK,
00091                                  lfeat::PointMatchVector_t& iNOK,
00092                                  Ransac& iRansac, bool iHalf)
00093 {
00094     double aDoubleFactor = 1.0;
00095     if (iHalf)
00096     {
00097         aDoubleFactor = 2.0;
00098     }
00099 
00100 
00101     std::cout << "writing file outcomp.png ..." << endl;
00102 
00103     // write a side by side image with match pairs and
00104     ImageImportInfo info1(i1.c_str());
00105     ImageImportInfo info2(i2.c_str());
00106 
00107     vigra::DRGBImage out1(info1.width() * 2, info1.height());
00108 
00109     if ((info1.width() != info2.width()) || (info1.height() != info2.height()))
00110     {
00111         std::cout << "images of different size, skip write of test img" << endl;
00112         return;
00113     }
00114 
00115     if(info1.isGrayscale())
00116     {
00117         vigra::DImage aImageGrey(info1.width(), info1.height());
00118         importImage(info1, destImage(aImageGrey));
00119 
00120         // copy left img
00121         vigra::copyImage(aImageGrey.upperLeft(),
00122                          aImageGrey.lowerRight(),
00123                          DImage::Accessor(),
00124                          out1.upperLeft(),
00125                          DImage::Accessor());
00126 
00127     }
00128     else
00129     {
00130         vigra::DRGBImage aImageRGB(info1.width(), info1.height());
00131         if(info1.numExtraBands() == 1)
00132         {
00133             vigra::BImage aAlpha(info1.size());
00134             //importImageAlpha(info1, destImage(aImageRGB), destImage(aAlpha));
00135         }
00136         else if (info1.numExtraBands() == 0)
00137         {
00138             vigra::importImage(info1, destImage(aImageRGB));
00139         }
00140 
00141         // copy left img
00142         vigra::copyImage(aImageRGB.upperLeft(),
00143                          aImageRGB.lowerRight(),
00144                          RGBToGrayAccessor<RGBValue<double> >(),
00145                          out1.upperLeft(),
00146                          DImage::Accessor());
00147     }
00148 
00149     if(info2.isGrayscale())
00150     {
00151         vigra::DImage aImageGrey(info2.width(), info2.height());
00152         importImage(info2, destImage(aImageGrey));
00153 
00154         // copy left img
00155         vigra::copyImage(aImageGrey.upperLeft(),
00156                          aImageGrey.lowerRight(),
00157                          DImage::Accessor(),
00158                          out1.upperLeft() + vigra::Diff2D(info1.width(), 0),
00159                          DImage::Accessor());
00160 
00161     }
00162     else
00163     {
00164         vigra::DRGBImage aImageRGB(info2.width(), info2.height());
00165         if(info2.numExtraBands() == 1)
00166         {
00167             vigra::BImage aAlpha(info2.size());
00168             //importImageAlpha(info2, destImage(aImageRGB), destImage(aAlpha));
00169         }
00170         else if (info2.numExtraBands() == 0)
00171         {
00172             vigra::importImage(info2, destImage(aImageRGB));
00173         }
00174 
00175         // copy left img
00176         vigra::copyImage(aImageRGB.upperLeft(),
00177                          aImageRGB.lowerRight(),
00178                          RGBToGrayAccessor<RGBValue<double> >(),
00179                          out1.upperLeft() + vigra::Diff2D(info1.width(), 0),
00180                          DImage::Accessor());
00181     }
00182 
00183     BOOST_FOREACH(PointMatchPtr& aV, iOK)
00184     {
00185         vigra::RGBValue<int> color(gen127(), 255 , gen127());
00186         drawLine(out1,  aDoubleFactor * aV->_img1_x,
00187                  aDoubleFactor * aV->_img1_y,
00188                  aDoubleFactor *  aV->_img2_x + info1.width(),
00189                  aDoubleFactor * aV->_img2_y, color);
00190         //cout << "----------------------" << endl;
00191         //cout << "x= " << aV->_img2_x + info1.width() << " y= " << aV->_img2_y << endl;
00192         double x1p, y1p;
00193         iRansac.transform(aV->_img1_x, aV->_img1_y, x1p, y1p);
00194         //cout << "xp= " << x1p << " yp= " << y1p << endl;
00195 
00196         if (x1p <0)
00197         {
00198             x1p = 0;
00199         }
00200         if (y1p <0)
00201         {
00202             y1p = 0;
00203         }
00204 
00205 
00206         if (x1p > info1.width())
00207         {
00208             x1p=info1.width()-1;
00209         }
00210         if (y1p > info1.height())
00211         {
00212             y1p=info1.height()-1;
00213         }
00214 
00215         vigra::RGBValue<int> color2(0, 255 , 255);
00216         drawLine(out1,  aDoubleFactor * aV->_img2_x + info1.width(),
00217                  aDoubleFactor * aV->_img2_y,
00218                  aDoubleFactor * x1p         + info1.width(),
00219                  aDoubleFactor * y1p, color2);
00220 
00221     }
00222 
00223     BOOST_FOREACH(PointMatchPtr& aV, iNOK)
00224     {
00225         vigra::RGBValue<int> color(255, gen127() , gen127());
00226         drawLine(out1,  aDoubleFactor * aV->_img1_x,
00227                  aDoubleFactor * aV->_img1_y,
00228                  aDoubleFactor * aV->_img2_x + info1.width(),
00229                  aDoubleFactor * aV->_img2_y, color);
00230         //cout << "----------------------" << endl;
00231         //cout << "x= " << aV->_img2_x + info1.width() << " y= " << aV->_img2_y << endl;
00232         double x1p, y1p;
00233         iRansac.transform(aV->_img1_x, aV->_img1_y, x1p, y1p);
00234         //cout << "xp= " << x1p << " yp= " << y1p << endl;
00235 
00236         if (x1p <0)
00237         {
00238             x1p = 0;
00239         }
00240         if (y1p <0)
00241         {
00242             y1p = 0;
00243         }
00244 
00245         if (x1p > info1.width())
00246         {
00247             x1p=info1.width()-1;
00248         }
00249         if (y1p > info1.height())
00250         {
00251             y1p=info1.height()-1;
00252         }
00253 
00254         vigra::RGBValue<int> color2(0, 255 , 255);
00255         drawLine(out1,  aDoubleFactor * aV->_img2_x + info1.width(),
00256                  aDoubleFactor * aV->_img2_y,
00257                  aDoubleFactor * x1p         + info1.width(),
00258                  aDoubleFactor * y1p, color2);
00259 
00260     }
00261 
00262     exportImage(srcImageRange(out1), vigra::ImageExportInfo("outcomp.png"));
00263 
00264 
00265 
00266 }
00267 
00268 

Generated on 22 Oct 2014 for Hugintrunk by  doxygen 1.4.7