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

Generated on 2 Sep 2015 for Hugintrunk by  doxygen 1.4.7