Installing OpenCV 2.4.13 on Ubuntu 16.04

Installing OpenCV 2.4.13 on Ubuntu 16.04

Sun, Oct 16, 2016Tags: #OpenCV#Ubuntu#C++#CMake

This post is (for the most part) note to self. Please follow with caution.

Optional Pre-Clean Up

If created with make install then make uninstall
Else if created with checkinstall then dpkg -r release
Else sudo apt-get purge libopencv* && sudo apt-get autoremove

Additional files to remove:
sudo rm -rf /usr/local/include/opencv/
sudo rm -rf /usr/local/include/opencv2/
sudo rm -rf /usr/local/share/OpenCV/
sudo find /var/cache/apt/archives/ -name \*opencv\* -exec rm -rf {} \;
sudo find /usr/local/lib/ -name \*opencv\* -exec rm -rf {} \;
sudo find /usr/local/bin/ -name \*opencv\* -exec rm -rf {} \;


Install Script

# source: http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html

# install dependencies
sudo apt-get update
sudo apt-get install -y build-essential checkinstall cmake git libopencv-dev libgtk2.0-dev pkg-config libavcodec-dev libpng12-dev libavformat-dev libswscale-dev yasm libxine2 libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libqt4-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils

# optional packages
sudo apt-get install -y python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff5-dev libjasper-dev libdc1394-22-dev

# download opencv-2.4.13
wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.13/opencv-2.4.13.zip
unzip opencv-2.4.13.zip && cd opencv-2.4.13
mkdir release && cd release

# compile and install
cmake -G "Unix Makefiles" -D CMAKE_CXX_COMPILER=/usr/bin/g++ -D CMAKE_C_COMPILER=/usr/bin/gcc -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -DWITH_OPENGL=ON -D BUILD_FAT_JAVA_LIB=ON -D INSTALL_TO_MANGLED_PATHS=ON -D INSTALL_CREATE_DISTRIB=ON -D INSTALL_TESTS=ON -D ENABLE_FAST_MATH=ON -D WITH_IMAGEIO=ON -D BUILD_SHARED_LIBS=OFF -D WITH_GSTREAMER=ON ..
make all -j2
sudo make install && sudo checkinstall

Sanity Check

~$ pkg-config --cflags opencv
-I/usr/local/include/opencv -I/usr/local/include

~$ pkg-config --libs opencv
-L/usr/local/lib -lopencv_contrib -lopencv_stitching -lopencv_nonfree -lopencv_superres -lopencv_ocl -lopencv_ts -lopencv_videostab -lopencv_gpu -lopencv_photo -lopencv_objdetect -lopencv_legacy -lopencv_video -lopencv_ml -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_imgproc -lopencv_flann -lopencv_core -lQtCore -lQtTest -lQtGui -lQtOpenGL -lswscale-ffmpeg -lavutil-ffmpeg -lavformat-ffmpeg -lavcodec-ffmpeg -lv4l2 -lv4l1 -ldc1394 -lgstpbutils-0.10 -lgstriff-0.10 -lgstapp-0.10 -lgstvideo-0.10 -lxml2 -lglib-2.0 -lgthread-2.0 -lgmodule-2.0 -lgobject-2.0 -lgstreamer-0.10 -lgstbase-0.10 -lIlmThread -lHalf -lIex -lIlmImf -lImath -ljasper -ltiff -lpng -ljpeg -latomic -ltbb -lGL -lGLU -lrt -lpthread -lm -ldl -lstdc++ -lz

~$ whereis opencv
opencv: /usr/include/opencv /usr/share/opencv

~$ whereis opencv2
opencv2: /usr/include/opencv2

Run Sample OpenCV program

Save code below as DisplayImage.cpp

#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;
}

~$ sudo g++ DisplayImage.cpp -o DisplayImage `pkg-config --cflags --libs opencv`
~$ ./DisplayImage [path-to-image]

Compile using CMake

Create CMakeLists.txt on the same level as the main source file with the following code:

cmake_minimum_required( VERSION 2.8 )
project( DisplayImage )
set( OpenCV_DIR "/usr/share/OpenCV/" )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

Create a build folder on the same level as the other two files. You can name it however you want. cd into the folder and cmake .. then make.

~$ mkdir build && cd build
~/build$ cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/build
~/build$ make
Scanning dependencies of target DisplayImage
[ 50%] Building CXX object CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o
[100%] Linking CXX executable DisplayImage
[100%] Built target DisplayImage

Overall project structure looks like below:

~$ tree -L 2
.
├── build
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   ├── cmake_install.cmake
│   ├── DisplayImage
│   └── Makefile
├── CMakeLists.txt
└── DisplayImage.cpp

Qt Creator integration

Install Qt 5.7
File > New file or project
Choose Application > Qt Console Application

Append these to the .pro files.

INCLUDEPATH += .
INCLUDEPATH += /usr/local/include
INCLUDEPATH += /usr/local/include/opencv
INCLUDEPATH += /usr/local/include/opencv2
INCLUDEPATH += /usr/local/include/opencv2/calib3d
INCLUDEPATH += /usr/local/include/opencv2/contrib
INCLUDEPATH += /usr/local/include/opencv2/core
INCLUDEPATH += /usr/local/include/opencv2/features2d
INCLUDEPATH += /usr/local/include/opencv2/flann
INCLUDEPATH += /usr/local/include/opencv2/gpu
INCLUDEPATH += /usr/local/include/opencv2/highgui
INCLUDEPATH += /usr/local/include/opencv2/imgproc
INCLUDEPATH += /usr/local/include/opencv2/legacy
INCLUDEPATH += /usr/local/include/opencv2/ml
INCLUDEPATH += /usr/local/include/opencv2/nonfree
INCLUDEPATH += /usr/local/include/opencv2/objdetect
INCLUDEPATH += /usr/local/include/opencv2/ocl
INCLUDEPATH += /usr/local/include/opencv2/photo
INCLUDEPATH += /usr/local/include/opencv2/stitching
INCLUDEPATH += /usr/local/include/opencv2/superres
INCLUDEPATH += /usr/local/include/opencv2/ts
INCLUDEPATH += /usr/local/include/opencv2/video
INCLUDEPATH += /usr/local/include/opencv2/videostab
LIBS += `pkg-config opencv --cflags --libs`

