Ubuntu 12.04安装OpenCV

1. 先从sourceforge上下载OpenCV的源码

http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.0/


2. 解压到任意目录

tar xvfj OpenCV-2.4.3.tar.bz2

3. 进入源码目录,创建release目录

cd OpenCV-2.4.3
mkdir release


4. 可以看到在OpenCV目录下,有个CMakeLists.txt文件,需要事先安装一些软件

sudo apt-get install build-essential cmake libgtk2.0-dev pkg-config python-dev python-numpy libavcodec-dev libavformat-dev libswscale-dev


5.  进入release目录,安装OpenCV是所有的文件都会被放到这个release目录下

cd release


6. cmake编译OpenCV源码,安装所有的lib文件都会被安装到/usr/local目录下

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..

7. 安装

sudo make install


8. 测试,在某个目录下建立一个DisplayImage.cpp文件

#include 
#include 

using namespace cv;

int main(int argc, char* argv[])
{
    Mat image;
    image = imread(argv[1], 1);

    if (argc != 2 || !image.data) 
    {
        printf("No image data\n");
        return -1;
    }

    namedWindow("Display Image", CV_WINDOW_AUTOSIZE);
    imshow("Display Image", image);
    waitKey(0);
    return 0;
}


9. 从网上下那个著名的lena.jpg放在cpp目录下


10. 写一个cmake的makefile,也叫CMakeLists.txt

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

11. 编译+运行

cmake .
make
./DisplayImage lena.jpg

12. 完成

你可能感兴趣的:(Linux学习)