ubuntu18.04python3.6安装cv2
时间: 2023-06-21 21:17:08 浏览: 179
你可以按照以下步骤安装cv2:
1. 确保你已经安装了pip和python3-dev,如果没有,请使用以下命令进行安装:
```
sudo apt-get update
sudo apt-get install python3-pip python3-dev
```
2. 安装OpenCV依赖库:
```
sudo apt-get install libopencv-dev python3-opencv
```
3. 使用pip安装cv2:
```
pip3 install opencv-python-headless
```
4. 测试cv2是否成功安装:
```
python3 -c "import cv2; print(cv2.__version__)"
```
如果成功安装,则会输出cv2的版本号。
相关问题
ubuntu18.04安装opencv4.5.1
你可以按照以下步骤在 Ubuntu 18.04 中安装 OpenCV 4.5.1:
1. 更新系统软件包:
```
sudo apt update
sudo apt upgrade
```
2. 安装依赖项:
```
sudo apt install build-essential cmake git pkg-config libgtk-3-dev \
libavcodec-dev libavformat-dev libswscale-dev \
libv4l-dev libxvidcore-dev libx264-dev \
libjpeg-dev libpng-dev libtiff-dev \
gfortran openexr libatlas-base-dev \
python3-dev python3-numpy libtbb2 \
libtbb-dev libdc1394-22-dev
```
3. 克隆 OpenCV 存储库:
```
git clone https://github.com/opencv/opencv.git
```
4. 切换到克隆的存储库目录:
```
cd opencv
```
5. 创建一个构建目录并进入该目录:
```
mkdir build
cd build
```
6. 运行 CMake 生成 Makefile:
```
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=OFF \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D WITH_TBB=ON \
-D WITH_V4L=ON \
-D WITH_QT=OFF \
-D OPENCV_PYTHON3_INSTALL_PATH=/usr/local/lib/python3.6/dist-packages \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
```
7. 编译 OpenCV:
```
make -j8
```
(请注意,这里的 `-j8` 表示使用 8 个线程进行并行编译。如果你的系统核心数不同,可以相应地修改。)
8. 安装 OpenCV:
```
sudo make install
```
9. 配置动态链接库:
```
sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
```
10. 验证安装:
```
python3 -c "import cv2; print(cv2.__version__)"
```
这样就完成了在 Ubuntu 18.04 中安装 OpenCV 4.5.1。希望对你有所帮助!如有任何问题,请随时提问。
virtualbox下ubuntu18.04安装opencv
可以直接安装OpenCV 4.x或者OpenCV 3.x,具体步骤可以在终端中运行以下命令:
1. 更新系统软件包
sudo apt-get update && sudo apt-get upgrade
2. 安装OpenCV依赖
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev gfortran openexr libatlas-base-dev python-dev python-bumpy libgstreamer-plugins-base1.0-dev libavresample-dev libgphoto2-dev libopenblas-dev libwebp-dev libatlas-base-dev liblapack-dev libblas-dev libeigen3-dev libhdf5-dev libprotobuf-dev protobuf-compiler libgoogle-glog-dev libgflags-dev libtbb2 libtbb-dev libdc1394-22-dev libvorbis-dev libogg-dev libtheora-dev libx264-dev libxvidcore-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libvpx-dev libprotobuf-dev protobuf-compiler libgoogle-glog-dev libgflags-dev libtesseract-dev -y
3. 下载OpenCV源码
cd ~
git clone https://github.com/opencv/opencv.git
cd opencv
git checkout 4.0.0
4. 编译并安装OpenCV
cd ~
cd opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_NEW_PYTHON_SUPPORT=ON -D BUILD_opencv_python3=ON -D INSTALL_PYTHON_EXAMPLES=ON -D INSTALL_C_EXAMPLES=OFF -D OPENCV_ENABLE_NONFREE=ON -D WITH_CUDA=OFF -D WITH_CUDNN=OFF -D WITH_OPENGL=ON -D WITH_OPENCL=ON -D WITH_IPP=ON -D WITH_TBB=ON -D WITH_EIGEN=ON -D WITH_DNN=ON -D WITH_FFMPEG=ON -D WITH_GSTREAMER=ON -D WITH_GSTREAMER_0_10=OFF -D WITH_OPENEXR=ON -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules -D PYTHON3_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so -D PYTHON3_INCLUDE_DIR=/usr/include/python3.6m -D PYTHON3_EXECUTABLE=/usr/bin/python3 -D BUILD_EXAMPLES=ON ..
make -j8
sudo make install
安装完成后,你可以尝试使用以下命令测试是否安装成功:
python3
import cv2
print(cv2.__version__)
阅读全文