ddt.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
33 /*CISC 829 Final Project
34 CISC829 Final project: Image reconstruction using Data-Dependent triangulation (DDT)
35 Spring 2008
36 
37 The following is an implementation of the DDT algorithm presented in
38 "Image Reconstruction Using Data-Dependent Triangulation,", Xiaohua Yu, Bryan S. Morse, Thomas W. Sederberg, IEEE Computer Graphics and Applications, vol. 21, no. 3, pp. 62-68, May/Jun, 2001
39 
40 Gowri Somanath
41 Feng Li
42 CIS
43 University of Delaware.
44 */
45 #include "preh.h"
46 #include "cv.h"
47 #include "highgui.h"
48 #include <assert.h>
49 
50 #include <GL/glut.h>
51 
52 
53 GLuint mode;
54 IplImage* grab = NULL;
55 IplImage* res = NULL;
57 IplImage* src = NULL;
58 int ratio = 1;
59 
61 
62 //
63 // GLUT keypress function
64 //
65 void onKeyPress(unsigned char key, int x, int y) {
66 
67  if ( key == 'w' ) { // quit the program.
68  mode = GL_LINE_LOOP;
69  }
70  else if ( key == 'p' ) { // quit the program.
71  mode = GL_POINTS;
72  }
73  else if ( key == 's' ) { // quit the program.
74  glShadeModel(GL_SMOOTH); // Set Smooth Shading
75  mode = GL_POLYGON;
76  }
77  else if ( key == 'f' ) { // quit the program.
78  glShadeModel(GL_FLAT); // Set Smooth Shading
79  mode = GL_POLYGON;
80  }
81  else if ( key == 'q' ) { // quit the program.
82 // delete data;
83  exit(0);
84  }
85  else if ( key == 'r' ) { // reset the data
86 // delete data;
87 // data = new WavefrontObj( data_name );
88  }
89  else if (key == 'i') {
90  //Subdivision_method_3::Loop_subdivision(P,1);
91  }
92  else if(key == 'd') {
93 
94  }
95  else if(key == 'g') {
96 
97  glReadPixels(0, 0, grab->width, grab->height,
98  GL_RGB, GL_UNSIGNED_BYTE, grab->imageData);
99  cvSaveImage("res.bmp", grab);
100  }
101 
102  glutPostRedisplay();
103 }
104 
105 
106 void display()
107 {
108  double x = 0, y = 0, color = 0;
109 
110  // draw
111  glClear (GL_COLOR_BUFFER_BIT);
112 
113  All_faces_iterator fai;
114  int index = 0;
115  Vertex_handle vh; //f.vertex ( int i)
116  for(fai = dt.all_faces_begin(); fai != dt.all_faces_end(); fai++) {
117  if(dt.is_infinite(fai))
118  continue;
119 
120  glBegin(mode);
121  for(int k = 0; k < 3; k ++) {
122  Point pt = fai->vertex(k)->point();
123  color = ((uchar*)(src->imageData +
124  int(pt.y()/ratio) * src->widthStep))[int(pt.x()/ratio)] / 255.0f;
125  if(mode == GL_LINE_LOOP)
126  glColor3f(0.5, 0.5, 1);
127  else
128  glColor3f(color, color, color);
129  x = pt.x();
130  y = pt.y();
131  glVertex2d(x, y);
132  }
133  glEnd();
134 
135  }
136 
137  glFlush();
138  glutSwapBuffers();
139 }
140 
141 
142 void init (void)
143 {
144  glClearColor (0.0, 0.0, 0.0, 0.0);
145  glShadeModel (GL_FLAT);
146 }
147 
148 
149 void reshape(int w, int h)
150 {
151  glViewport(0, 0, (GLsizei) w, (GLsizei) h);
152  glMatrixMode(GL_PROJECTION);
153  glLoadIdentity();
154  gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);
155 }
156 
157 
158 
159 
161 
162 // fill the geometry trait using the src image
163 //int wraper(Delaunay* dt, Point_value_map* values, IplImage* src, int ratio)
164 int wraper( Point_value_map* values, IplImage* src, int ratio)
165 {
166  assert(src->nChannels == 1);
167  int tmp = 0;
168  Point pt;
169 
170  for(int j = 0; j < src->height; j ++) {
171  for(int i = 0; i < src->width; i ++) {
172  tmp = int(((uchar*)(src->imageData + j * src->widthStep))[i]);
173  pt = Point(ratio * i, ratio * j);
174  dt.insert(pt);
175  values->insert(std::make_pair(pt, tmp));
176  }
177  }
178 
179  // some statistic info
180  std::cout << "width, height of source img: " << src->width << ", " << src->height << std::endl;
181  std::cout << "upsample ratio: " << ratio << std::endl;
182  std::cout << "# of vertices: " << dt.number_of_vertices() << std::endl;
183 
184  return 0;
185 }
186 
188 {
189  // opengl drawing
190  int w = res->width;
191  int h = res->height;
192 
193  int argcg = 1;
194  char* argvg[] = {"interpolation"};
195  glutInit(&argcg, argvg);
196  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
197  glutInitWindowSize(w,h);
198  glutInitWindowPosition(100,100);
199  glutCreateWindow("Interpolation");
200  //grab = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 3);
201 
202  init();
203  glutDisplayFunc(display);
204  glutReshapeFunc(reshape);
205  glutKeyboardFunc(onKeyPress);
206  glShadeModel(GL_SMOOTH); // Set Smooth Shading
207  mode = GL_POLYGON;
208  glutMainLoop();
209 
210  return 0;
211 }
212 
213 
214 // data dependent triangulation
215 IplImage* ddt(IplImage* src, int ratio)
216 {
217  assert(src->nChannels == 1);
218 
219  Point_value_map values;
220  wraper(&values, src, ratio); // generate the initial triangulation
221  std::cout << "Done Delaunay " << std::endl;
222  char n[15];
223 
224 for(int itr=0;itr<4;itr++)
225 {
226  std::cout<<"Iteration "<<itr<<std::endl;
227  EdgeVector edges(0);
228  //edges=(EdgeVector *)malloc(sizeof(Edge)*v);
229  EdgeVectorIterator evi;
230  Edge_iterator ei=dt.finite_edges_begin();
231  //make_heap(edges.begin(), edges.end()) ;
232  do
233  {
234 
235  //edges[i++]=Edge(ei->first,ei->second);
236  Face_handle f=ei->first;
237  if(dt.is_infinite(f))
238  continue;
239 
240  int i=ei->second;
241  if(!dt.is_edge(f->vertex(f->cw(i)),f->vertex(f->ccw(i))))
242  continue;
243 
244  if(dt.is_infinite(f->vertex(f->cw(i))))
245  continue;
246  if(dt.is_infinite(f->vertex(f->ccw(i))))
247  continue;
248  edges.push_back(GEdge(f->vertex(f->cw(i)),f->vertex(f->ccw(i))));
249  push_heap(edges.begin(), edges.end()) ;
251  }while(++ei!=dt.finite_edges_end());
252  //make_heap(edges.begin(), edges.end()) ;
253 // push_heap(edges.begin(), edges.end()) ;
254  //sort_heap(edges.begin(), edges.end()) ;
255 
256  int cnt=0;
257  for(evi=edges.begin();evi!=edges.end();evi++)
258  {
259  cnt++;
260  printf("edge %d\n",cnt);
261 
262  Vertex_handle v1=evi->first;
263 
264  Vertex_handle v2=evi->second;
265 
266  pop_heap(edges.begin(), edges.end()) ;
267 
268  Face_handle f1;
269 
270  int i,j;
271  if(!v1->is_valid() || !v2->is_valid() || dt.is_infinite(v1) || dt.is_infinite(v2))
272  continue;
273  //std::cout << "--- " <<cnt << std::endl;
274  //std::cout << "v1:" <<v1->point()<<" v2:" <<v2->point()<< std::endl;
275 
276  if(!dt.is_edge(v1,v2))
277  continue;
278 
279  if(dt.is_edge ( v1, v2,f1,i))
280  {
281 
282  if(!dt.is_infinite(f1))
283  {
284 
285  Face_handle f2;
286  if(dt.is_edge(v2,v1,f2,j) && !dt.is_infinite(f2) && f1->has_neighbor(f2))
287  {
288 
289  GPoint pts[]={GPoint(v1->point().x(),v1->point().y()),GPoint(f1->vertex(i)->point().x(),f1->vertex(i)->point().y()),GPoint(v2->point().x(),v2->point().y()),GPoint(f2->vertex(j)->point().x(),f2->vertex(j)->point().y())};
290 
291  Polygon_2 pgn(pts, pts+4);
292 
293  if(pgn.is_convex())
294  {
295 
296  processForCost(f1,i,values,&dt);
297 
298  }
299  }
300  }
301  }
302  //std::cout << "--- " << std::endl;
303  //else
304  // std::cout << "rubbish in the way " << std::endl;
305  //processForCost(Face_handle f1,int i,Point_value_map values,Delaunay *dt);
306  //processForCost(f1,i,values,&dt);
307  }
308 
309 
310 }
311 
312  std::cout << "Done DDT " << std::endl;
313 
314  // interpolation
315  res = cvCreateImage(cvSize((src->width - 1) * ratio + 1,
316  (src->height - 1) * ratio + 1), IPL_DEPTH_8U, 1);
317  grab = cvCreateImage(cvGetSize(res), IPL_DEPTH_8U, 3);
318  cvZero(res);
319  //interpolation(&dt, &values, res);
321  std::cout << "Done interpolation " << std::endl;
322 
323  return res;
324  //return NULL;
325 }
326 
327 
328 int main(int argc, char** argv)
329 {
330  char* filename = NULL;
331  ratio = 1;
332 
333  if(argc > 2) {
334  filename = argv[1];
335  ratio = atoi(argv[2]);
336  }
337  else {
338  filename = "test.bmp";
339  ratio = 4;
340  }
341 
342  // load image
343  src = cvLoadImage(filename, 0); // load the image as gray image
344 
345 
346  IplImage* res = ddt(src, ratio);
347  cvSaveImage("ddt.bmp", res);
348 
349 
350  return 0;
351 }
void onKeyPress(unsigned char key, int x, int y)
Definition: ddt.cpp:65
IplImage * ddt(IplImage *src, int ratio)
Definition: ddt.cpp:215
K1::Point_2 GPoint
Definition: preh.h:106
int ratio
Definition: ddt.cpp:58
CDT::All_faces_iterator All_faces_iterator
CGAL::Polygon_2< K1 > Polygon_2
Definition: preh.h:107
int wraper(Point_value_map *values, IplImage *src, int ratio)
Definition: ddt.cpp:164
std::pair< Vertex_handle, Vertex_handle > GEdge
Definition: preh.h:91
Delaunay::Finite_edges_iterator Edge_iterator
Definition: preh.h:83
EdgeVector::iterator EdgeVectorIterator
Definition: preh.h:98
IplImage * grab
Definition: ddt.cpp:54
bool processForCost(Face_handle f1, int i, Point_value_map values, Delaunay *dt)
Definition: otherfns.cpp:118
void display()
Definition: ddt.cpp:106
int main(int argc, char **argv)
Definition: ddt.cpp:328
Ss::Vertex_handle Vertex_handle
header for a DDT example
std::vector< GEdge > EdgeVector
Definition: preh.h:97
std::map< Point, Coord_type, K::Less_xy_2 > Point_value_map
Definition: preh.h:89
IplImage * res
Definition: ddt.cpp:55
IplImage * src
Definition: ddt.cpp:57
void init(void)
Definition: ddt.cpp:142
int interpolation_gl()
Definition: ddt.cpp:187
K::Point_2 Point
Definition: preh.h:66
void reshape(int w, int h)
Definition: ddt.cpp:149
Delaunay dt
Definition: ddt.cpp:56
CGAL::Delaunay_triangulation_2< K > Delaunay
Definition: preh.h:70
GLuint mode
Definition: ddt.cpp:53
CDT::Face_handle Face_handle


polygon_primitives_extraction
Author(s): Miguel Oliveira
autogenerated on Mon Mar 2 2015 01:32:42