Skip to content

Commit

Permalink
Merge remote-tracking branch 'oreilly/master' into learning15
Browse files Browse the repository at this point in the history
examples 15-02 changed, added 15-03
  • Loading branch information
garybradski committed Jun 1, 2017
2 parents ae1c435 + 674d3d7 commit 24d4f2a
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This is the example code that accompanies Learning OpenCV 3 by Adrian Kaehler an

**In progress May 24, 2017**

This code is in progress, currently at Chapter 15, bear with us, the code is coming, both Authors are busy, as always, building startups.
This code is in progress, currently at Chapter 15, bear with us, the code is coming, both Authors are busy, as always, building startups. If you want to help, request to join the project. Keep pull requests to one function at a time. It doesn't have to be just examples from the book, it can be other code snippets from the book, exercises, new functionality or even a "how to use" opencv_contrib functions.

Note, for your interest, included here is a _Docker_ file that
* Loads Ubuntu 16.04
Expand Down
112 changes: 84 additions & 28 deletions example_09-04.cpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,94 @@
//Example 9-4. Slightly modified code from the OpenCV documentation that draws a
//cube every frame; this modified version uses the global variables rotx and roty that are
//connected to the sliders in Figure 9-6
// Note: This example needs OpenGL installed on your system. It doesn't build if
// the OpenGL libraries cannot be found.
#include <GL/gl.h>
#include <GL/glu.h>

#include <opencv2/opencv.hpp>
#include <GL/gl.h>
#include <GL/glut.h>
#include <opencv2/core/opengl.hpp>
#include <iostream>

using namespace std;

void on_opengl( void* param ) {
glMatrixModel( GL_MODELVIEW );
glLoadIdentity();
glTranslated( 0.0, 0.0, -1.0 );
glRotatef( rotx, 1, 0, 0 );
glRotatef( roty, 0, 1, 0 );
glRotatef( 0, 0, 0, 1 );
static const int coords[6][4][3] = {
{ { +1, -1, -1 }, { -1, -1, -1 }, { -1, +1, -1 }, { +1, +1, -1 } },
{ { +1, +1, -1 }, { -1, +1, -1 }, { -1, +1, +1 }, { +1, +1, +1 } },
{ { +1, -1, +1 }, { +1, -1, -1 }, { +1, +1, -1 }, { +1, +1, +1 } },
{ { -1, -1, -1 }, { -1, -1, +1 }, { -1, +1, +1 }, { -1, +1, -1 } },
{ { +1, -1, +1 }, { -1, -1, +1 }, { -1, -1, -1 }, { +1, -1, -1 } },
{ { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 } }
};
for (int i = 0; i < 6; ++i) {
glColor3ub( i*20, 100+i*10, i*42 );
glBegin(GL_QUADS);
for (int j = 0; j < 4; ++j) {
glVertex3d(
0.2 * coords[i][j][0],
0.2 * coords[i][j][1],
0.2 * coords[i][j][2]
);
int rotx = 55, roty = 40;

void on_opengl(void* param) {
cv::ogl::Texture2D* backgroundTex = (cv::ogl::Texture2D*)param;
glEnable( GL_TEXTURE_2D );
backgroundTex->bind();
cv::ogl::render(*backgroundTex);
glDisable( GL_TEXTURE_2D );

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef(0, 0, -0.5);
glRotatef( rotx, 1, 0, 0 );
glRotatef( roty, 0, 1, 0 );
glRotatef( 0, 0, 0, 1 );
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LESS );
static const int coords[6][4][3] = {
{ { +1, -1, -1 }, { -1, -1, -1 }, { -1, +1, -1 }, { +1, +1, -1 } },
{ { +1, +1, -1 }, { -1, +1, -1 }, { -1, +1, +1 }, { +1, +1, +1 } },
{ { +1, -1, +1 }, { +1, -1, -1 }, { +1, +1, -1 }, { +1, +1, +1 } },
{ { -1, -1, -1 }, { -1, -1, +1 }, { -1, +1, +1 }, { -1, +1, -1 } },
{ { +1, -1, +1 }, { -1, -1, +1 }, { -1, -1, -1 }, { +1, -1, -1 } },
{ { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 } }
};
for (int i = 0; i < 6; ++i) {
glColor3ub( i*20, 100+i*10, i*42 );
glBegin( GL_QUADS );
for (int j = 0; j < 4; ++j) {
glVertex3d(
0.2 * coords[i][j][0],
0.2 * coords[i][j][1],
0.2 * coords[i][j][2]
);
}
glEnd();
}
}

void on_trackbar( int, void* ) {
cv::updateWindow( "Example 9-4" );
}

void help(char ** argv) {
cout << "Call: " << argv[0] << " <image>" << endl;
cout << "Here OpenGL is used to render a cube on top of an image.\n"
<< "User can rotate the cube with the sliders" <<endl;
}

int main(int argc, char* argv[])
{
if(argc != 2) {
help (argv);
return -1;
}
glEnd();
}

cv::Mat img = cv::imread(argv[1]);
if( img.empty() ) {
cout << "Cannot load " << argv[1] << endl;
return -1;
}

cv::namedWindow( "Example 9-4", CV_WINDOW_OPENGL );
cv::resizeWindow("Example 9-4", img.cols, img.rows);
cv::createTrackbar( "X-rotation", "Example 9-4", &rotx, 360, on_trackbar);
cv::createTrackbar( "Y-rotation", "Example 9-4", &roty, 360, on_trackbar);

cv::ogl::Texture2D backgroundTex(img);
cv::setOpenGlDrawCallback("Example 9-4", on_opengl, &backgroundTex);
cv::updateWindow("Example 9-4");

cv::waitKey(0);

cv::setOpenGlDrawCallback("Example 9-4", 0, 0);
cv::destroyAllWindows();

return 0;
}

0 comments on commit 24d4f2a

Please sign in to comment.