Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TestCode.cpp
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2007-2008 Anael Orlinski
3 *
4 * This file is part of Panomatic.
5 *
6 * Panomatic is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * Panomatic is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Panomatic; if not, write to the Free Software
18 * <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "ImageImport.h"
22 #include "TestCode.h"
24 
25 // bresenham
26 
27 static int gen127()
28 {
29  return (int)((double)rand()*127/(double)RAND_MAX);
30 }
31 
32 
33 void drawLine(vigra::DRGBImage& img, int x0, int y0, int x1, int y1, vigra::RGBValue<int>& color)
34 {
35  bool steep = (abs(y1 - y0) > abs(x1 - x0));
36  if (steep)
37  {
38  std::swap(x0,y0);
39  std::swap(x1,y1);
40  }
41 
42  if (x0 > x1)
43  {
44  std::swap(x0, x1);
45  std::swap(y0, y1);
46  }
47 
48  int deltax = x1 - x0;
49  int deltay = abs(y1 - y0);
50  int error = -(deltax + 1) / 2;
51  int ystep;
52  int y = y0;
53 
54  if (y0 < y1)
55  {
56  ystep = 1;
57  }
58  else
59  {
60  ystep = -1;
61  }
62 
63  for(int x=x0; x<=x1; ++x)
64  {
65  if (steep)
66  {
67  img(y,x) = color;
68  }
69  else
70  {
71  img(x,y) = color;
72  }
73  error += deltay;
74  if (error >=0)
75  {
76  y += ystep;
77  error -= deltax;
78  }
79  }
80 }
81 
82 void TestCode::drawRansacMatches(std::string& i1, std::string& i2,
85  lfeat::Ransac& iRansac, bool iHalf)
86 {
87  double aDoubleFactor = 1.0;
88  if (iHalf)
89  {
90  aDoubleFactor = 2.0;
91  }
92 
93 
94  std::cout << "writing file outcomp.png ..." << std::endl;
95 
96  // write a side by side image with match pairs and
97  vigra::ImageImportInfo info1(i1.c_str());
98  vigra::ImageImportInfo info2(i2.c_str());
99 
100  vigra::DRGBImage out1(info1.width() * 2, info1.height());
101 
102  if ((info1.width() != info2.width()) || (info1.height() != info2.height()))
103  {
104  std::cout << "images of different size, skip write of test img" << std::endl;
105  return;
106  }
107 
108  if(info1.isGrayscale())
109  {
110  vigra::DImage aImageGrey(info1.width(), info1.height());
111  importImage(info1, destImage(aImageGrey));
112 
113  // copy left img
114  vigra::copyImage(aImageGrey.upperLeft(),
115  aImageGrey.lowerRight(),
116  vigra::DImage::Accessor(),
117  out1.upperLeft(),
118  vigra::DImage::Accessor());
119 
120  }
121  else
122  {
123  vigra::DRGBImage aImageRGB(info1.width(), info1.height());
124  if(info1.numExtraBands() == 1)
125  {
126  vigra::BImage aAlpha(info1.size());
127  //importImageAlpha(info1, destImage(aImageRGB), destImage(aAlpha));
128  }
129  else if (info1.numExtraBands() == 0)
130  {
131  vigra::importImage(info1, destImage(aImageRGB));
132  }
133 
134  // copy left img
135  vigra::copyImage(aImageRGB.upperLeft(),
136  aImageRGB.lowerRight(),
137  vigra::RGBToGrayAccessor<vigra::RGBValue<double> >(),
138  out1.upperLeft(),
139  vigra::DImage::Accessor());
140  }
141 
142  if(info2.isGrayscale())
143  {
144  vigra::DImage aImageGrey(info2.width(), info2.height());
145  importImage(info2, destImage(aImageGrey));
146 
147  // copy left img
148  vigra::copyImage(aImageGrey.upperLeft(),
149  aImageGrey.lowerRight(),
150  vigra::DImage::Accessor(),
151  out1.upperLeft() + vigra::Diff2D(info1.width(), 0),
152  vigra::DImage::Accessor());
153 
154  }
155  else
156  {
157  vigra::DRGBImage aImageRGB(info2.width(), info2.height());
158  if(info2.numExtraBands() == 1)
159  {
160  vigra::BImage aAlpha(info2.size());
161  //importImageAlpha(info2, destImage(aImageRGB), destImage(aAlpha));
162  }
163  else if (info2.numExtraBands() == 0)
164  {
165  vigra::importImage(info2, destImage(aImageRGB));
166  }
167 
168  // copy left img
169  vigra::copyImage(aImageRGB.upperLeft(),
170  aImageRGB.lowerRight(),
171  vigra::RGBToGrayAccessor<vigra::RGBValue<double> >(),
172  out1.upperLeft() + vigra::Diff2D(info1.width(), 0),
173  vigra::DImage::Accessor());
174  }
175 
176  for (size_t i = 0; i < iOK.size(); ++i)
177  {
178  lfeat::PointMatchPtr& aV = iOK[i];
179  vigra::RGBValue<int> color(gen127(), 255 , gen127());
180  drawLine(out1, aDoubleFactor * aV->_img1_x,
181  aDoubleFactor * aV->_img1_y,
182  aDoubleFactor * aV->_img2_x + info1.width(),
183  aDoubleFactor * aV->_img2_y, color);
184  //cout << "----------------------" << endl;
185  //cout << "x= " << aV->_img2_x + info1.width() << " y= " << aV->_img2_y << endl;
186  double x1p, y1p;
187  iRansac.transform(aV->_img1_x, aV->_img1_y, x1p, y1p);
188  //cout << "xp= " << x1p << " yp= " << y1p << endl;
189 
190  if (x1p <0)
191  {
192  x1p = 0;
193  }
194  if (y1p <0)
195  {
196  y1p = 0;
197  }
198 
199 
200  if (x1p > info1.width())
201  {
202  x1p=info1.width()-1;
203  }
204  if (y1p > info1.height())
205  {
206  y1p=info1.height()-1;
207  }
208 
209  vigra::RGBValue<int> color2(0, 255 , 255);
210  drawLine(out1, aDoubleFactor * aV->_img2_x + info1.width(),
211  aDoubleFactor * aV->_img2_y,
212  aDoubleFactor * x1p + info1.width(),
213  aDoubleFactor * y1p, color2);
214 
215  }
216 
217  for(size_t i=0; i<iNOK.size(); ++i)
218  {
219  lfeat::PointMatchPtr& aV = iNOK[i];
220  vigra::RGBValue<int> color(255, gen127() , gen127());
221  drawLine(out1, aDoubleFactor * aV->_img1_x,
222  aDoubleFactor * aV->_img1_y,
223  aDoubleFactor * aV->_img2_x + info1.width(),
224  aDoubleFactor * aV->_img2_y, color);
225  //cout << "----------------------" << endl;
226  //cout << "x= " << aV->_img2_x + info1.width() << " y= " << aV->_img2_y << endl;
227  double x1p, y1p;
228  iRansac.transform(aV->_img1_x, aV->_img1_y, x1p, y1p);
229  //cout << "xp= " << x1p << " yp= " << y1p << endl;
230 
231  if (x1p <0)
232  {
233  x1p = 0;
234  }
235  if (y1p <0)
236  {
237  y1p = 0;
238  }
239 
240  if (x1p > info1.width())
241  {
242  x1p=info1.width()-1;
243  }
244  if (y1p > info1.height())
245  {
246  y1p=info1.height()-1;
247  }
248 
249  vigra::RGBValue<int> color2(0, 255 , 255);
250  drawLine(out1, aDoubleFactor * aV->_img2_x + info1.width(),
251  aDoubleFactor * aV->_img2_y,
252  aDoubleFactor * x1p + info1.width(),
253  aDoubleFactor * y1p, color2);
254 
255  }
256 
257  exportImage(srcImageRange(out1), vigra::ImageExportInfo("outcomp.png"));
258 
259 
260 
261 }
262 
263 
static void swap(T &x, T &y)
Definition: svm.cpp:67
std::vector< PointMatchPtr > PointMatchVector_t
Definition: PointMatch.h:53
static int gen127()
Definition: TestCode.cpp:27
void transform(double iX, double iY, double &oX, double &oY)
vigra::pair< typename ROIImage< Image, Alpha >::image_traverser, typename ROIImage< Image, Alpha >::ImageAccessor > destImage(ROIImage< Image, Alpha > &img)
Definition: ROIImage.h:324
static void drawRansacMatches(std::string &i1, std::string &i2, lfeat::PointMatchVector_t &iOK, lfeat::PointMatchVector_t &iNOK, lfeat::Ransac &iRansac, bool iHalf)
Definition: TestCode.cpp:82
vigra::triple< typename ROIImage< Image, Mask >::image_const_traverser, typename ROIImage< Image, Mask >::image_const_traverser, typename ROIImage< Image, Mask >::ImageConstAccessor > srcImageRange(const ROIImage< Image, Mask > &img)
helper function for ROIImages
Definition: ROIImage.h:287
void copyImage(SrcImageIterator src_upperleft, SrcImageIterator src_lowerright, SrcAccessor src_acc, DestImageIterator dest_upperleft, DestAccessor dest_acc)
Definition: openmp_vigra.h:305
void drawLine(vigra::DRGBImage &img, int x0, int y0, int x1, int y1, vigra::RGBValue< int > &color)
Definition: TestCode.cpp:33
std::shared_ptr< PointMatch > PointMatchPtr
Definition: PointMatch.h:52