由于需要经常搭建编程训练环境,需要用到OpenCV,把编译过程和遇到的问题记录一下,作为以后参考。
1. Ubuntu下如果你只是需要在python里使用,那么可以直接通过apt-get安装:
sudo apt-get install python-opencv
如果你需要在自己安装的python程序里使用,那么你需要使用自己的python库里的pip来装:
pip install opencv_python
2. 但是当我们需要C++的环境时,就需要自己编译了,方法如下:
首先,安装一些必须的库:
sudo apt-get install cmake
sudo apt-get install python-devel numpy
sudo apt-get install gcc gcc-c++
sudo apt-get install gtk2-devel
sudo apt-get install libv4l-devel
sudo apt-get install ffmpeg-devel
sudo apt-get install gstreamer-plugins-base-devel
下面可视需要安装:
sudo apt-get install libpng-devel
sudo apt-get install libjpeg-turbo-devel
sudo apt-get install jasper-devel
sudo apt-get install openexr-devel
sudo apt-get install libtiff-devel
sudo apt-get install libwebp-devel
从Github上下载OpenCV(这里你可以自己从release下载想要的版本,默认是最新版,目前已到4.x)
sudo apt-get install git
git clone https://github.com/opencv/opencv.git
编译:
cd opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j
sudo make install
此时编译生成的.so会安装在/usr/local/lib下;头文件在/usr/loca/include下。
如果你没有sudo权限,或者想把opencv库放在自己工作目录下面,那么修改--prefix到自定义目录即可,使用这个方法可以让多个OpenCV版本共存,互不影响。
3. 如果需要编译opencv静态库,只需要修改cmake参数即可
cd opencv
mkdir static-build
cd static-build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_SHARED_LIBS=OFF -D BUILD_SHARED_LIBS=NO -D BUILD_PNG=ON -D BUILD_JASPER=ON -D BUILD_JPEG=ON -D BUILD_TIFF=ON -D BUILD_ZLIB=ON -D WITH_JPEG=ON -D WITH_PNG=ON -D WITH_JASPER=ON -D WITH_TIFF=ON ..
make -j
sudo make install
遇到过的问题:
1. nvcc fatal : Unsupported gpu architecture 'compute_11'
解决办法:
如果你安装了CUDA并希望编译OpenCV的CUDA支持,那么添加-D CUDA_GENERATION=Auto 可以解决
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D CUDA_GENERATION=Auto ..
如果你需要编译不带CUDA的OpenCV,那么cmake时添加-D WITH_CUDA=OFF即可:
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=OFF ..
While compile opencv-2.4.13.6
1. math cannot parse the expression: "*100 + ": syntax error, unexpected exp_TIMES, expecting exp_PLU
CMake Error at cmake/OpenCVDetectCXXCompiler.cmake:89 (math):
math cannot parse the expression: "*100 + ": syntax error, unexpected
exp_TIMES, expecting exp_PLUS or exp_MINUS or exp_OPENPARENT or exp_NUMBER
(1)
Solution:
in file opencv2.4.13/cmake/ OpenCVDetectCXXCompiler.cmake, u have to change "dumpversion" to "dumpfullversion"
explanation is that in gcc with higher version, dumpversion function can't get true full version number of compiler so that cmake progress will fail.
2. Error:CPACK_PACKAGE_VERSION does not match version provided by version.hpp header!
Solution:
#in OpenCVPackaging.cmake file (line 23)
set(OPENCV_VCSVERSION "2.4.13.6") #<--add
参考资料:
https://docs.opencv.org/3.4.1/d2/de6/tutorial_py_setup_in_ubuntu.html
https://answers.opencv.org/question/65548/cmake-error-at-cmakeopencvdetectcxxcompilercmake/

8541

被折叠的 条评论
为什么被折叠?



