Ubuntu 18.04 opencv+vtk+anaconda+pycharm Environment Configuration

每次重新装ubuntu之后都得各处搜罗教程,这次打算自己写篇环境搭建的document以便之后查看。这次的环境搭建主要是用于三位重建,vtk用来进行suraface rendering。
note:文中的配置截图或者log中的“python3.7” 应为3.6, 我一开始用的3.7,截图后没有重新截图

目录

  • UPDATE:
  • Anaconda+PyCharm
  • VTK+OpenCV
    • OpenCV
    • VTK

UPDATE:

现在改用了OpenCV 4.4.10, 其他的不变。不要按照下面的用cmake-gui先进行配置再重新添加命令,会导致python3 libraries检测不到。原因是HAVE_opencv_python3变量只有在build文件夹为空的时候才能设置。直接用以下命令

cmake \
-D PYTHON3_EXECUTABLE=/home/himalaya/anaconda3/bin/python3 \
-D PYTHON3_INCLUDE_DIR=/home/himalaya/anaconda3/include/python3.6m \
-D PYTHON3_LIBRARY=/home/himalaya/anaconda3/lib/libpython3.6m.so \
-D PYTHON3_NUMPY_INCLUDE_DIR=$(python -c "import numpy; print(numpy.get_include())") \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D PYTHON_DEFAULT_EXECUTABLE=$(which python3.6) \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-master/modules \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_PYTHON3_INSTALL_PATH=/home/himalaya/anaconda3/lib/python3.6/site-packages \
-D PYTHON3_PACKAGES_PATH=/home/himalaya/anaconda3/lib/python3.6/site-packages \
-D BUILD_TIFF=ON \
..

-----------------------------------------------------------------------------------------------

Anaconda+PyCharm

直接在官网下载anaconda3 最新版本 5.2.0(最新版本死活装不上最新的opencv) ,用bash filepath执行.sh文件。一路默认直至安装完成。最后设置好环境变量,将"~/anaconda3/bin"添加到环境变量中。
PyCharm在Ubuntu Software Store 里安装。

VTK+OpenCV

为了方便之后增加新的依赖库,打算继续采用编译source code的方式进行。先编译好VTK, 方便编译OpenCV时直接添加VTK dependency.
先安装了各种依赖库,如下:

sudo apt-get install build-essential  
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python3.6-dev python3-dev libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev  libdc1394-22-dev
sudo apt-get install libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran pylint

OpenCV

从github上下载了opencv以及opencv_contrib master 3.4,同样在opencv文件夹下创建build文件夹,打开cmake-gui
找到OPENCV_EXTRA_MODULES_PATH,路径选中opencv_contrib-master/modules
搜索python, 将OPENCV_PYTHON3_VERSION选中。
CMAKE_BUILD_TYPE, release
依次配置好Python3的路径,如下
Ubuntu 18.04 opencv+vtk+anaconda+pycharm Environment Configuration_第1张图片
Configure.
Generate.

