由于要用c++,迫不得已要安装opencv了,之前的Python都是直接pip install 就好了,c++的话还是麻烦一些。不过这样自己编译的话,是c++和Python两个语言都装了。
这里用了两种安装方式:1.本机安装 2.docker安装。中途踩了一些坑,特此记录。
一.本机安装
参考https://www.learnopencv.com/install-opencv3-on-ubuntu/
#更新包sudo apt-get update sudo apt-get upgrade#安装os库sudo apt-get remove x264 libx264-dev #先移除,自己命令执行后发现并没有这个sudo apt-get install build-essential checkinstall cmake pkg-config yasm sudo apt-get install git gfortran sudo apt-get install libjpeg8-dev libjasper-dev libpng12-dev sudo apt-get install libtiff5-dev #ubuntu16.04sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev sudo apt-get install libxine2-dev libv4l-dev sudo apt-get install libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev sudo apt-get install qt5-default libgtk2.0-dev libtbb-dev sudo apt-get install libatlas-base-dev sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev sudo apt-get install libvorbis-dev libxvidcore-dev sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev sudo apt-get install x264 v4l-utils#自己这里可选的依赖没有装git clone https://github.com/opencv/opencv.gitcd opencv git checkout 3.3.1 cd .. git clone https://github.com/opencv/opencv_contrib.gitcd opencv_contrib git checkout 3.3.1cd ..#编译安装cd opencv mkdir buildcd build cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_C_EXAMPLES=ON \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D WITH_TBB=ON \ -D WITH_V4L=ON \ -D WITH_QT=ON \ -D WITH_OPENGL=ON \ -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \ -D BUILD_EXAMPLES=ON ..#查看cpu核数nproc make -j4 #j后面的数字代表用几个核sudo make install sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'sudo ldconfig
二、通过docker安装
参考:https://www.learnopencv.com/install-opencv-docker-image-ubuntu-macos-windows/
1.安装docker
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"sudo apt-get update apt-cache policy docker-ce sudo apt-get install -y docker-ce
2.在docker里安装opencv3镜像
docker pull spmallick/opencv-docker:opencv-3.4.1
3.启动docker镜像
docker run --device=/dev/video0:/dev/video0 -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY -p 5000:5000 -p 8888:8888 -it spmallick/opencv-docker:opencv-3.4.1 /bin/bash
报错如下
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.38/images/create?fromImage=spmallick%2Fopencv-docker&tag=opencv-3.4.1: dial unix /var/run/docker.sock: connect: permission denied
这是由于自己用了sudo 安装docker ,在用docker的时候就没有权限了。解决方法有二,1直接用sudo run 因为 /var/run/docker.sock 所属 docker 组具有 setuid 权限,所以将自己加入到docker group 里面
sudo gpasswd -a chunyan docker sudo service docker restart #重启docker服务newgrp - docker #切换当前会话到新group
之后执行
docker run --device=/dev/video0:/dev/video0 -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY -p 5000:5000 -p 8888:8888 -it spmallick/opencv-docker:opencv-3.4.1 /bin/bash
其中各个指令含义:
–device=/dev/video0:/dev/video0 allows use of webcam
-v /tmp/.X11-unix:/tmp/.X11-unix helps in X11 forwarding so that we can use functions like cv::imshow.
-e is used to pass an environment variable.
-it starts an interactive session
-p sets up a port forward. This flag maps the container’s port to a port on the host system.
/bin/bash runs .bashrc file on startup
作者:涂山容容
链接:https://www.jianshu.com/p/1b59060f90f6
共同学习,写下你的评论
评论加载中...
作者其他优质文章