OpenCV安装

写在前面:

先尝试:

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:

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

  2. 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:

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

  2. 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 <opencv-version>

  3. 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)
  1. 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)
  1. 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)
  1. 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的时候,往往会卡在这里:

<colgroup><col style="width: 130px;"><col style="width: 130px;"></colgroup>
|

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,它的值可以从代码中找到:

<colgroup><col style="width: 130px;"><col style="width: 130px;"></colgroup>
|

1

|

set(IPPICV_COMMIT "a62e20676a60ee0ad6581e217fe7e4bada3b95db")

|

第二个是OPENCV_IPPICV_URL,同理可以找到:

<colgroup><col style="width: 130px;"><col style="width: 130px;"></colgroup>
|

1

|

"ENV{OPENCV_IPPICV_URL}"<br>"https://raw.githubusercontent.com/opencv/opencv_3rdparty/{IPPICV_COMMIT}/ippicv/"

|

这两个联合起来就可以得到文件的路径为:

<colgroup><col style="width: 130px;"><col style="width: 130px;"></colgroup>
|

1

|

https://raw.githubusercontent.com/opencv/opencv_3rdparty/a62e20676a60ee0ad6581e217fe7e4bada3b95db/ippicv/

|

最后一个是文件名OPENCV_ICV_NAME,针对不同的系统与环境会有所不同,Linux 64bit下的文件名可以从如下代码中找到:

<colgroup><col style="width: 130px;"><col style="width: 130px;"></colgroup>
|

1

2

3

|

if(X86_64)

set(OPENCV_ICV_NAME "ippicv_2017u2_lnx_intel64_20170418.tgz")

set(OPENCV_ICV_HASH "87cbdeb627415d8e4bc811156289fa3a")

|

将这三样组合在一起即可得到最终的URL:

<colgroup><col style="width: 130px;"><col style="width: 130px;"></colgroup>
|

1

|

https://raw.githubusercontent.com/opencv/opencv_3rdparty/a62e20676a60ee0ad6581e217fe7e4bada3b95db/ippicv/ippicv_2017u2_lnx_intel64_20170418.tgz

|

当然,你也可以不这么麻烦,直接访问OpenCV在Github上的opencv_3rdparty也可以找到文件的具体地址,选择正确的分支即可。

目前最新的ippcv可以在这里找到:(本人注:这个已经过时了,当然要是有你需要的,那好挺好)

<colgroup><col style="width: 130px;"><col style="width: 130px;"></colgroup>
|

1

|

https://github.com/opencv/opencv_3rdparty/tree/ippicv/master_20170418/ippicv

|

以上是那位好人博客的方法

之后就是

将 opencv/3rdparty/ippicv文件夹下的 ippicv.cmake中,第47行

<colgroup><col style="width: 130px;"><col style="width: 130px;"></colgroup>
|

1

|

"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"

|

改成:

<colgroup><col style="width: 130px;"><col style="width: 130px;"></colgroup>
|

1

|

"file://{刚刚手动下载的IPP文件的上一级目录}/"

|

比如,我的IPP文件在这:

/home/yongyang/Downloads/ippicv_2019_lnx_intel64_general_20180723.tgz

所以改成:

<colgroup><col style="width: 130px;"><col style="width: 130px;"></colgroup>
|

1

|

"file:///home/yongyang/Downloads/"

|

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,734评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,931评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,133评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,532评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,585评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,462评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,262评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,153评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,587评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,792评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,919评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,635评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,237评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,855评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,983评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,048评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,864评论 2 354

推荐阅读更多精彩内容