首頁 > 軟體

Ubuntu 14.04下OpenCV 3.0+Python 2.7安裝測試

2020-06-16 18:01:01

本文記錄了Ubuntu 14.04下使用原始碼手動安裝OpenCV 3.0的過程。此外記錄了在Python中安裝及載入OpenCV的方法。

1、安裝OpenCV所需的庫(編譯器、必須庫、可選庫)

  • GCC 4.4.x or later
  • CMake 2.6 or higher
  • Git
  • GTK+2.x or higher, including headers (libgtk2.0-dev)
  • pkg-config
  • Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
  • ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
  • [optional] libtbb2 libtbb-dev
  • [optional] libdc1394 2.x
  • [optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev

[compiler] sudo apt-get install build-essential
[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

2、從官網下載最新opencv原始碼(2.4以上)http://sourceforge.net/projects/opencvlibrary/

或者github

3、編譯opencv

將opencv放至任意目錄,解壓

  unzip opencv-3.0.0-rc1.zip

建立編譯目錄,編譯

cd ~/opencv-3.0.0-rc1
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make
sudo make install

4、測試opencv

1) 建立工作目錄

mkdir ~/opencv-lena
cd ~/opencv-lena
gedit DisplayImage.cpp

2) 編輯如下程式碼

#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;

int main(int argc, char** argv )
{
    if ( argc != 2 )
    {
        printf("usage: DisplayImage.out <Image_Path>n");
        return -1;
    }

    Mat image;
    image = imread( argv[1], 1 );

    if ( !image.data )
    {
        printf("No image data n");
        return -1;
    }
    namedWindow("Display Image", WINDOW_AUTOSIZE );
    imshow("Display Image", image);

    waitKey(0);

    return 0;
}

3) 建立CMake編譯檔案

gedit CMakeLists.txt

寫入如下內容

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

4) 編譯

cd ~/opencv-lena
cmake .
make

5) 執行

此時opencv-lena資料夾中已經產生了可執行檔案DisplayImage,下載lena.jpg放在opencv-lena下,執行

./DisplayImage lena.jpg

6) 結果

5、安裝python-opencv

可直接使用apt安裝

sudo apt-get install python-opencv
sudo apt-get install python-numpy

測試:

開啟python,import cv模組成功即可。

import cv

--------------------------------------分割線 --------------------------------------

Ubuntu Linux下安裝OpenCV2.4.1所需包 http://www.linuxidc.com/Linux/2012-08/68184.htm

Ubuntu 12.04 安裝 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htm

CentOS下OpenCV無法讀取視訊檔 http://www.linuxidc.com/Linux/2011-07/39295.htm

Ubuntu 12.04下安裝OpenCV 2.4.5總結 http://www.linuxidc.com/Linux/2013-06/86704.htm

Ubuntu 10.04中安裝OpenCv2.1九步曲 http://www.linuxidc.com/Linux/2010-09/28678.htm

基於QT和OpenCV的臉部辨識系統 http://www.linuxidc.com/Linux/2011-11/47806.htm


IT145.com E-mail:sddin#qq.com