How to Install OpenCV3 WITH_VTK WITH_OPENGL on macOS High Sierra 10.13.3

Install VTK

  1. water@waters-MacBook-Pro:~/Downloads/VTK-8.1.0/build$ cmake ..
  2. water@waters-MacBook-Pro:~/Downloads/VTK-8.1.0/build$ make
  3. water@waters-MacBook-Pro:~/Downloads/VTK-8.1.0/build$ sudo make install

Install QT5

  1. water@waters-MacBook-Pro:~/Documents/GitHub/opencv/build$ brew install qt5
  2. water@waters-MacBook-Pro:~/Documents/GitHub/opencv/build$ export "PATH=/usr/local/opt/qt/bin:$PATH"

Install OpenCV & OpenCV_contrib

  1. 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 ..

  1. water@waters-MacBook-Pro:~/Documents/GitHub/opencv/build$ make -j8
  2. water@waters-MacBook-Pro:~/Documents/GitHub/opencv/build$ sudo make install

Test VIZ

  1. #include <opencv2/viz.hpp>
  2. #include <opencv2/calib3d.hpp>
  3. #include <iostream>
  4.  
  5. using namespace cv;
  6. using namespace std;
  7.  
  8. /*
  9.  * @function main
  10.  */
  11. int main()
  12. {
  13.     /// Create a window
  14.     viz::Viz3d myWindow("Coordinate Frame");
  15.  
  16.     /// Add coordinate axes
  17.     myWindow.showWidget("Coordinate Widget", viz::WCoordinateSystem());
  18.  
  19.     /// Add line to represent (1,1,1) axis
  20.     viz::WLine axis(Point3f(-1.0f,-1.0f,-1.0f), Point3f(1.0f,1.0f,1.0f));
  21.     axis.setRenderingProperty(viz::LINE_WIDTH, 4.0);
  22.     myWindow.showWidget("Line Widget", axis);
  23.  
  24.     /// Construct a cube widget
  25.     viz::WCube cube_widget(Point3f(0.5,0.5,0.0), Point3f(0.0,0.0,-0.5), true, viz::Color::blue());
  26.     cube_widget.setRenderingProperty(viz::LINE_WIDTH, 4.0);
  27.  
  28.     /// Display widget (update if already displayed)
  29.     myWindow.showWidget("Cube Widget", cube_widget);
  30.  
  31.     /// Rodrigues vector
  32.     Mat rot_vec = Mat::zeros(1,3,CV_32F);
  33.     float translation_phase = 0.0, translation = 0.0;
  34.     while(!myWindow.wasStopped())
  35.     {
  36.         /* Rotation using rodrigues */
  37.         /// Rotate around (1,1,1)
  38.         rot_vec.at<float>(0,0) += CV_PI * 0.01f;
  39.         rot_vec.at<float>(0,1) += CV_PI * 0.01f;
  40.         rot_vec.at<float>(0,2) += CV_PI * 0.01f;
  41.  
  42.         /// Shift on (1,1,1)
  43.         translation_phase += CV_PI * 0.01f;
  44.         translation = sin(translation_phase);
  45.  
  46.         Mat rot_mat;
  47.         Rodrigues(rot_vec, rot_mat);
  48.  
  49.         /// Construct pose
  50.         Affine3f pose(rot_mat, Vec3f(translation, translation, translation));
  51.  
  52.         myWindow.setWidgetPose("Cube Widget", pose);
  53.  
  54.         myWindow.spinOnce(1, true);
  55.     }
  56.  
  57.     return 0;
  58. }

Test OpenGL

  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6. // OpenGL includes
  7. #ifdef __APPLE__
  8. #include <OpenGL/gl.h>
  9. #else
  10. #include <GL/gl.h>
  11. #endif
  12.  
  13. // OpenCV includes
  14. #include "opencv2/core.hpp"
  15. #include "opencv2/imgproc.hpp"
  16. #include "opencv2/highgui.hpp"
  17. using namespace cv;
  18.  
  19. Mat frame;
  20. GLfloat angle= 0.0;
  21. GLuint texture;
  22. VideoCapture camera;
  23.  
  24. int loadTexture() {
  25.  
  26.     if (frame.data==NULL) return -1;
  27.  
  28.     glBindTexture( GL_TEXTURE_2D, texture ); //bind the texture to it's array
  29.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  30.     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  31.  
  32.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  33.  
  34.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, frame.cols, frame.rows,0, GL_BGR, GL_UNSIGNED_BYTE, frame.data);
  35.     return 0;
  36.  
  37. }
  38.  
  39. void on_opengl(void* param)
  40. {
  41.     glLoadIdentity();
  42.     // Load Texture
  43.     glBindTexture( GL_TEXTURE_2D, texture );
  44.     // Rotate plane
  45.     glRotatef( angle, 1.0f, 1.0f, 1.0f );
  46.     // Create the plate
  47.     glBegin (GL_QUADS);
  48.     glTexCoord2d(0.0,0.0); glVertex2d(-1.0,-1.0);
  49.     glTexCoord2d(1.0,0.0); glVertex2d(+1.0,-1.0);
  50.     glTexCoord2d(1.0,1.0); glVertex2d(+1.0,+1.0);
  51.     glTexCoord2d(0.0,1.0); glVertex2d(-1.0,+1.0);
  52.     glEnd();
  53.  
  54. }
  55.  
  56. int main( int argc, const char** argv )
  57. {
  58.     // Open WebCam
  59.     camera.open(0);
  60.     if(!camera.isOpened())
  61.         return -1;
  62.  
  63.     // Create new windows
  64.     namedWindow("OpenGL Camera", WINDOW_OPENGL);
  65.  
  66.     // Enable texture
  67.     glEnable( GL_TEXTURE_2D );
  68.     glGenTextures(1, &texture);
  69.  
  70.     setOpenGlDrawCallback("OpenGL Camera", on_opengl);
  71.  
  72.     while(waitKey(30)!='q'){
  73.         camera >> frame;
  74.         // Create first texture
  75.         loadTexture();
  76.         updateWindow("OpenGL Camera");
  77.         angle =angle+4;
  78.     }
  79.  
  80.  
  81. 	// Destroy the windows
  82. 	destroyWindow("OpenGL Camera");
  83.  
  84. 	return 0;
  85. }

How to Install Tesseract With All Languages

Install tesseract

  1. water@waters-MBP:~$ brew install tesseract --with-all-languages

Have a test

  1. water@waters-MBP:~/Downloads$ tesseract tesseract-test-english.png out

Install chi-sim.traineddata

  1. water@waters-MBP:~$ ls -al /usr/local/Cellar/tesseract/3.05.01/share/tessdata/chi-sim.traineddata
  2. -rw-r--r--  1 water  admin  52662579 Jan 20 22:30 /usr/local/Cellar/tesseract/3.05.01/share/tessdata/chi-sim.traineddata

Have a test

  1. water@waters-MBP:~/Downloads$ tesseract tesseract-test-chinese.png out -l chi-sim