sudo apt-get install build-essential cmake pkg-config
sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
sudo apt-get install libgtk-3-dev
sudo apt-get install libatlas-base-dev gfortran
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
(1)andaconda安装:
参考:https://blog.csdn.net/luteresa/article/details/79819476
我的安装目录:/home/leon/x86_install/anaconda3
系统已经有python2.7,python3.5,python3.6(anaconda打包安装的),我的目标是用opencv支持python3.6,这样以后用anaconda开发方便调用cv库,安装opencv所需的numpy也可以直接用anaconda里的,不用额外再安装;
(2)修改默认python3链接
删除/usr/bin/python
sudo rm /usr/bin/python
sudo ln -s /home/leon/anaconda3/bin/python /usr/bin/python3
ps:假如默认访问顺序anaconda里的python3优先的话,也可以不改,总之默认python3访问anaconda里的python3.6就可以。
OPENCV配置项非常多,最新的版本(前几天编的是opencv3.4, 写文章时pull升级到4.0了)都支持cmake,建议用cmake-gui配置,简单方便实用;
sudo apt install cmake-qt-gui
cmake-gui
配置源码路径和目标生成路径:比如
点configure,弹出对话框默认即可->Unix Makefiles->Use default native comilers
X86平台可以用默认,直接点Finish.其他平台选最后一项Specify options for cross-compiling,指定相应编译器,交叉编译后面再专门讨论;
常见的需要配置项:
1)在Search栏上输入OPENCV_EXTRA_MODULES_PATH,配置刚下载的modules路径,比如:/home/leon/src_caffe/opencv_contrib/modules;
2)输入CMAKE_INSTALL_PREFIX,配置成opencv安装目录,如/home/leon/caffe_install/
3)输入BUILD_TYPE,配置为RELEASE;
4)输入WITH_CUDA,去掉,不编译CUDA部分代码;
5)输入BUILD_DOCS,去掉,表示不生成帮助文档;
6)输入WITH_CUFFT,去掉,表示不用cuda的fft;
7)输入CL,分别将WITH_OPENCL, WITH_OPENCLAMDBLAS, WITH_OPENCLAMDFFT, OPENCL_FOUND的勾去掉,不采用OpenCL的GPU加速;
8)输入matlab,相关的去掉;
9)输入WITH_IPP,去掉;
10)输入TIFF,还有其他的jpeg,png之类都选上;
11)添加选项ENABLE_CXX11,支持C++11;
12)配置python,如图:
添加BUILD_opencv_python3选项
点击Configure,进度条完成后,再点击Generate,即会在build下产生Makefile编译文件。
看到Configuring done , 说明配置成功;
make
make install
编译过程中如果遇到其他问题,按提示配置;
把python版本动态库拷到anaconda目录:
cp lib/python3/cv2.cpython-36m-x86_64-linux-gnu.so /home/leon/x86_install/anaconda3/lib/python3.6/
安装好后,如果修改了/usr/bin/python3链接,改回去,不然gnome-terminal起不来:
sudo rm /usr/bin/python3
sudo ln -s /usr/bin/python3.5 /python3
查看版本:
python -c "import cv2; print (cv2.__version__)"
显示lens图片:
代码:
import cv2
srcImage=cv2.imread('./samples/data/lena.jpg')
cv2.imshow('lena', srcImage)
cv2.waitKey(0)
sudo apt-get install --reinstall python-gi
export GIO_EXTRA_MODULES=/usr/lib/x86_64-linux-gnu/gio/modules/
警告消失:
#include
#include
using namespace cv;
int main(int argc, char**argv)
{
if(argc!=2){
printf("usage:displayImage \n");
return -1;
}
Mat image;
image = imread(argv[1],1);
if(!image.data)
{
printf("No iamge data.\n");
return -1;
}
std::cout<<"read ok.\n";
cv::namedWindow("Display Image",WINDOW_AUTOSIZE);
std::cout<<"get namedWindow.\n";
rectangle(image,cvPoint(190,74),cvPoint(340,269),Scalar(255,0,0),1,1,0);
imshow("Display Image",image);
std::cout<<"show Image\n";
waitKey(0);
return 0;
}
编译:
g++ DisplayImage.cpp -L/home/leon/caffe_install/lib -std=c++11 -o DisplayImage -lopencv_core -lopencv_imgcodecs -lopencv_highgui -lopencv_imgproc
sudo /bin/bash -c 'echo "/home/leon/caffe_install/lib" > /etc/ld.so.conf.d/opencv.conf'
$ sudo ldconfig