Advertisement
Guest User

Untitled

a guest
May 24th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.01 KB | None | 0 0
  1. // display the difference between adjacent video frames (press any key to exit)
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <opencv2/core/core.hpp>
  6. #include <opencv2/highgui/highgui.hpp>
  7. #include <opencv2/imgproc/imgproc.hpp>
  8. #include <opencv2/features2d/features2d.hpp>
  9.  
  10. #include <GL/gl.h>
  11. #include <GL/freeglut.h>
  12. //#include <GL/glm.h>
  13. //#include <freeglut.h>
  14. //#include <opencv2/nonfree/features2d.hpp>
  15. #define KEY_ESCAPE 27
  16. using namespace cv;
  17.  
  18. typedef Mat Image ;
  19. typedef Mat View;
  20.  
  21. typedef struct {
  22.   float x;
  23.   float y;
  24.   float z;
  25. } P;
  26.  
  27. typedef struct {
  28.   float r;
  29.   float b;
  30.   float g;
  31. } Color;
  32.  
  33. typedef struct {
  34.   P a;
  35.   P b;
  36.   P c;
  37.   Color color;
  38. } Triangle;
  39.  
  40. typedef struct {
  41.   Triangle *members;
  42.   int size;
  43. } Model;
  44.  
  45. typedef struct {
  46.   Model *members;
  47.   int size;
  48. } Models;
  49.  
  50. Model start_model;
  51. Model current_model;
  52. View current_view;
  53. Color red,blue,green;
  54.  
  55. void printModel(Model m) {
  56.   int i;
  57.   printf("model:\n");
  58.   for (i=0;i<m.size;i++) {
  59.     Triangle t = m.members[i];
  60.     printf("\ttriangle:\n");
  61.     printf(  "\t\ta =     (%f %f %f)"
  62.            "\n\t\tb =     (%f %f %f)"
  63.            "\n\t\tc =     (%f %f %f)"
  64.            "\n\t\tcolor = [%f %f %f]\n",
  65.            t.a.x, t.a.y, t.a.z,
  66.            t.b.x, t.b.y, t.b.z,
  67.            t.c.x, t.c.y, t.c.z,
  68.            t.color.r, t.color.b, t.color.g
  69.           );
  70.   }
  71. }
  72.  
  73. void initStartModel(){
  74.   //&start_model = malloc(sizeof(Model))
  75.   static Color r = {1.0f,0.0f,0.0f};
  76.   red = r;
  77.   static Color b = {0.0f,1.0f,0.0f};
  78.   blue = b;
  79.   static Color g = {0.0f,0.0f,1.0f};
  80.   green = g;
  81.   static P p1 = {0.0f,0.0f,0.0f};
  82.   static P p2 = {0.0f,1.0f,0.0f};
  83.   static P p3 = {1.0f,0.0f,0.0f};
  84.   static Triangle t0 = {p1,p2,p3,red};
  85.   static P p1_ = {0.0f,1.0f,1.0f};
  86.   static P p2_ = {0.0f,1.0f,0.0f};
  87.   static P p3_ = {0.0f,0.0f,0.0f};
  88.   static Triangle t1 = {p1_,p2_,p3_,blue};
  89.   static Triangle* ts = (Triangle*)malloc(2*sizeof(Triangle));
  90.   ts[0] = t0;
  91.   ts[1] = t1;
  92.   start_model.members = ts;
  93.   start_model.size = 2;
  94.   printModel(start_model);
  95.   current_model = start_model;
  96. }
  97.  
  98. typedef struct {
  99.   int width;
  100.   int height;
  101.   char* title;
  102.   float field_of_view_angle;
  103.   float z_near;
  104.   float z_far;
  105. } glutWindow;
  106.  
  107. glutWindow win;
  108.  
  109. Image glutTakeCVImage() {
  110.   // take a picture within glut and return formatted
  111.   // for use in openCV
  112.   int width = win.width;
  113.   int height = win.height;
  114.   unsigned char* buffer = new unsigned char[width*height*3];
  115.   glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
  116.   Image img(height, width, CV_8UC3, buffer);
  117.   Image flipped_img;
  118.   flip(img,flipped_img,0);
  119.   Image BGR_img;
  120.   cvtColor(flipped_img,BGR_img, COLOR_RGB2BGR);
  121.   return BGR_img;
  122. }
  123.  
  124. void drawModel(Model m){
  125.   int i;
  126.   for (i=0;i<m.size;i++) {
  127.     Triangle t = m.members[i];
  128.     glPushMatrix();
  129.       glColor3f(t.color.r,t.color.b,t.color.g);
  130.       glBegin(GL_TRIANGLE_STRIP);
  131.       glVertex3f(t.a.x,t.a.y,t.a.z);
  132.       glVertex3f(t.b.x,t.b.y,t.b.z);
  133.       glVertex3f(t.c.x,t.c.y,t.c.z);
  134.       glEnd();
  135.     glPopMatrix();
  136.   }
  137. }
  138.  
  139. typedef P L;
  140.  
  141. L line (P a, P b) {
  142.   L *r = (L*)malloc(sizeof(L));
  143.   r->x = b.x - a.x;
  144.   r->y = b.y - a.y;
  145.   r->z = b.z - a.z;
  146.   return *r;
  147. }
  148.  
  149. float magnitude (L line) {
  150.   return sqrt(pow(line.x,2)+pow(line.y,2)+pow(line.z,2));
  151. }
  152.  
  153. float dot_product (L a, L b) {
  154.   return a.x*b.x +
  155.          a.y*b.y +
  156.          a.z*b.z;
  157. }
  158.  
  159. float angle (L AB, L AC) {
  160.   return acos(dot_product(AB,AC)
  161.               /(magnitude(AB) * magnitude(AC)));
  162. }
  163.  
  164. float area_triangle(Triangle t) {
  165.   L AB = line(t.a,t.b);
  166.   L AC = line(t.a,t.c);
  167.   return .5 * magnitude(AB)*magnitude(AC)*sin(angle(AB,AC));
  168. }
  169.  
  170. float area_model (Model m) {
  171.   float total = 0;
  172.   int i = 0;
  173.   for (;i<m.size;i++) {
  174.     total += area_triangle(m.members[i]);
  175.   }
  176.   return total;
  177. }
  178.  
  179. float model_compare_with(Model m1, Model m2, float f(Model)) {
  180.   return f(m2)/f(m1);
  181. }
  182.  
  183. float model_compare(Model m1, Model m2) {
  184.   return model_compare_with(m1,m2,area_model);
  185. }
  186.  
  187. //Model
  188. void reconstruct(Image i1, View v1, Image i2, View v2) {
  189.   vector<cv::KeyPoint> kpts1,kpts2;
  190.   Ptr<AKAZE> akazze = AKAZE::create();
  191.   ORB detector();
  192.   //GoodFeaturesToTrackDetector::Params paramsGTFF;
  193.   //detector.detect(i0,keypoints0);
  194.   //detector.detect(i1,keypoints1);
  195.   //surf.detect(sceneMat,keypoints1);
  196.   //surf.detect(objectMat,keypoints0);
  197.   //SurfDescriptorExtractor extractor;
  198.   //extractor.compute(sceneMat,keypointsS,descriptors_scene);
  199.   //extractor.compute(objectMat,keypoints0,descriptors_objject);
  200.   //ClannBasedMatcher matcher;
  201.   //BFMatcher matcher(NORM_L1);
  202.   //vector<vector<DMatch>> matches;
  203.   //matcher.knnMatch(descriptors_object,descriptors_scene,matches,2);
  204.   //vector<DMatch> good_matches;
  205.   //size_t i = 0;
  206.   //for (;i<matches.size();++i) {
  207.     //if (matches[i].size()<2) continue;
  208.     //const DMatch &m1 = matches[i][0];
  209.     //const DMatch &m2 = matche[i][1];
  210.     //if (m1.distance <= nndrRatio * m2.distance) good_matches.push_back(m1);
  211.   //}
  212.   //triangulatePoints(v1,v2,i1,i2,reconstruction);
  213.   //return reconstruction;
  214.   //triangulatePoints(InputArray projMatr1,   //cam0 3×4 projection mat
  215.                     //InputArray projMatr2,   //cam1 3×4 projection mat
  216.                     //InputArray projPoints1, //cam0pnts feature points
  217.                     //InputArray ProjPoints2, //cam1pnts feature points
  218.                     //OutputArray points4D)   //pnts3D 4×N array reconstructed points
  219.                                                 //homogeneous coordinates?
  220.   //Model m = mesh_from_points(points4D);
  221.   //return m;
  222. }
  223.  
  224. /*
  225.  
  226. vector<Model> experiment(View v1, View v2, int max_model) {
  227.   vector<Model> model_series;
  228.   model_series.push(start_model);
  229.   int model_index = 1;
  230.   for (;model_index<max_model;model_index++) {
  231.     img1 = get_img(model_series[model_index-1],v1);
  232.     img2 = get_img(model_series[model_index-1],v2);
  233.     model_series.push(reconstruct(img1,v1,img2,v2));
  234.   }
  235.   return model_series;
  236. }
  237. */
  238.  
  239. void showModelView() {
  240.   GLfloat*mv = (GLfloat*)malloc(16*sizeof(GLfloat));
  241.   glGetFloatv(GL_MODELVIEW_MATRIX,mv);
  242.   int i,j,n;
  243.   printf("MODEL VIEW\n");
  244.   for (n=0,i=0;i<4;i++) {
  245.     for (j=0;j<4;j++,n++)
  246.       printf("%f ", mv[n]);
  247.     putchar('\n');
  248.   }
  249.   putchar('\n');
  250. }
  251.  
  252. Matx34f getProjection() {
  253.   // 0 4 8 12
  254.   // 1 5 9 13
  255.   // 2 6 10 14
  256.   // 3 7 11 15
  257.   GLfloat*p = (GLfloat*)malloc(16*sizeof(GLfloat));
  258.   glGetFloatv(GL_PROJECTION,p);
  259.   Matx34f m(p[0], p[4], p[8], p[12],
  260.             p[1], p[5], p[9], p[13],
  261.             p[2], p[6], p[10], p[14]);
  262.             //p[3], p[7], p[11], p[15] cut this off?
  263.   return m;
  264. }
  265.  
  266. void display() {
  267.   showModelView();
  268.   // Clear Screen and Depth Buffer
  269.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  270.   glLoadIdentity();
  271.   // Define a viewing transformation
  272.   // need to use View matrix
  273.   gluLookAt(4,2,0, 0,0,0, 0,1,0);
  274.   glPushMatrix();
  275.     drawModel(current_model);
  276.   glPopMatrix();
  277.   glutSwapBuffers();
  278.   //Image img = glutTakeCVImage();
  279.   //GlutleaveMainloop();
  280.   //return img;
  281. }
  282.  
  283. Image get_img (Model m, View v) {
  284.   current_model = m;
  285.   current_view = v;
  286.   glutMainLoop();
  287.   Image img = glutTakeCVImage();
  288.   glutLeaveMainLoop();
  289.   return img;
  290. }
  291.  
  292.  
  293. void initializeGlut () {
  294.    // select projection matrix
  295.   glMatrixMode(GL_PROJECTION);
  296.   // set the viewport
  297.   glViewport(0, 0, win.width, win.height);
  298.   // set matrix mode
  299.   glMatrixMode(GL_PROJECTION);
  300.   // reset projection matrix
  301.   glLoadIdentity();
  302.   GLfloat aspect = (GLfloat) win.width / win.height;
  303.   // set up a perspective projection matrix
  304.   gluPerspective(win.field_of_view_angle, aspect, win.z_near, win.z_far);
  305.   // specify which matrix is the current matrix
  306.   glMatrixMode(GL_MODELVIEW);
  307.   glShadeModel(GL_SMOOTH);
  308.   // specify the clear value for the depth buffer
  309.   glClearDepth( 1.0f );
  310.   glEnable(GL_DEPTH_TEST);
  311.   glDepthFunc(GL_LEQUAL);
  312.   // specify implementation-specific hints
  313.   glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  314.   GLfloat amb_light[] = { 0.1, 0.1, 0.1, 1.0 };
  315.   GLfloat diffuse[] = { 0.6, 0.6, 0.6, 1 };
  316.   GLfloat specular[] = { 0.7, 0.7, 0.3, 1 };
  317.   glLightModelfv( GL_LIGHT_MODEL_AMBIENT, amb_light);
  318.   glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  319.   glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
  320.   glEnable(GL_LIGHT0);
  321.   glEnable(GL_COLOR_MATERIAL);
  322.   glShadeModel(GL_SMOOTH);
  323.   glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
  324.   glDepthFunc(GL_LEQUAL);
  325.   glEnable(GL_DEPTH_TEST);
  326.   glEnable(GL_LIGHTING);
  327.   glEnable(GL_LIGHT0);
  328.   glClearColor(0.0, 0.0, 0.0, 1.0);
  329. }
  330.  
  331. Image snap(){
  332.   VideoCapture capture(0); // connect to a camera
  333.   Image img;
  334.   capture >> img;
  335.   return img;
  336. }
  337.  
  338. void keyboard (unsigned char key, int mousePositionX, int mousePositionY) {
  339.   Image img;
  340.   switch (key)
  341.   {
  342.     case KEY_ESCAPE:
  343.       exit(0);
  344.       break;
  345.     case 'c':
  346.       printf("hello\n");
  347.       glutLeaveMainLoop();
  348.       img = glutTakeCVImage();
  349.       while (waitKey(10)<0) {
  350.         imshow("CV image", img);
  351.       }
  352.       glutMainLoop();
  353.       break;
  354.     default:
  355.       break;
  356.   }
  357. }
  358.  
  359. int main(int argc, char **argv) {
  360.   // set window values
  361.   win.width = 640;
  362.   win.height = 480;
  363.   win.title = const_cast<char*>("OpenGL/GLUT");
  364.   win.field_of_view_angle = 45;
  365.   win.z_near = 1.0f;
  366.   win.z_far = 500.0f;
  367.   initStartModel();
  368.   printf("area model = %f\n", area_model(start_model));
  369.   // initialize and run program
  370.   glutInit(&argc, argv);                                     // GLUT initialization
  371.   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);  // Display Mode
  372.   glutInitWindowSize(win.width,win.height);                  // set window size
  373.   glutCreateWindow(win.title);                               // create Window
  374.   glutDisplayFunc(display);                                  // register Display Function
  375.   glutIdleFunc(display);                                     // register Idle Function
  376.   glutKeyboardFunc(keyboard);                                // register Keyboard Handler
  377.   initializeGlut();
  378.   glutMainLoop();
  379.  
  380.   return 0;
  381. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement