不多说,直接上干货!
说明:
Anaconda2-5.0.0-Windows-x86_64.exe安装下来,默认的Python2.7
Anaconda3-4.2.0-Windows-x86_64.exe安装下来,默认的Python3.5
Anaconda3-5.0.0-Windows-x86_64.exe安装下来,默认的Python3.6
参考
全网最全最详细的Windows下安装Anaconda2 / Anaconda3(图文详解)
1、 以下是在Windows下Anaconda2里正确下载安装OpenCV(离线方式)
因为python装的是Anaconda2版本,所以对应着选择,就安装
https://www.lfd.uci.edu/~gohlke/pythonlibs/
PS C:\Anaconda2\Lib\site-packages> pip install opencv_python-2.4.13.2-cp27-cp27m-win_amd64.whl Processing c:\anaconda2\lib\site-packages\opencv_python-2.4.13.2-cp27-cp27m-win_amd64.whl Installing collected packages: opencv-python Successfully installed opencv-python-2.4.13.2 PS C:\Anaconda2\Lib\site-packages>
测试一下,输python
进入python,输入import cv2
回车,不报错就说明安装配置成功了
PS C:\Anaconda2\Lib\site-packages> python Python 2.7.13 |Anaconda, Inc.| (default, Sep 19 2017, 08:25:59) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> >>> import cv2 >>>
对于这种方式,我尝试了,是可以行得通的。
2、 以下是在Windows下Anaconda3里正确下载安装OpenCV(离线方式)
因为python装的是Anaconda3版本,所以对应着选择,就安装
PS C:\Anaconda3\Lib\site-packages> pip install opencv_python-3.3.1-cp36-cp36m-win_amd64.whl opencv_python-3.3.1-cp36-cp36m-win_amd64.whl is not a supported wheel on this platform. PS C:\Anaconda3\Lib\site-packages>
如果还出现这个情况,则换个版本试试。这个很简单,尝试这个版本
PS C:\Anaconda3\Lib\site-packages> pip install opencv_python-3.3.0-cp36-cp36m-win_amd64.whl Processing c:\anaconda3\lib\site-packages\opencv_python-3.3.0-cp36-cp36m-win_amd64.whl Installing collected packages: opencv-python Successfully installed opencv-python-3.3.0 PS C:\Anaconda3\Lib\site-packages>
成功!这样的安装方式是没问题的。
import cv2成功。
3、以下是在Windows下Anaconda3里的envs里正确下载安装OpenCV(离线方式)
PS C:\Anaconda3\envs\python35\Lib\site-packages> pip install opencv_python-3.3.0-cp36-cp36m-win_amd64.whl Requirement already satisfied: opencv-python==3.3.0 from file:///C:/Anaconda3/envs/python35/Lib/site-packages/opencv_python-3.3.0-cp36-cp36m-win_amd64.whl in c:\anaconda3\lib\site-packages PS C:\Anaconda3\envs\python35\Lib\site-packages>
一样的,因为这个envs环境是通过如下创建得到的,所以,是寄生在Anaconda3里。
这种方式,也是成功的。
如果大家中间有遇到如下的问题,则对应解决。
C:\Users\lenovo>python Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 19 2017, 08:03:39) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> >>> import cv2 RuntimeError: module compiled against API version 0xb but this version of numpy is 0xa Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: numpy.core.multiarray failed to import >>> >>> import numpy >>> exit() C:\Users\lenovo> C:\Users\lenovo>pip install numpy --upgrade Exception: Traceback (most recent call last): File "C:\Anaconda3\lib\site-packages\pip\basecommand.py", line 215, in main status = self.run(options, args) File "C:\Anaconda3\lib\site-packages\pip\commands\install.py", line 335, in run wb.build(autobuilding=True) File "C:\Anaconda3\lib\site-packages\pip\wheel.py", line 749, in build self.requirement_set.prepare_files(self.finder) File "C:\Anaconda3\lib\site-packages\pip\req\req_set.py", line 380, in prepare_files ignore_dependencies=self.ignore_dependencies)) File "C:\Anaconda3\lib\site-packages\pip\req\req_set.py", line 487, in _prepare_file req_to_install, finder) File "C:\Anaconda3\lib\site-packages\pip\req\req_set.py", line 428, in _check_skip_installed req_to_install, upgrade_allowed) File "C:\Anaconda3\lib\site-packages\pip\index.py", line 465, in find_requirement all_candidates = self.find_all_candidates(req.name) File "C:\Anaconda3\lib\site-packages\pip\index.py", line 423, in find_all_candidates for page in self._get_pages(url_locations, project_name): File "C:\Anaconda3\lib\site-packages\pip\index.py", line 568, in _get_pages page = self._get_page(location) File "C:\Anaconda3\lib\site-packages\pip\index.py", line 683, in _get_page return HTMLPage.get_page(link, session=self.session) File "C:\Anaconda3\lib\site-packages\pip\index.py", line 811, in get_page inst = cls(resp.content, resp.url, resp.headers) File "C:\Anaconda3\lib\site-packages\pip\index.py", line 731, in __init__ namespaceHTMLElements=False, TypeError: parse() got an unexpected keyword argument 'transport_encoding' C:\Users\lenovo>
如果运行下来,还是没得到解决,则
再尝试下,
问题出现的原因是numpy的版本较高,不能与opencv2兼容
解决办法:
pip install -U numpy
即可将numpy恢复到合适的版本(这里为1.9.2)
再 import cv2 就不会出现上面的问题了。
成功!
有时候,你可能得卸载Anaconda3里已经安装好了的opencv,再来安装。(为什么呢?是因为你可能没看仔细,下错版本了)
成功!
同样,对于如果你是Anaconda3-4.2.0-Windows-x86_64.exe安装下来,默认的Python3.5,则如下
成功!
4、以下是在Windows下Anaconda3里正确下载安装OpenCV(在线方式)(注意这里有坑,一定要按照我的来)
(C:\Anaconda3) C:\Users\lenovo>pip install opencv-python Collecting opencv-python Cache entry deserialization failed, entry ignored Downloading opencv_python-3.3.0.10-cp35-cp35m-win_amd64.whl (39.7MB) 100% |████████████████████████████████| 39.7MB 25kB/s Requirement already satisfied (use --upgrade to upgrade): numpy>=1.11.1 in c:\anaconda3\lib\site-packages (from opencv-python) Installing collected packages: opencv-python Successfully installed opencv-python-3.3.0.10 You are using pip version 8.1.2, however version 9.0.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command. (C:\Anaconda3) C:\Users\lenovo>
验证下,出现如下的错误
(C:\Anaconda3) C:\Users\lenovo>pip install opencv-python Collecting opencv-python Cache entry deserialization failed, entry ignored Downloading opencv_python-3.3.0.10-cp35-cp35m-win_amd64.whl (39.7MB) 100% |████████████████████████████████| 39.7MB 25kB/s Requirement already satisfied (use --upgrade to upgrade): numpy>=1.11.1 in c:\anaconda3\lib\site-packages (from opencv-python) Installing collected packages: opencv-python Successfully installed opencv-python-3.3.0.10 You are using pip version 8.1.2, however version 9.0.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command. (C:\Anaconda3) C:\Users\lenovo> (C:\Anaconda3) C:\Users\lenovo>python Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> >>> import cv3 Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named 'cv3' >>> import cv2 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Anaconda3\lib\site-packages\cv2\__init__.py", line 9, in <module> from .cv2 import * ImportError: DLL load failed: 找不到指定的模块。 >>>
尝试解决
>>> import cv2 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Anaconda3\lib\site-packages\cv2\__init__.py", line 9, in <module> from .cv2 import * ImportError: DLL load failed: 找不到指定的模块。 >>> pip install -U numpy File "<stdin>", line 1 pip install -U numpy ^ SyntaxError: invalid syntax >>> exit() (C:\Anaconda3) C:\Users\lenovo>pip install -U numpy Cache entry deserialization failed, entry ignored Collecting numpy Downloading numpy-1.13.3-cp35-none-win_amd64.whl (13.1MB) 100% |████████████████████████████████| 13.1MB 74kB/s Installing collected packages: numpy Found existing installation: numpy 1.11.1 Uninstalling numpy-1.11.1: Successfully uninstalled numpy-1.11.1 Successfully installed numpy-1.13.3 You are using pip version 8.1.2, however version 9.0.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command. (C:\Anaconda3) C:\Users\lenovo> (C:\Anaconda3) C:\Users\lenovo>
可以看到,如果你是直接这么来安装,是错误的。为什么呢?按照我的来,先卸载吧。
于是,我把它给卸载了,带大家来重新做一遍。
解决办法:
https://github.com/skvark/opencv-python/issues/36
什么意思,很简单。
(1)下载安装 Visual C++ Redistributable for Visual Studio 2015 不难,别嫌麻烦。
(2) 把下载的python3.dll放到你所安装Anaconda3的目录下即可。
我的是这样的。
然后,再尝试pip install opencv-python执行完后,再import cv2就可以成功了
成功!
5、也可以直接在Pycharm里搜索并安装Opencv
这个不多赘述
总结
很多人写的对于OpenCV都是冰山一角,其实没有大家想的那么复杂,只是没有多尝试罢了。
无论是Python2还是Python3系列,都可以安装,且离线和在线安装成功。
提醒的是,注意对应自己的版本去安装。
本文转自大数据躺过的坑博客园博客,原文链接:http://www.cnblogs.com/zlslch/p/8059715.html,如需转载请自行联系原作者