cmake -D PYTHON_DEFAULT_EXECUTABLE=/home/himalaya/anaconda3/bin/python3 ..
--   OpenCV modules:
--     To be built:                 aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dpm face features2d flann freetype fuzzy gapi hdf hfs highgui img_hash imgcodecs imgproc line_descriptor ml objdetect optflow phase_unwrapping photo plot python3 quality reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
--     Disabled:                    java_bindings_generator world
--     Disabled by dependency:      -
--     Unavailable:                 cnn_3dobj cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv java js matlab ovis python2 sfm viz
--     Applications:                tests perf_tests apps
--     Documentation:               NO
--     Non-free algorithms:         YES
-- 
--   GUI: 
--     GTK+:                        YES (ver 3.22.30)
--       GThread :                  YES (ver 2.56.4)
--       GtkGlExt:                  NO
--     VTK support:                 NO
-- 
--   Media I/O: 
--     ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
--     JPEG:                        build-libjpeg-turbo (ver 2.0.2-62)
--     WEBP:                        build (ver encoder: 0x020e)
--     PNG:                         build (ver 1.6.37)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.0.9)
--     JPEG 2000:                   /usr/lib/x86_64-linux-gnu/libjasper.so (ver 1.900.1)
--     OpenEXR:                     build (ver 2.3.0)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
-- 
--   Video I/O:
--     DC1394:                      YES (2.2.5)
--     FFMPEG:                      YES
--       avcodec:                   YES (57.107.100)
--       avformat:                  YES (57.83.100)
--       avutil:                    YES (55.78.100)
--       swscale:                   YES (4.8.100)
--       avresample:                NO
--     GStreamer:                   NO
--     v4l/v4l2:                    YES (linux/videodev2.h)
-- 
--   Parallel framework:            pthreads
-- 
--   Trace:                         YES (with Intel ITT)
-- 
--   Other third-party libraries:
--     Lapack:                      NO
--     Eigen:                       NO
--     Custom HAL:                  NO
--     Protobuf:                    build (3.5.1)
-- 
--   OpenCL:                        YES (no extra features)
--     Include path:                /home/himalaya/Documents/opencv/opencv-master/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
-- 
--   Python 3:
--     Interpreter:                 /home/himalaya/anaconda3/bin/python3 (ver 3.7.1)
--     Libraries:                   /home/himalaya/anaconda3/lib/libpython3.7m.so.1.0-D (ver 3.7.1)
--     numpy:                       /home/himalaya/anaconda3/lib/python3.7/site-packages/numpy/core/include (ver 1.15.4)
--     install path:                /home/himalaya/anaconda3/lib/python3.7/site-packages/cv2/python-3.7
-- 
--   Python (for build):            /home/himalaya/anaconda3/bin/python3
-- 
--   Install to:                    /usr/local
-- -----------------------------------------------------------------
-- 
-- Configuring done
-- Generating done
-- Build files have been written to: /home/himalaya/Documents/opencv/opencv-master/build

make -j8
sudo make install
sudo ldconfig
和大多数教程或者经验不一样的是,cmake中已经写好了python library的install path, 所以不需要更改.so文件名字以及路径,直接用就行。
最后测试下imshow以及namedWindow函数是否可用。

VTK

从 vtk.org 上下载8.2(7.1.1 好像不支持python3.7,编译过程中总是出错)版本。没下载最新版本是因为有稳定性方面的担心。下载完之后解压,到解压后的目录里面创建build文件夹,cd build.
我用的cmake-gui进行的编译,打开cmake-gui之后设置好source code的目录(即解压后的目录路径)以及where to build(即刚创建的build文件夹路径)。Click Configure. 遇到了没有安装libxt-dev的报错,重新安装好这个库再Configure.
把python wrapping 钩上,再configure,发现python3.6 mistached with python2.7(系统自带的),这个更改python的路径到anaconda路径下面:
先在cmake-gui界面点击group, advanced两个选项,再找到VTK_PYTHON_VERSION这一项,选择3.6版本,并且把VTK_WRAP_PYTHON勾选。再configure,这时会多了一个名为PYTHON的group。如果anaconda3的环境变量设置好了的的话会自动找到python3.6的路径。再点击configure,如下会变白:
/home/himalaya/Pictures/Screenshot from 2019-06-27 14-47-18.png
只用python的话可以将CMAKE_INSTALL_PREFIX 改为/home/usrname/anaconda3。考虑到自己可能还会用c++, 遂还是装在了/usr/local里。
最后Generate.
退出cmake-gui,进入到build文件夹。终端执行make -j4
执行sudo make install
sudo ldconfig
现在import还不能找到vtk的库,需要将路径添加到PYTHONPATH环境变量。
更改~/.bashrc,文末添加export PYTHONPATH="$PYTHONPATH:/usr/local/lib/python3.6/site-packages"
source ~/.bashrc
最后随便找找python的vtk测试代码试下是否可以。

你可能感兴趣的:(Ubuntu 18.04 opencv+vtk+anaconda+pycharm Environment Configuration)