00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #include "preh.h"
00046 #include "cv.h"
00047 #include "highgui.h"
00048 #include <assert.h>
00049
00050 #include <GL/glut.h>
00051
00052
00053 GLuint mode;
00054 IplImage* grab = NULL;
00055 IplImage* res = NULL;
00056 Delaunay dt;
00057 IplImage* src = NULL;
00058 int ratio = 1;
00059
00061
00062
00063
00064
00065 void onKeyPress(unsigned char key, int x, int y) {
00066
00067 if ( key == 'w' ) {
00068 mode = GL_LINE_LOOP;
00069 }
00070 else if ( key == 'p' ) {
00071 mode = GL_POINTS;
00072 }
00073 else if ( key == 's' ) {
00074 glShadeModel(GL_SMOOTH);
00075 mode = GL_POLYGON;
00076 }
00077 else if ( key == 'f' ) {
00078 glShadeModel(GL_FLAT);
00079 mode = GL_POLYGON;
00080 }
00081 else if ( key == 'q' ) {
00082
00083 exit(0);
00084 }
00085 else if ( key == 'r' ) {
00086
00087
00088 }
00089 else if (key == 'i') {
00090
00091 }
00092 else if(key == 'd') {
00093
00094 }
00095 else if(key == 'g') {
00096
00097 glReadPixels(0, 0, grab->width, grab->height,
00098 GL_RGB, GL_UNSIGNED_BYTE, grab->imageData);
00099 cvSaveImage("res.bmp", grab);
00100 }
00101
00102 glutPostRedisplay();
00103 }
00104
00105
00106 void display()
00107 {
00108 double x = 0, y = 0, color = 0;
00109
00110
00111 glClear (GL_COLOR_BUFFER_BIT);
00112
00113 All_faces_iterator fai;
00114 int index = 0;
00115 Vertex_handle vh;
00116 for(fai = dt.all_faces_begin(); fai != dt.all_faces_end(); fai++) {
00117 if(dt.is_infinite(fai))
00118 continue;
00119
00120 glBegin(mode);
00121 for(int k = 0; k < 3; k ++) {
00122 Point pt = fai->vertex(k)->point();
00123 color = ((uchar*)(src->imageData +
00124 int(pt.y()/ratio) * src->widthStep))[int(pt.x()/ratio)] / 255.0f;
00125 if(mode == GL_LINE_LOOP)
00126 glColor3f(0.5, 0.5, 1);
00127 else
00128 glColor3f(color, color, color);
00129 x = pt.x();
00130 y = pt.y();
00131 glVertex2d(x, y);
00132 }
00133 glEnd();
00134
00135 }
00136
00137 glFlush();
00138 glutSwapBuffers();
00139 }
00140
00141
00142 void init (void)
00143 {
00144 glClearColor (0.0, 0.0, 0.0, 0.0);
00145 glShadeModel (GL_FLAT);
00146 }
00147
00148
00149 void reshape(int w, int h)
00150 {
00151 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
00152 glMatrixMode(GL_PROJECTION);
00153 glLoadIdentity();
00154 gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);
00155 }
00156
00157
00158
00159
00161
00162
00163
00164 int wraper( Point_value_map* values, IplImage* src, int ratio)
00165 {
00166 assert(src->nChannels == 1);
00167 int tmp = 0;
00168 Point pt;
00169
00170 for(int j = 0; j < src->height; j ++) {
00171 for(int i = 0; i < src->width; i ++) {
00172 tmp = int(((uchar*)(src->imageData + j * src->widthStep))[i]);
00173 pt = Point(ratio * i, ratio * j);
00174 dt.insert(pt);
00175 values->insert(std::make_pair(pt, tmp));
00176 }
00177 }
00178
00179
00180 std::cout << "width, height of source img: " << src->width << ", " << src->height << std::endl;
00181 std::cout << "upsample ratio: " << ratio << std::endl;
00182 std::cout << "# of vertices: " << dt.number_of_vertices() << std::endl;
00183
00184 return 0;
00185 }
00186
00187 int interpolation_gl()
00188 {
00189
00190 int w = res->width;
00191 int h = res->height;
00192
00193 int argcg = 1;
00194 char* argvg[] = {"interpolation"};
00195 glutInit(&argcg, argvg);
00196 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
00197 glutInitWindowSize(w,h);
00198 glutInitWindowPosition(100,100);
00199 glutCreateWindow("Interpolation");
00200
00201
00202 init();
00203 glutDisplayFunc(display);
00204 glutReshapeFunc(reshape);
00205 glutKeyboardFunc(onKeyPress);
00206 glShadeModel(GL_SMOOTH);
00207 mode = GL_POLYGON;
00208 glutMainLoop();
00209
00210 return 0;
00211 }
00212
00213
00214
00215 IplImage* ddt(IplImage* src, int ratio)
00216 {
00217 assert(src->nChannels == 1);
00218
00219 Point_value_map values;
00220 wraper(&values, src, ratio);
00221 std::cout << "Done Delaunay " << std::endl;
00222 char n[15];
00223
00224 for(int itr=0;itr<4;itr++)
00225 {
00226 std::cout<<"Iteration "<<itr<<std::endl;
00227 EdgeVector edges(0);
00228
00229 EdgeVectorIterator evi;
00230 Edge_iterator ei=dt.finite_edges_begin();
00231
00232 do
00233 {
00234
00235
00236 Face_handle f=ei->first;
00237 if(dt.is_infinite(f))
00238 continue;
00239
00240 int i=ei->second;
00241 if(!dt.is_edge(f->vertex(f->cw(i)),f->vertex(f->ccw(i))))
00242 continue;
00243
00244 if(dt.is_infinite(f->vertex(f->cw(i))))
00245 continue;
00246 if(dt.is_infinite(f->vertex(f->ccw(i))))
00247 continue;
00248 edges.push_back(GEdge(f->vertex(f->cw(i)),f->vertex(f->ccw(i))));
00249 push_heap(edges.begin(), edges.end()) ;
00251 }while(++ei!=dt.finite_edges_end());
00252
00253
00254
00255
00256 int cnt=0;
00257 for(evi=edges.begin();evi!=edges.end();evi++)
00258 {
00259 cnt++;
00260 printf("edge %d\n",cnt);
00261
00262 Vertex_handle v1=evi->first;
00263
00264 Vertex_handle v2=evi->second;
00265
00266 pop_heap(edges.begin(), edges.end()) ;
00267
00268 Face_handle f1;
00269
00270 int i,j;
00271 if(!v1->is_valid() || !v2->is_valid() || dt.is_infinite(v1) || dt.is_infinite(v2))
00272 continue;
00273
00274
00275
00276 if(!dt.is_edge(v1,v2))
00277 continue;
00278
00279 if(dt.is_edge ( v1, v2,f1,i))
00280 {
00281
00282 if(!dt.is_infinite(f1))
00283 {
00284
00285 Face_handle f2;
00286 if(dt.is_edge(v2,v1,f2,j) && !dt.is_infinite(f2) && f1->has_neighbor(f2))
00287 {
00288
00289 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())};
00290
00291 Polygon_2 pgn(pts, pts+4);
00292
00293 if(pgn.is_convex())
00294 {
00295
00296 processForCost(f1,i,values,&dt);
00297
00298 }
00299 }
00300 }
00301 }
00302
00303
00304
00305
00306
00307 }
00308
00309
00310 }
00311
00312 std::cout << "Done DDT " << std::endl;
00313
00314
00315 res = cvCreateImage(cvSize((src->width - 1) * ratio + 1,
00316 (src->height - 1) * ratio + 1), IPL_DEPTH_8U, 1);
00317 grab = cvCreateImage(cvGetSize(res), IPL_DEPTH_8U, 3);
00318 cvZero(res);
00319
00320 interpolation_gl();
00321 std::cout << "Done interpolation " << std::endl;
00322
00323 return res;
00324
00325 }
00326
00327
00328 int main(int argc, char** argv)
00329 {
00330 char* filename = NULL;
00331 ratio = 1;
00332
00333 if(argc > 2) {
00334 filename = argv[1];
00335 ratio = atoi(argv[2]);
00336 }
00337 else {
00338 filename = "test.bmp";
00339 ratio = 4;
00340 }
00341
00342
00343 src = cvLoadImage(filename, 0);
00344
00345
00346 IplImage* res = ddt(src, ratio);
00347 cvSaveImage("ddt.bmp", res);
00348
00349
00350 return 0;
00351 }