Install VTK
water@waters-MacBook-Pro:~/Downloads/VTK-8.1.0/build$ cmake ..
water@waters-MacBook-Pro:~/Downloads/VTK-8.1.0/build$ make
water@waters-MacBook-Pro:~/Downloads/VTK-8.1.0/build$ sudo make install
Install QT5
water@waters-MacBook-Pro:~/Documents/GitHub/opencv/build$ brew install qt5
water@waters-MacBook-Pro:~/Documents/GitHub/opencv/build$ export "PATH=/usr/local/opt/qt/bin:$PATH"
Install OpenCV & OpenCV_contrib
water@waters-MacBook-Pro:~/Documents/GitHub/opencv/build$ cmake -DCMAKE_BUILD_TYPE=Release -DWITH_OPENGL=ON -DWITH_OPENVX=ON -DWITH_QT=ON -DWITH_OPENCL=ON -DOPENCV_EXTRA_MODULES_PATH=/Users/water/Documents/GitHub/opencv_contrib/modules –DWITH_VTK=ON -DVTK_DIR=/Users/water/Downloads/VTK-8.1.0 ..
water@waters-MacBook-Pro:~/Documents/GitHub/opencv/build$ make -j8
water@waters-MacBook-Pro:~/Documents/GitHub/opencv/build$ sudo make install
Test VIZ
#include <opencv2/viz.hpp>
#include <opencv2/calib3d.hpp>
#include <iostream>
using namespace cv;
using namespace std;
/*
* @function main
*/
int main()
{
/// Create a window
viz::Viz3d myWindow("Coordinate Frame");
/// Add coordinate axes
myWindow.showWidget("Coordinate Widget", viz::WCoordinateSystem());
/// Add line to represent (1,1,1) axis
viz::WLine axis(Point3f(-1.0f,-1.0f,-1.0f), Point3f(1.0f,1.0f,1.0f));
axis.setRenderingProperty(viz::LINE_WIDTH, 4.0);
myWindow.showWidget("Line Widget", axis);
/// Construct a cube widget
viz::WCube cube_widget(Point3f(0.5,0.5,0.0), Point3f(0.0,0.0,-0.5), true, viz::Color::blue());
cube_widget.setRenderingProperty(viz::LINE_WIDTH, 4.0);
/// Display widget (update if already displayed)
myWindow.showWidget("Cube Widget", cube_widget);
/// Rodrigues vector
Mat rot_vec = Mat::zeros(1,3,CV_32F);
float translation_phase = 0.0, translation = 0.0;
while(!myWindow.wasStopped())
{
/* Rotation using rodrigues */
/// Rotate around (1,1,1)
rot_vec.at<float>(0,0) += CV_PI * 0.01f;
rot_vec.at<float>(0,1) += CV_PI * 0.01f;
rot_vec.at<float>(0,2) += CV_PI * 0.01f;
/// Shift on (1,1,1)
translation_phase += CV_PI * 0.01f;
translation = sin(translation_phase);
Mat rot_mat;
Rodrigues(rot_vec, rot_mat);
/// Construct pose
Affine3f pose(rot_mat, Vec3f(translation, translation, translation));
myWindow.setWidgetPose("Cube Widget", pose);
myWindow.spinOnce(1, true);
}
return 0;
}
Test OpenGL
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
// OpenGL includes
#ifdef __APPLE__
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#endif
// OpenCV includes
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;
Mat frame;
GLfloat angle= 0.0;
GLuint texture;
VideoCapture camera;
int loadTexture() {
if (frame.data==NULL) return -1;
glBindTexture( GL_TEXTURE_2D, texture ); //bind the texture to it's array
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, frame.cols, frame.rows,0, GL_BGR, GL_UNSIGNED_BYTE, frame.data);
return 0;
}
void on_opengl(void* param)
{
glLoadIdentity();
// Load Texture
glBindTexture( GL_TEXTURE_2D, texture );
// Rotate plane
glRotatef( angle, 1.0f, 1.0f, 1.0f );
// Create the plate
glBegin (GL_QUADS);
glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0);
glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
glEnd();
}
int main( int argc, const char** argv )
{
// Open WebCam
camera.open(0);
if(!camera.isOpened())
return -1;
// Create new windows
namedWindow("OpenGL Camera", WINDOW_OPENGL);
// Enable texture
glEnable( GL_TEXTURE_2D );
glGenTextures(1, &texture);
setOpenGlDrawCallback("OpenGL Camera", on_opengl);
while(waitKey(30)!='q'){
camera >> frame;
// Create first texture
loadTexture();
updateWindow("OpenGL Camera");
angle =angle+4;
}
// Destroy the windows
destroyWindow("OpenGL Camera");
return 0;
}