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 #include #include 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(0,0) += CV\_PI \* 0.01f;
rot\_vec.at(0,1) += CV\_PI \* 0.01f;
rot\_vec.at(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 #include #include using namespace std;
// OpenGL includes
#ifdef __APPLE__
#include #else
#include #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;
}