Install OpenCv for python in Linux

Open cv is a powerful tool for image processing!
I’ll guided you to install open cv for python in Linux platform.

0. Anaconda

If you installed Anacondain you computer, you may install opencvby this command:

conda install opencv

But this may not work, since in Linux, a lot of differences existed.
You may meet this mistakes:

Thefunction is not implemented. Rebuild the library with Windows, GTK+ 2.x orCarbon support. If you are on Ubuntu or Debian, install libgtk2.0‑dev and pkg‑config, then re‑runcmake or configure script

If so, you should use conda uninstall opencv and then go to the following steps.

1. Follow the official guidance

https://docs.opencv.org/trunk/d2/de6/tutorial_py_setup_in_ubuntu.html

2. Refer to website

https://jingyan.baidu.com/article/14bd256e466474bb6d2612db.html

3. Mistakes I meet

3.1 ‘NppiGraphcutState’

When I reached 95, I got ‘NppiGraphcutState’ has not been declared as the follow website
https://devtalk.nvidia.com/default/topic/986950/opencv-installation-problem-nppigraphcutinitalloc-not-declared/

This is caused by the Incompatible between cuda8.0 and opencv2(or opencv3).
To solve this:
Modify opencv-3.0.0/modules/cudalegacy/src/graphcuts.cppfile.
In detail:
change

#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) 

to

#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) || (CUDART_VERSION >= 8000)

Then, re-make. I passed.

3.1 import error

Even I finished make process and I also did

sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'

sudo ldconfig

But I still can’t import cv2in python.
To solve this, I used

cd /home/xx/anaconda2/lib/python2.7/site-packages/
ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so

Thing done!

<think>我们正在处理用户的问题:用户希望使用pip安装opencv-python包,并且想要通过清华源加速下载。 根据引用[1]和引用[2]的建议,我们可以使用清华大学的镜像源来加速安装。 因此,我们推荐用户使用以下命令: pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple 但是,请注意: 1. 如果用户使用的是Windows系统,可以按照引用[2]的步骤:打开命令提示符(win+R,然后输入cmd),再输入上述命令。 2. 如果用户遇到SSL证书问题,可以添加`--trusted-host`参数(如引用[1]所示): pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn 另外,需要提醒用户: - 确保pip版本是最新的,因为旧版本的pip可能不支持某些功能或镜像源。 - 如果用户使用了虚拟环境,请确保在虚拟环境中执行命令。 如果用户遇到其他问题,比如权限问题,可以尝试在命令前加上`sudo`(Linux/macOS)或者以管理员身份运行命令提示符(Windows)。 最后,生成相关问题。</think>### 使用清华源安装 OpenCV-Python 的完整指南 要使用清华大学镜像源加速安装 OpenCV-Python,请执行以下命令: ```bash pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn ``` #### 关键参数说明: 1. `-i https://pypi.tuna.tsinghua.edu.cn/simple` 指定清华大学 PyPI 镜像源,大幅提升下载速度(尤其在国内) 2. `--trusted-host pypi.tuna.tsinghua.edu.cn` 跳过 SSL 证书验证,解决可能出现的信任错误[^1][^2] #### 验证安装: 安装完成后,运行 Python 验证: ```python import cv2 print(cv2.__version__) # 应显示版本号如 4.9.0 ``` #### 常见问题解决方案: | 问题类型 | 解决方案 | |---------|---------| | 权限错误 | 添加 `--user` 参数:`pip install --user opencv-python...` | | 网络超时 | 添加超时参数:`--default-timeout=100` | | 版本冲突 | 指定版本:`opencv-python==4.8.0` | | 缺少依赖 | 先安装:`pip install numpy scipy matplotlib` | #### 其他镜像源备选: ```bash # 阿里云 pip install opencv-python -i https://mirrors.aliyun.com/pypi/simple # 腾讯云 pip install opencv-python -i https://mirrors.cloud.tencent.com/pypi/simple # 华为云 pip install opencv-python -i https://repo.huaweicloud.com/repository/pypi/simple ``` > **注意**:OpenCV-Python 包含多个变体包: > - `opencv-python`:基础版本(推荐) > - `opencv-contrib-python`:包含额外模块 > - `opencv-python-headless`:无 GUI 支持(服务器环境) 安装完成后,可通过以下代码测试基础功能: ```python import cv2 # 读取图像 img = cv2.imread('test.jpg') # 转换为灰度图 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 显示结果 cv2.imshow('OpenCV Test', gray) cv2.waitKey(0) ``` [^1]: 引用自用户提供的指令建议 [^2]: 引用自用户提供的清华源安装方法 --- **
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值