[oepncv] Ubuntu安装Opencv - 百年刘傲天

in Ubuntu with 0 comment

亲测Ubuntu18.04安装Opencv

需要提前安装好的内容

sudo apt install build-essential cmake unzip pkg-config
sudo apt install libjpeg-dev libpng-dev libtiff-dev
sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt install libxvidcore-dev libx264-dev
sudo apt install libgtk-3-dev
sudo apt install libatlas-base-dev gfortran
sudo apt install python3-dev

下载源码

wget -O opencv.zip https://github.com/opencv/opencv/archive/3.4.4.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/3.4.4.zip

解压资源

unzip opencv.zip 
unzip opencv_contrib.zip

迁移目录

cd ~
mkdir opencv
mv opencv-3.4.4 opencv
mv opencv_contrib-3.4.4 opencv_contrib 
mv opencv* opencv/

开始安装

cd opencv/opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv/opencv_contrib/modules \
-D PYTHON_EXECUTABLE=/usr/bin/python3 \
-D BUILD_EXAMPLES=ON ..

这个过程很漫长

如果出错

cd /usr/include/linux
sudo apt install libv4l-dev
sudo ln -s ../libv4l1-videodev.h videodev.h

编译安装

make -j4
sudo make install
sudo ldconfig

验证成功是否

pkg-config --modversion opencv

在Python中使用

cd /usr/local/python/cv2/python-3.6
sudo mv cv2.cpython-36m-x86_64-linux-gnu.so cv2.so
cd ~/.local/lib/python3.6/site-packages/
ln -s /usr/local/python/cv2/python-3.6/cv2.so cv2.so
Responses