写在前面:
先尝试:
pip install opencv-python
或者:
sudo pip3 install opencv-python
https://docs.opencv.org/4.2.0/d2/de6/tutorial_py_setup_in_ubuntu.html
https://linuxize.com/post/how-to-install-opencv-on-ubuntu-18-04/
https://www.pyimagesearch.com/2018/05/28/ubuntu-18-04-how-to-install-opencv/
OpenCV (Open Source Computer Vision Library) is an open-source computer vision library and has bindings for C++, Python, and Java. It is used for a very wide range of applications, including medical image analysis, stitching street view images, surveillance video, detecting and recognizing faces, tracking moving objects, extracting 3D models, and much more. OpenCV can take advantage of multi-core processing and features GPU acceleration for real-time operation.
This tutorial explains how to install OpenCV on Ubuntu 18.04.
For most people, the easiest way to install OpenCV on Ubuntu is to install it using the apt package management tool. If you want to install the latest stable version of OpenCV from source, scroll down to the Installing OpenCV from the Source section of this tutorial.
Choose one of the installation options that works best for you.
Install OpenCV from the Ubuntu Repository
The OpenCV package is available from the Ubuntu 18.04 distribution repository. At the time of writing, the version in the repositories is 3.2, which is not the latest version.
To install OpenCV from the Ubuntu 18.04 repositories, follow these steps:
-
Refresh the packages index and install the OpenCV package by typing:
sudo apt updatesudo apt install python3-opencv
The command above will install all packages necessary to run OpenCV.
-
To verify the installation, import the cv2 module and print the OpenCV version:
python3 -c "import cv2; print(cv2.version)"
Copy
3.2.0
The default Python version in Ubuntu 18.04 LTS is version 3.6. If you want to install OpenCV with python 2 bindings install the python-opencv package.
Installing OpenCV from the Source
Building the OpenCV library from source is the recommended way of installing OpenCV. It will be optimized for your particular system and you will have complete control over the build options.
To install the latest OpenCV version from the source, perform the following steps:
-
Install the required dependencies:
sudo apt install build-essential cmake git pkg-config libgtk-3-dev \ libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \ libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev \ gfortran openexr libatlas-base-dev python3-dev python3-numpy \ libtbb2 libtbb-dev libdc1394-22-dev
-
Clone the OpenCV’s and OpenCV contrib repositories:
mkdir ~/opencv_build && cd ~/opencv_buildgit clone https://github.com/opencv/opencv.gitgit clone https://github.com/opencv/opencv_contrib.git
At the time of writing, the default version in the github repositories is version 4.2.0. If you want to install an older version of OpenCV, cd to both opencv and opencv_contrib directories and run git checkout
-
Once the download is complete, create a temporary build directory, and switch to it:
cd ~/opencv_build/opencvmkdir build && cd build
Set up the OpenCV build with CMake:
cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_C_EXAMPLES=ON \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_GENERATE_PKGCONFIG=ON \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \ -D BUILD_EXAMPLES=ON ..
When the CMake build system is finalized, you will see something like below:
![image.jpeg](https://upload-images.jianshu.io/upload_images/7184443-a846085e6a4805d9.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
-
Start the compilation process:
make -j8
Modify the -j flag according to your processor. If you do not know the number of cores your processor, you can find it by typing nproc.
The compilation may take several minutes or more, depending on your system configuration. Once it is completed you will see something like below:
![image.jpeg](https://upload-images.jianshu.io/upload_images/7184443-70a9aa5d40fc102f.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
-
Install OpenCV with:
sudo make install
![image.jpeg](https://upload-images.jianshu.io/upload_images/7184443-bc2296582832ad61.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
-
To verify whether OpenCV has been installed successfully, type the following command and you should see the OpenCV version:
pkg-config --modversion opencv4
4.2.0
python3 -c "import cv2; print(cv2.version)"
Copy
4.2.0-dev
Conclusion
We have shown you two different ways to install OpenCV on your Ubuntu 18.04 server. The method you choose depends on your requirements and preferences. Even though installing the packaged version from the Ubuntu repository is easier, building OpenCV from source gives you more flexibility, and it should be your first option when installing OpenCV.
If you have any questions or feedback, feel free to comment below.
在 cmake时,会出现 Downlad IPPICV,这时可以先下载。
https://www.cnblogs.com/yongy1030/p/10293178.html
手动安装OpenCV下的IPP加速库
写在前面
安装opencv的时候,往往会卡在这里:
|
1
|
IPPICV: Download: ippicv_2019_lnx_intel64_general_20180723.tgz
|
其实就是墙的原因,然后你去搜,解决方法就是先下载好,然后安装opencv的时候从本地载入。
有的人是真恶心,放个CSDN下载在那里。
转载一个好人的博客吧,这里有下载的方式,所有的IPP版本都可以这样做。
https://yinguobing.com/install-ippcv-manually/
以下是那位好人博客的方法
ippcv会在cmake的时候自动从github上下载,但是在网络状况不佳的情况下会下载失败。这时候我们只能采用手动安装的方式。
ippcv的下载地址其实就藏在ippcv.cmake文件(本人注:在opencv/3rdparty/ippicv文件夹下)中。里边有几个比较重要的变量。
第一个是IPPICV_COMMIT,它的值可以从代码中找到:
|
1
|
set(IPPICV_COMMIT "a62e20676a60ee0ad6581e217fe7e4bada3b95db")
|
第二个是OPENCV_IPPICV_URL,同理可以找到:
|
1
|
"{IPPICV_COMMIT}/ippicv/"
|
这两个联合起来就可以得到文件的路径为:
|
1
|
https://raw.githubusercontent.com/opencv/opencv_3rdparty/a62e20676a60ee0ad6581e217fe7e4bada3b95db/ippicv/
|
最后一个是文件名OPENCV_ICV_NAME,针对不同的系统与环境会有所不同,Linux 64bit下的文件名可以从如下代码中找到:
|
1
2
3
|
if(X86_64)
set(OPENCV_ICV_NAME "ippicv_2017u2_lnx_intel64_20170418.tgz")
set(OPENCV_ICV_HASH "87cbdeb627415d8e4bc811156289fa3a")
|
将这三样组合在一起即可得到最终的URL:
|
1
|
https://raw.githubusercontent.com/opencv/opencv_3rdparty/a62e20676a60ee0ad6581e217fe7e4bada3b95db/ippicv/ippicv_2017u2_lnx_intel64_20170418.tgz
|
当然,你也可以不这么麻烦,直接访问OpenCV在Github上的opencv_3rdparty也可以找到文件的具体地址,选择正确的分支即可。
目前最新的ippcv可以在这里找到:(本人注:这个已经过时了,当然要是有你需要的,那好挺好)
|
1
|
https://github.com/opencv/opencv_3rdparty/tree/ippicv/master_20170418/ippicv
|
以上是那位好人博客的方法
之后就是
将 opencv/3rdparty/ippicv文件夹下的 ippicv.cmake中,第47行
|
1
|
"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"
|
改成:
|
1
|
"file://{刚刚手动下载的IPP文件的上一级目录}/"
|
比如,我的IPP文件在这:
/home/yongyang/Downloads/ippicv_2019_lnx_intel64_general_20180723.tgz
所以改成:
|
1
|
"file:///home/yongyang/Downloads/"
|