Paste this slightly modified code to .cpp file.

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

using namespace cv;

int main(int argc, char** argv )
{
    Mat image;
    image = imread("/path/to/your/image/file");

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

    waitKey(0);

    return 0;
}

Build and Run.

Comments

原文地址:https://www.cnblogs.com/wdzeng/p/8324913.html

时间: 2024-11-05 17:29:19

Installing OpenCV 2.4.13 on Ubuntu 16.04的相关文章

Installing Hyperledger Fabric v1.1 on Ubuntu 16.04?—?Part I

There is an entire library of Blockchain APIs which you can select according to the needs that suffice your application. Some libraries are open-sourced and some are private. For examples, IBM’s Hyperledger Fabric Project, Ethereum, OpenChain, MultiC

Installing Hyperledger Fabric v1.1 on Ubuntu 16.04?—?Part II &amp; ?Part III

This entire tutorial is the second part of the installation of Hyperledger Fabric v1.1. In the previous article, we installed all the dependencies required for us to make the Fabric environment run. In this part of the tutorial, we aim to launch our

Ubuntu 16.04 安装opencv的各种方法(含opencv contrib扩展包安装方法)

Ubuntu 16.04 安装opencv的各种方法(含opencv contrib扩展包安装方法) https://blog.csdn.net/ksws0292756/article/details/79511170 本文主要介绍安装opencv C++接口和python接口的几种方法. 首先介绍C++接口的,然后介绍Python接口的 C++接口安装 采用源码编译的安装方式,基本也就是这种方法了,首先在官网下载你想安装的版本的opencv压缩包,下载连接如下: https://github.

解决Ubuntu 16.04 上Android Studio2.3上面运行APP时提示DELETE_FAILED_INTERNAL_ERROR Error while Installing APKs的问题

本人工作环境:Ubuntu 16.04 LTS + Android Studio 2.3 AVD启动之后,运行APP,报错提示: DELETE_FAILED_INTERNAL_ERROR Error while Installing APKs 搜索后发现这是因为未关闭android studio上的instant app所致. File->settings->Buil,Execution,Deployment->Instant Run->Disable it. 详情请参看以下sta

Ubuntu 16.04配置OpenCV 3.1.0

我们都知道,OpenCV是基于C++的开源计算机视觉库,但是从2.4.4版本开始提供了Java绑定,也就是说,我们也可以使用Java来开发基于OpenCV的计算机视觉应用.目前,最新的版本是3.1.0,在本文中将会介绍如何中Ubuntu 16.04上搭建OpenCV for Java的开发环境,假设目前使用的是刚刚重装的Linux操作系统. ipp_file=../ippicv_linux_20151201.tgz && ipp_hash=$(md5sum $ipp_file | cut

ubuntu 16.04 安装 opencv +contrib (3.2.0) + python 3.5

环境: - ubuntu 16.04 - OpenCV + contrib 3.2.0 (文中附下载链接) - Python 3.5 基于其他环境的配置应该大同小异. 没时间解释了,直接上车. 更新下系统: sudo apt-get update sudo apt-get upgrade 安装依赖项: sudo apt-get install build-essential cmake pkg-config sudo apt-get install libjpeg8-dev libtiff5-d

Linux(Ubuntu 16.04)中安装OpenCV + OpenCV_Contrib

近两个月来接触了Linux系统,在老板的建议下翻了Ubuntu的牌子,我安装的版本是16.04,用习惯之后感觉蛮好的,比Windows要强.好啦,废话不说啦,下面开始说在Ubuntu中安装OpemCV+OpenCV_Contrib. 首先,准备一下开发环境: Ubuntu 16.04 64位 cmake ant jdk git python 接下来,从github中down下OPenCV + OpenCV_Contrib库,可以从https://github.com/opencv上下载(注意版本

ubuntu 16.04 安装opencv 3.4.0

官网手册中的说明基本上是比较全的,这里主要记录下安装过程,以及编译选项 多看官方手册,少走弯路 官网安装说明:https://docs.opencv.org/3.4.0/d7/d9f/tutorial_linux_install.html 环境: Ubuntu 16.04 64bit ,openCV 3.4.0 一.安装opencv依赖包 GCC 4.4.x or later CMake 2.8.7 or higher Git GTK+2.x or higher, including heade

Ubuntu 16.04下为Android编译OpenCV 3.2.0 Manager

http://johnhany.net/2016/07/build-opencv-manager-for-android-on-ubuntu/ 最近想在Android上尝试一下SIFT和SURF匹配算法,但考虑到这些算法都是专利保护的,并没有被包含在预编译库中,所以还需要自己来动手编译OpenCV Android SDK.在OpenCV 2.4.x版本中,这些算法被包含在nonfree模块中:从3.0版本开始,用于图像特征匹配的一些算法(比如SIFT,SURF,BRIEF,FREAK等)被转移到