Ubuntu14下安装最新版本的CMake 和opencv C++版本

1.安装CMake

1.1. 卸载已经安装的旧版的CMake

apt-get autoremove cmake

1.2. 文件下载解压:

tar zxvf cmake-3.9.1-Linux-x86_64.tar.gz

1.3. 创建软链接

注: 文件路径是可以指定的, 一般选择在/opt 或 /usr 路径下, 这里选择/opt

mv cmake-3.9.1-Linux-x86_64 /opt/cmake-3.9.1 ln -sf /opt/cmake-3.9.1/bin/* /usr/bin/

1.4然后执行命令检查一下:

>>>cmake --version cmake version 3.9.1 CMake suite maintained and supported by Kitware (kitware.com/cmake). >>> >>>cmake-gui

OK。现在cmake最新版本安装成功。 

2.安装opencv

2.1更新

$ sudo apt-get update

$ sudo apt-get upgrade

2.2安装依赖

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

$ sudo apt-get install python3.5-dev python3-numpy libtbb2 libtbb-dev

$ sudo apt-get install libjpeg-dev libpng-dev libtiff5-dev libjasper-dev libdc1394-22-dev libeigen3-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev sphinx-common libtbb-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libavutil-dev libavfilter-dev libavresample-dev

2.3下载opencv(或者自己先下载下载包,然后再解压,比较快)

$ sudo -s

$ cd /opt

/opt$ git clone https://github.com/Itseez/opencv.git

/opt$ git clone https://github.com/Itseez/opencv_contrib.git

2.4编译和安装opencv(如果是刚刚解压的,直接进入opencv-master目录下,创建build,记得要删除CMakeCahe.txt这个文件)

/opt$ cd opencv

/opt/opencv$ mkdir release

/opt/opencv$ cd release

/opt/opencv/release$ cmake -D BUILD_TIFF=ON -D WITH_CUDA=OFF -D ENABLE_AVX=OFF -D WITH_OPENGL=OFF -D WITH_OPENCL=OFF -D WITH_IPP=OFF -D WITH_TBB=ON -D BUILD_TBB=ON -D WITH_EIGEN=OFF -D WITH_V4L=OFF -D WITH_VTK=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/opt/opencv_contrib/modules /opt/opencv/

/opt/opencv/release$ make -j4

/opt/opencv/release$ make install

/opt/opencv/release$ ldconfig

/opt/opencv/release$ exit

/opt/opencv/release$ cd ~

2.5检查是否安装

$ pkg-config --modversion opencv

(如果不行,出现

Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found   )

那就

apt-file search opencv.pc

sudo apt-get install libopencv-dev

pkg-config --cflags opencv 

pkg-config --libs opencv

之后$ pkg-config --modversion opencv就可以看到版本了

致此安装成功。

 

验证

$ mkdir cpp_test

$ cd cpp_test

$ touch main.cpp

 

#include

#include

int main( int argc, char** argv ) {

    cv::Mat image;

  image = cv::imread("sample.jpeg" , CV_LOAD_IMAGE_COLOR);

    if(! image.data ) {

      std::cout <<  "Could not open or find the image" << std::endl ;

      return -1;

    }

   cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE );

  cv::imshow( "Display window", image );

   cv::waitKey(0);

  return 0;

}

 

编译

g++ main.cpp -o output `pkg-config --cflags --libs opencv`

./output

 

 

 

 

 

你可能感兴趣的:(openCV,opencv,ubuntu14,cmake)