怎样用condaconda 安装opencv2

Just Do It!
如果您是直接安裝 Anaconda 或 miniconda,可以使用下面的方式來安裝 opencv:
conda install -c menpo opencv=2.4.11
conda install -c menpo opencv=2.4.11
conda install -c menpo opencv3=3.1.0
conda install -c menpo opencv3=3.1.0
參考資料:
Share this:Like this:Like Loading...
This entry was posted in
and tagged , . Bookmark the .
目前興趣在資料分析以及 Web 技術上。比較常接觸的軟體工具:Python, R, Javascript,... 等等。
目前是下面兩個社群的義工:
Kaohsiung Python User Group
Kaohsiung useR! Meetup
November 2017
6789101112
13141516171819
20212223242526
Recent Posts:
分類:分類:
Select Category
.NET&&(23)
Algorithms&&(3)
Android&&(4)
ASP.NET&&(2)
Chrome&&(2)
Cordova&&(7)
Data Mining&&(1)
Database&&(12)
Docker&&(8)
Editors&&(2)
HTML5&&(4)
Hyper-V&&(1)
JAVA&&(16)
Javascript&&(49)
Joomla&&(3)
jQuery&&(14)
Linux&&(14)
monogame&&(3)
Office&&(3)
OpenOffice&&(1)
Processing&&(8)
Python&&(176)
Search&&(1)
Security&&(1)
Sublime&&(8)
Uncategorized&&(2)
Windows&&(3)
Windows Phone&&(2)
WordPress&&(10)
X.509&&(2)
使用者介面&&(2)
創業&&(73)
各行各業&&(17)
市場分析&&(11)
微軟相關&&(4)
投資理財&&(26)
政府計畫&&(23)
機車汽車&&(1)
無線網路&&(3)
理論相關&&(5)
研究相關&&(3)
科技新知&&(3)
程式設計&&(265)
經營管理&&(9)
網路安全&&(2)
股票&&(27)
自我成長&&(4)
語言文化&&(3)
讀書心得&&(62)
軟體工具&&(10)
軟體工程&&(2)
通訊協定&&(1)
養生&&(28)
過去文章:
%d bloggers like this:How to install python opencv through Conda? - Stack Overflow
Learn, Share, Build
Each month, over 50 million developers come to Stack Overflow to learn, share their knowledge, and build their careers.
Join the world’s largest developer community.
Display name
Email address
By registering, you agree to the
I'm trying to install opencv for python through anaconda, but I can't seem to figure this out.
conda install opencv
conda install cv2
I also tried searching
conda search cv
No cigar. I ran across this which lists opencv as an included package:
After running conda info I noticed my version is 3.4.1 but I couldn't seem to find any information about this version online.
I'm pretty confused about this.
Am I missing something pretty obvious here? If opencv was available for a previous version of anaconda, then why wouldn't it be available for the newer version? And why does that link only show me docs for version 1.9.2?
4,51063468
You can install it using binstar:
conda install -c menpo opencv
7,13873362
Did you find this question interesting? Try our newsletter
Sign up for our newsletter and get our top new questions delivered to your inbox ().
Subscribed!
Success! Please click the link in the confirmation email to activate your subscription.
This worked for me (on Ubuntu and conda 3.18.3):
conda install --channel https://conda.anaconda.org/menpo opencv3
The command above was what was shown to me when I ran the following:
anaconda show menpo/opencv3
This was the output:
To install this package with conda run:
conda install --channel https://conda.anaconda.org/menpo opencv3
I tested the following in python without errors:
&&& import cv2
conda install opencv currently works for me on UNIX/python2. This is worth trying first before consulting other solutions.
I have summarized my now fully working solution . Nevertheless I've copied and pasted the important bits to this post.
Currently, I am using Windows 8.1 and 64-bit machine,
as IDE for Python 2.x.
Note: if you are on Windows 10 (or above) and if the below instruction works, please could you kindly add a comment? This will help out the community a lot! :-)
To use OpenCV fully with Anaconda (and Spyder IDE), we need to:
Download the OpenCV package from
Copy and paste the cv2.pyd to the Anaconda site-packages directory.
Set user environmental variables so that Anaconda knows where to find the FFMPEG utility.
Do some testing to confirm OpenCV and FFMPEG are now working.
(Read on for the detail instructions...)
Prerequisite
Install Anaconda
Anaconda is essentially a nicely packaged Python IDE that is shipped with tons of useful packages, such as NumPy, Pandas, IPython Notebook, etc. It seems to be recommended everywhere in the scientific community. Check out
to get it installed.
Install OpenCV-Python to Anaconda
Cautious Note: I originally tried out installing the binstar.org opencv package, as suggested. That method however does not include the FFMPEG codec - i.e. you may be able to use OpenCV but you won't be able to process videos.
The following instruction works for me is inspired by . So far I have got it working on both my Desktop and Laptop. Both 64-bit machines and Windows 8.1.
Download OpenCV Package
Firstly, go to the
to download the complete OpenCV package. Pick a version you like (2.x or 3.x). I am on Python 2.x and OpenCV 3.x - mainly because this is how the
are setup/based on.
In my case, I've extracted the package (essentially a folder) straight to my C drive. (C:\opencv).
Copy and Paste the cv2.pyd file
The Anaconda Site-packages directory (e.g. C:\Users\Johnny\Anaconda\Lib\site-packages in my case) contains the Python packages that you may import. Our goal is to copy and paste the cv2.pyd file to this directory (so that we can use the import cv2 in our Python codes.).
To do this, copy the cv2.pyd file...
From this OpenCV directory (the beginning part might be slightly different on your machine):
# Python 2.7 and 32-bit machine:
C:\opencv\build\python\2.7\x84
# Python 2.7 and 64-bit machine:
C:\opencv\build\python\2.7\x64
To this Anaconda directory (the beginning part might be slightly different on your machine):
C:\Users\Johnny\Anaconda\Lib\site-packages
After performing this step we shall now be able to use import cv2 in Python code. BUT, we still need to do a little bit more work to get FFMPEG (video codec) to work (to enable us to do things like processing videos.)
Set Enviromental Variables
Right-click on "My Computer" (or "This PC" on Windows 8.1) -> left-click Properties -> left-click "Advanced" tab -> left-click "Environment Variables..." button.
Add a new User Variable to point to the OpenCV (either x86 for 32-bit system or x64 for 64-bit system.) I am currently on a 64-bit machine.
| 32-bit or 64 bit machine? | Variable
|---------------------------|--------------|--------------------------------------|
| `OPENCV_DIR` | `C:\opencv\build\x86\vc12`
| `OPENCV_DIR` | `C:\opencv\build\x64\vc12`
Append %OPENCV_DIR%\bin to the User Variable PATH.
For example, my PATH user variable looks like this...
C:\Users\Johnny\AC:\Users\Johnny\Anaconda\Scripts
C:\Users\Johnny\AC:\Users\Johnny\Anaconda\S%OPENCV_DIR%\bin
This is it we are done! FFMPEG is ready to be used!
Test to confirm
We need to test whether we can now do these in Anaconda (via Spyder IDE):
Import OpenCV package
Use the FFMPEG utility (to read/write/process videos)
Test 1: Can we import OpenCV?
To confrim that Anaconda is now able to import the OpenCV-Python package (namely, cv2), issue these in the IPython Console:
import cv2
print cv2.__version__
If the package cv2 is imported ok with no errors, and the cv2 version is printed out, then we are all good! Here is a snapshot:
Test 2: Can we Use the FFMPEG codec?
Place a sample input_video.mp4 video file in a directory. We want to test whether we can:
read this .mp4 video file, and
write out a new video file (can be .avi or .mp4 etc.)
To do this we need to have a test python code, call it test.py. Place it in the same directory as the sample input_video.mp4 file.
This is what test.py may look like (I've listed out both newer and older version codes here - do let us know which one works / not work for you!):
(Newer verison...)
import cv2
cap = cv2.VideoCapture("input_video.mp4")
print cap.isOpened()
# True = read video successfully. False - fail to read video.
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter("output_video.avi", fourcc, 20.0, (640, 360))
print out.isOpened()
# True = write out video successfully. False - fail to write out video.
cap.release()
out.release()
(or the older version...)
import cv2
cv2.VideoCapture("input_video.mp4")
print cv2.isOpened()
# True = read video successfully. False - fail to read video.
fourcc = cv2.cv.CV_FOURCC(*'XVID')
out = cv2.VideoWriter("output_video.avi",fourcc, 20.0, (640,360))
print out.isOpened()
# True = write out video successfully. False - fail to write out video.
cap.release()
out.release()
This test is VERY IMPORTANT. If you'd like to process video files, you'd need to ensure that Anaconda / Spyder IDE can use the FFMPEG (video codec). It took me days to have got it working. But I hope it would take you much less time! :)
Note: one more very important tip when using the Anaconda Spyder IDE. Make sure you check the Current Working Directory (CWD)!!!
Conclusion
To use OpenCV fully with Anaconda (and Spyder IDE), we need to:
Download the OpenCV package from
Copy and paste the cv2.pyd to the Anaconda site-packages directory.
Set user environmental variables so that Anaconda knows where to find the FFMPEG utility.
Do some testing to confirm OpenCV and FFMPEG are now working.
Good luck!
Doesn't seem like the page you linked includes opencv anymore. (Funny, I do recall it being included at a previous point as well.)
In any case, installation of OpenCV into Anaconda, although unavailable through conda, is pretty trivial. You just need to download one file.
Download OpenCV from
and extract
From the extracted folder, copy the file from the extracted directory: opencv/build/python/2.7/(either x86 or x64, depending on your Anaconda version)/cv2.pyd to your Anaconda site-packages directory, e.g., C:\Anaconda\Lib\site-packages
To get ffmpeg within opencv to work, you'll have to add the directory that ffmpeg is located in to the path (e.g., opencv/sources/3rdparty/ffmpeg). Then you'll have to find the dll in that folder (e.g., opencv_ffmpeg_64.dll) and copy or rename it to a filename that includes the opencv version you are installing, (e.g., opencv_ffmpeg249_64) for 2.4.9.
Now at the python prompt you should be able to type "import cv2"...to verify that it works, type "print cv2.__version__" and it should print out the OpenCV version you downloaded.
To install opencv in Anaconda start up the Anaconda command prompt
and install the opencv with
conda install -c https://conda.anaconda.org/menpo opencv3
Test that it works in your Anaconda Spyder or IPython console with
import cv2
You can also check the installed version using
cv2.__version__
I had exactly the same problem, and could not get conda to install OpenCV. However, I managed to install it with the OpenCV installer you find at this site:
His files are "Wheel" whl files that can be installed with pip, e.g.
pip install SomePackage-1.0-py2.py3-none-any.whl
in a command window. It worked with Spyder directly after executing this command for me. I have had the same experience with other packages, the above UC Irvine site is a gold mine.
I entered the following command in the command prompt:
conda install -c menpo opencv=2.4.11
This worked for me!!!
3,60041632
You can install OpenCV 3 by running this command in the Anaconda command prompt:
conda install -c menpo opencv3
It worked in Windows 10 and Ubuntu 14.
3,60041632
To install OpenCv with conda on Windows-64 and Python 3.5, the only one that worked for me is:
conda install -c conda-forge opencv=3.1.0
Like others, I had issues with Python 3.5.1/Anaconda 2.4.0 on OS X 10.11..
But I found a compatible package here:
It can be installed via the command line like so:
conda install -c https://conda.anaconda.org/menpo opencv3
Worked like a charm. First time I've ever gotten OpenCV to work on 3.x!
1,48911219
To install opneCV package with conda run:
conda install -c menpo opencv3=3.1.0
15.1k124972
On Linux, as discussed , the best way to get opencv at present is from loopbio at conda-forge:
conda install -c loopbio -c conda-forge -c pkgw-forge ffmpeg-feature ffmpeg gtk2 opencv
If you have 'a modern CPU' there exists also a compiled version "enabling all modern CPU instruction set extensions [...] and against libjpeg-turbo":
conda install -c loopbio -c conda-forge -c pkgw-forge ffmpeg-feature ffmpeg gtk2 opencv-turbo`
Two of the solutions mentioned in other answers don't work unconditionally:
The conda you get through conda install opencv or pip install opencv-python doesn't have gtk2 support, so you can't display images through imshow.
Conda built by Menpo (conda install -c menpo opencv3) has gtk2 support, but
they have only built OpenCV 3.2 for Python 3.5, not Python 3.6
Ubuntu 16.10 has deprecated libpng12, leading to a missing dependency and the following error when trying to import cv2: ImportError: libpng12.so.0: cannot open shared object file: No such file or directory, as discussed
EDIT: @Yamaneko points out that as of 14 June 2017, "there is an issue with loopbio's OpenCV version 3.2.0. It silently fails to read and write videos. It is due to a combination of an upstream OpenCV issue in combination with an old GCC. More details . There is a PR on its way to solve the issue."
I think the easiest option that should work cross-platform and across various versions of Anaconda is
#Run Anaconda Prompt as an Administrator (on Windows) & execute the command below
#(you may need to use
on a Unix-Based or Mac system
#to have the needed permissions)
conda install -c conda-forge opencv
Windows only solution. OpenCV 3.x pip install for Python 3.x
file (cpMN where you have Python M.N). contrib includes OpenCV-extra packages. For example, assuming you have Python 3.6 and Windows 64-bit, you might download opencv_python-3.2.0+contrib-cp36-cp36m-win_amd64.whl
From command prompt type:
pip install opencv_python-3.2.0+contrib-cp36-cp36m-win_amd64.whl
You'll have a package in your conda list : opencv-python 3.2.0+contrib &pip&
Now you could test it (no errors):
&&& import cv2
Original source page where I took the information .
An easy and straight forward solution is to install python3.5 first before installing opencv3
conda install python=3.5
followed by
conda install --channel https://conda.anaconda.org/menpo opencv3
conda install -c https://conda.anaconda.org/menpo opencv
29.1k1074154
I have just tried on two win32 Python 3.5 computers.
On the first I was able to conda install opencv but it didn't work nor did the version from menpp but this did conda install -c https://conda.binstar.org/conda-forge opencv.
Correct command for installing current version of OpenCV 3.3 in anaconda windows:
conda install -c conda-forge opencv
conda install -c conda-forge/label/broken opencv
for 3.2 use this:
conda install -c menpo opencv3
If conda install opencv
or conda install -c https://conda.binstar.org/menpo opencv does not work, you can try to compile from the source.
Download the source from , follow the install instruction in , (maybe you can jump to the last part directly, 'Building OpenCV from Source Using CMake...), change the cmake command as following:
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/home/**/env/opencv-2.4.10 -D BUILD_NEW_PYTHON_SUPPORT=ON -D PYTHON_EXECUTABLE=/home/**/env/anaconda/bin/python -D PYTHON_INCLUDE_DIR=/home/**/env/anaconda/include/python2.7 -D PYTHON_LIBRARY=/home/**/env/anaconda/lib/libpython2.7.so -D PYTHON_PACKAGES_PATH=/home/**/env/anaconda/lib/python2.7/site-packages -D PYTHON_NUMPY_INCLUDE_DIRS=/home/**/env/anaconda/lib/python2.7/site-packages/numpy/core/include ..
make install
You will find cv2.so in anaconda/lib/python2.7/site-packages
import cv2
print cv2.__version__
It will print out 2.4.10
My environment is gcc 4.4.6, python 2.7(anaconda), opencv-2.4.10.
You just copy the cv2.pyd file to the C:\Users\USERNAME\Anaconda2\Lib
You get the cv2.pyd file at this link()
The cv2.pyd is located at C:\Users\USERNAME\Desktop\opencv\build\python\2.7\x64
The following command works for me too. I am using embeded Ipython Notebook in anacoda.
conda install -c https://conda.binstar.org/menpo opencv
Just wanted to update the brilliant answer by atlas7
if your using opencv3 change the test code to the following:
import cv2
cap=cv2.VideoCapture("input_video.mp4")
print cap.isOpened()
# True = read video successfully. False - fail to read video.
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
print out.isOpened()
# True = write out video successfully. False - fail to write out video.
cap.release()
out.release()
$ conda install --channel https://conda.anaconda.org/conda-forge opencv
I installed like above. I tried conda install opencv directly but it does not work for me since I am using Python 3.5 which is higher version that default opencv library in conda. Later, I tried 'anaconda/opencv' but it also does not works. I found finally that conda-forge/opencv works for Python 3.5.
Using Wheel files is easier approach. If you cannot install Wheel files in command prompt, you can use executable pip file which exists in /Scripts folder.
Here's a general approach to using conda to install packages for python that applies:
1) conda search packageName
e.g. conda search opencv
2) If this doesn't return results, conda install packageName will not work
3) At this point you can go to,
and type the packageName into the search box.
If this pulls up results (which it should for opencv), then click on one of the results that is for your platform (e.g. win-64).
The next page will show you the command to use to install this package (e.g. conda install -c menpo opencv=2.4.11).
4) If your package doesn't return results by search , then you can try pip install packageName.
Caution: when I used step 3 to install opencv for win-64, I got an error when I tried to import cv2.
Here is the error:
RuntimeError: module compiled against API version a but this version of numpy is 9
Traceback (most recent call last):
File "&stdin&", line 1, in &module&
ImportError: numpy.core.multiarray failed to import
I think the error is due to package version conflicts.
Nevertheless, this is a valid way to install opencv and other python packages, just might need to resolve some package version conflicts.
I tried to use Minoconda on my Raspberry Pi (Raspibian OS), but I could not get Open CV for my platform. Finally the following command worked:
$ sudo apt-get install ipython python-opencv python-scipy python-numpy python-setuptools python-pip
I faced the same problem but solved it now. This is what I did:
First enter conda install -c https://conda.binstar.org/menpo opencvin the command prompt and then find this path Anaconda\pkgs\opencv-2.4.9.1-np19py27_0\Lib\site-packages. Now copy all the files present here into Anaconda\Lib\site-packages. Now you will be able to use OpenCV with python.
3,60041632
protected by ♦
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10
on this site (the ).
Would you like to answer one of these
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled实战anaconda2/3同时安装opencv - 简书
实战anaconda2/3同时安装opencv
最近在学习无人驾驶汽车的课程,需要安装Anaconda3+opencv,因为平时Python2用的也比较多,所以不想卸载Anaconda2,于是考虑二者如何同时并存。windows环境下安装
Anaconda2中安装opencv其实很容易,只要下载opencv2解压缩,将
\opencv\build\python\2.7\x64\cv2.pyd拷贝到Anaconda2的 \Anaconda2\Lib\site-packages目录下即可。但是在Anaconda3中安装opencv3却遇到很大麻烦,下载opencv3的python安装包有3个途径:1、使用命令 & pip install pillow& conda install -c menpo opencv3=3.1.0从上的,可惜无论如何下不下来;2、从上找,但是只有linux下的安装包,没有Windows的;3、从下载所需的版本,这里倒是能下载了,但是安装包在Cmd中安装时却提示平台不支持,我勒个去。这时查到一篇,提出了建立虚拟环境的思路。1、安装主版本,这里选择Anaconda2,假定安装目录为D:\Anaconda2;2、安装完成后勾选两个选项:&1&将安装目录加入到系统的PATH环境变量中,以后在CMD中便可以直接用python命令启动python;&2&让其他IDE能够检测到Anaconda2并将Anaconda2作为默认的Python 2.7;3、安装好Anaconda2之后,再安装Anaconda3,这里Anaconda3的安装目录必须选在D:\Anaconda2\envs子目录下,如图所示,最后的”py3”可以自己另取;4、这里取消掉那两个勾(按照字面意思理解,打上第二个勾应该是没有影响的。至于第一个选项,由于之前已经把Anaconda2添加到系统PATH路径下了,因此再把Anaconda3添加进去,由于顺序在Anaconda2的后面,几乎是没有作用的,所以这里没有勾上);5、安装完成之后,在CMD里面直接输入python会启动Python2,而使用activate py3(py3即之前Python3安装目录文件夹的名字)命令之后,再使用python即可切换至Python3。使用activate py3命令之后,在命令行前面会出现一个[py3]标记,此时使用任何的python命令都是在Python3下进行的。使用deactivate命令可取消激活Python3。经测试,可以实现anaconda2/3同时安装。这里需要注意的如果想在Spyder中使用不同的Python内核,需要进行切换:spyder-tools-preferences-interpreter,默认是anaconda3的python解释器,可以选use the following python interpreter指定想要的python解释器。出现异常再启动一次即可。到这里anaconda2/3同时安装已经实现,但是同时安装opencv库还没实现,原因如上,巧妇难为无米之炊,怎么办?Ubuntu下安装:首先还是按照windows下的思路,安装Anaconda2/3并存的环境。注意安装命令为:& conda create --name=yourNewEnvironment python=3 anaconda安装目录为Anaconda2\envs。输入python进入的是python2,激活和退出Python3的命令分别为& source activate py3& source deactivate py3在清华的镜像网站上下载对应opencv安装包,进入需使用的环境,使用命令& conda install 安装包名anaconda2/3同时安装opencv就成功了。spyder可以根据需要在不同环境下使用(可能需要重启几次,原因不明)。}

我要回帖

更多关于 conda opencv3 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信