Windows下要编译源码的话需要用到cmake这个软件,Linux下同样需要使用这个工具,不过在Linux下就不需要下载什么软件了,直接Terminal就搞定。下面是详细过程。
// 安装编译工具
sudo apt-get install cmake
sudo apt-get install build-essential
// 安装依赖包
sudo apt-get install libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
// 还有几个可选包也安装一下
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
unzip opencv-2.4.13.zip
cd opencv-2.4.13/
// 新建一个临时文件夹用于存放编译的文件
mkdir release
// 切换到新建的文件夹下
cd release/
// 配置路径,编译
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make -j4 //这是电脑cpu核数,查看一下自己的是几核,改成对应的数字
然后就开始漫长的编译过程了,耐心等待一段时间吧。编译成功大概是下面这个样子的:
[100%] Linking CXX executable ../../bin/opencv_test_gpu
/usr/bin/ld: warning: libpcre.so.1, needed by //home/jianqiang/anaconda2/lib/libgobject-2.0.so.0, not found (try using -rpath or -rpath-link)
[100%] Built target opencv_test_gpu
[100%] Linking CXX shared library ../../lib/cv2.so
[100%] Built target opencv_python
jianqiang@jianqiang-Ubuntu:~/Downloads/opencv-2.4.13/release$
sudo make install
sudo gedit /etc/ld.so.conf.d/opencv.conf
/usr/local/lib
sudo ldconfig
sudo gedit /etc/bash.bashrc
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
su //获取root权限,否则下面的source命令不可用
su: Authentication failure
如果出现这个错误的话,按照下面的方法更新一下密码即可:
sudo passwd root
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
source /etc/bash.bashrc
jianqiang@jianqiang-Ubuntu:~$ su
Password:
root@jianqiang-Ubuntu:/home/jianqiang# source /etc/bash.bashrc
sudo updatedb //更新database
#include
#include
#include
using namespace cv;
int main()
{
Mat image = imread("timg_1.jpg", 1);
if (image.empty())
{
std::cout << "Image file is empty." << std::endl;
return -1;
}
namedWindow("Test");
imshow("Test", image);
waitKey(0);
return 0;
}
g++ Dis.cpp -o Dis.o `pkg-config --cflags --libs opencv`
/usr/bin/ld: warning: libpcre.so.1, needed by //home/jianqiang/anaconda2/lib/libgobject-2.0.so.0, not found (try using -rpath or -rpath-link)
cd /usr/lib/x86_64-linux-gnu
sudo ln -s ~/anaconda2/lib/libpcre.so.1 libpcre.so.1
sudo ldconfig
cmake_minimum_required(VERSION 2.8)
project( Display )
find_package( OpenCV REQUIRED )
add_executable( Display display.cpp )
target_link_libraries( Display ${OpenCV_LIBS} )
cmake . //注意cmake后面是一个空格加一个黑点,这其实是cmake CMakeLists.txt的省略写法
make
./Display timg_1.jpg
/usr/bin/ld: cannot find -lopencv_dep_cudart
set(CUDA_USE_STATIC_CUDA_RUNTIME OFF)