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

Generated on 27 Jun 2016 for Hugintrunk by  doxygen 1.4.7