308

I'm trying to install OpenCV for Python through Anaconda, but I can't seem to figure this out.

I tried

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:

http://docs.continuum.io/anaconda/pkgs.html

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 documentation for version 1.9.2?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chet
  • 18,421
  • 15
  • 69
  • 113
  • 1
    the current opencv wrapper module is called cv2. (the first one in you list is an outdated 3rd party wrapper, the 3rd one is the outdated c-api wrapper, you should use none of those) - unfortunately, i never met a person running it successfully on anaconda. can't you use a regular python 2.7 ? – berak Apr 16 '14 at 20:28
  • 5
    There's nothing with anaconda that prevents you from using it with opencv. It's just not included in the conda install except for linux. You can get install binaries files from [here](http://www.lfd.uci.edu/~gohlke/pythonlibs/) for windows. – M4rtini Apr 16 '14 at 20:31
  • ah, thanks, M4rtini, i was obviously wrong above. – berak Apr 16 '14 at 21:21
  • I'm on MacOSX. Only available on linux? whats with that? how did you find that? – Chet Apr 16 '14 at 22:54
  • @Chet In the table you linked, there's an L behind the listing of opencv. If you look beneath the table you will se that that L means it's only for Linux. – M4rtini Apr 17 '14 at 15:16
  • 5
    It works already from the standard repo: ```conda install opencv``` – fviktor Jul 18 '15 at 12:48
  • I just installed OpenCV 3 on python 3.6.1 on a windows 10 machine using https://www.solarianprogrammer.com/2016/09/17/install-opencv-3-with-python-3-on-windows/ – Farhan Ahmed Jun 25 '17 at 13:22
  • 1
    everybody. I found that using other owners of the Anaconda Cloud Repository works as well. e.g. instead of using menpo's opencv y used conda-forge's. here are the owners of several OpenCVs listed with the respective OSs: https://anaconda.org/search?q=openCV I ran this code: conda install -c conda-forge opencv=3.2.0 Good luck! – RaúlMG Jun 28 '17 at 06:55
  • why not just use pip install ? – ng0323 Nov 09 '18 at 08:15
  • @ng0323, take a look at this: https://www.anaconda.com/using-pip-in-a-conda-environment/. It is not recommended to use both pip and conda because it can result in instability. – Vivek Subramanian Mar 29 '19 at 16:15
  • 4
    With anaconda 3, type `conda install opencv` command on annaconda prompt. Installs 3.4.1 opencv and various dependent packages with it. Worked for me and also it did not require any custom installing specification. – ambar mishra Oct 14 '19 at 08:39
  • **NOTE:** There is likely a version of the package for your system available on the official Anaconda channel or the popular conda-forge. Check there before trying to install things from github or random channels, or using pip. – AMC Apr 16 '20 at 01:00
  • [OpenCV on Anaconda's website](https://anaconda.org/conda-forge/opencv). – Gonçalo Peres Nov 02 '22 at 11:21

42 Answers42

270

You can install it using binstar:

conda install -c menpo opencv
Quanlong
  • 24,028
  • 16
  • 69
  • 79
jonnycowboy
  • 2,885
  • 1
  • 13
  • 3
  • 13
    I tried using this method to install OpenCV, but am running into an issue where when I type import cv I get the error DLL load failed: The specified module could not be found. I was wondering if you ran into this issue during your install. – cogle Dec 26 '14 at 06:18
  • 5
    Worked for me on windows 7 – waldol1 Jun 09 '15 at 20:32
  • This was a bug in me copying the files, it should work now – BeRecursive Jun 30 '15 at 13:20
  • Yes! Works for me on OSX, python 3.4. Installs opencv3.0 (presumably conda's default still has opencv 2.x which wasn't py3 compatible) – Andy Hayden Aug 13 '15 at 03:35
  • 1
    Hmmm, although this doesn't allow me to `import cv2.cv` (am I missing something obvious?) – Andy Hayden Aug 13 '15 at 05:03
  • @AndyHayden `cv2.cv` was [deprecated in OpenCV3](http://stackoverflow.com/questions/26132081/is-cv2-cv-missing-in-opencv-3-0) – BeRecursive Aug 13 '15 at 19:23
  • @BeRecursive Thanks! Do you know if there is a list/porting guide somewhere (for code that uses cv2.cv functions/constants) ? e.g. cv.X becomes cv2.Y? – Andy Hayden Aug 13 '15 at 22:31
  • 2
    This works on OSX 10.10.5 with conda 13.8.4 The only "minor" issue is that it requires numpy 1.10.1 which is ok but I ran conda update --all and some libraries required a downgrade to 1.9 in order to run. – mercergeoinfo Nov 12 '15 at 09:23
  • I found that this worked for my when using PyCharm IDE, Anaconda 2.7 and Ubuntu 14.04 whereas "conda install opencv" did not. Thanks! – Phil Glau Dec 09 '15 at 05:20
  • I'm finding that cv2.VideoCapture does not work. I'm using Ubuntu 14.04, Anaconda 2.7 and PyCharm IDE. Is there a way to tell if this conda package is compiled with ffmpeg support? (I'm fairly new to both anaconda and Ubuntu) – Phil Glau Dec 14 '15 at 06:05
  • 2
    This worked fine with Anaconda 2.7 on Win10 64bit, whereas `conda install opencv` did not. – Anton Schwaighofer Apr 18 '16 at 12:34
  • This binstar package is not compiled with ffmpeg support. You can check with `print cv2.getBuildInformation().split("Video I/O:")[-1].split("\n\n")[0]`. I found that `msarahan` has 3.1 compiled with ffmpeg for OS X, but I still can't find one for linux; don't know about windows. – user2561747 Jun 06 '16 at 14:01
  • Be sure to type opencv rather than opencv3 at end of the command. It doesn't work for me with opencv3 and looks like it's because depended packages are not downgraded that caused conflicts. – linbianxiaocao Feb 02 '17 at 03:07
  • NOTE: this repo has opencv 2.4.11 as of writing. For opencv 3.2 you'll need another method. – drevicko May 18 '17 at 22:40
  • 1
    @drevicko what's the other method? – Mona Jalal Jul 08 '17 at 05:30
  • @MonaJalal to be honest I don't have one. There's quite a few install scripts out there for various setups, some of which may work. I didn't find a good solution, but didn't look especially hard. If you're super keen, you may want to update the menpo conda install script – drevicko Jul 12 '17 at 11:06
  • Using this >>> import cv2 Traceback (most recent call last): File "", line 1, in ImportError: dlopen(/Users/mona/anaconda/lib/python3.6/site-packages/cv2.cpython-36m-darwin.so, 2): Library not loaded: @rpath/libopenblasp-r0.2.19.dylib Referenced from: /Users/mona/anaconda/lib/libopencv_core.3.3.dylib Reason: image not found – Mona Jalal Oct 03 '17 at 03:27
  • mportError: DLL load failed: The specified module could not be found. – Stepan Yakovenko Dec 13 '18 at 07:28
  • @mercergeoinfo is there any way this can be resolved? I'm trying to use both `numpy` 1.15-exclusive functions (`np.unique(x,axis=0)`) and funcs from `import cv2` in my code. I am willing to try a mix of conda and pip if necessary. I'm guessing the issue is `cv2` uses old `numpy` functions which are deprecated? – Nathan majicvr.com Dec 22 '18 at 00:20
  • 1
    UPDATE : opencv-python 3.4.0.12 has supported video related functions, so we can use pip install opencv-python as normal. . Just "pip install opencv-python" and it worked :) – Dang Manh Truong Apr 03 '19 at 14:23
  • 1
    **Outdated answer.** The `menpo` channel is no longer actively maintained. Please update answer. – merv Jan 25 '22 at 17:43
152

conda install opencv currently works for me on UNIX/python2. This is worth trying first before consulting other solutions.

RussellStewart
  • 5,293
  • 3
  • 26
  • 23
128

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
>>>
Joben R. Ilagan
  • 1,900
  • 1
  • 15
  • 10
84

I have summarized my now fully working solution, OpenCV-Python - How to install OpenCV-Python package to Anaconda (Windows). Nevertheless I've copied and pasted the important bits to this post.


At the time of writing I was using Windows 8.1, 64-bit machine, Anaconda/ Python 2.x. (see notes below - this works also for Windows 10, and likely Python 3.x too).

  • NOTE 1: as mentioned mentioned by @great_raisin (thank you) in comment section however, this solution appears to also work for Windows 10.

  • NOTE 2: this will probably work for Anaconda/Python 3.x too. If you are using Windows 10 and Anaconda/Python 3.x, and this solution works, please add a comment below. Thanks! (Update: noting from comment "Working on Windows 10")

  • NOTE 3: depending on whether you are using Python 2.x or 3.x, just adjust the print statement accordingly in code snippets. i.e. in Python 3.x it would be print("hello"), and in Python 2.x it would be print "hello".

TL;DR

To use OpenCV fully with Anaconda (and Spyder IDE), we need to:

  1. Download the OpenCV package from the official OpenCV site
  2. Copy and paste the cv2.pyd to the Anaconda site-packages directory.
  3. Set user environmental variables so that Anaconda knows where to find the FFMPEG utility.
  4. 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 Anaconda 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 this OpenCV YouTube video. 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 official OpenCV site 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 OpenCV-Python Tutorials 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). For Python 3.x, I guess, just change the 2.x to 3.x accordingly.

# 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 Environmental 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     | Value                                |
|---------------------------|--------------|--------------------------------------|
| 32-bit                    | `OPENCV_DIR` | `C:\opencv\build\x86\vc12`           |
| 64-bit                    | `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...

Before:

C:\Users\Johnny\Anaconda;C:\Users\Johnny\Anaconda\Scripts

After:

C:\Users\Johnny\Anaconda;C:\Users\Johnny\Anaconda\Scripts;%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 confirm 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:

import-cv2-ok-in-anaconda-python-2.png
(source: mathalope.co.uk)

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 version...)

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:

  1. Download the OpenCV package from the official OpenCV site
  2. Copy and paste the cv2.pyd to the Anaconda site-packages directory.
  3. Set user environmental variables so that Anaconda knows where to find the FFMPEG utility.
  4. Do some testing to confirm OpenCV and FFMPEG are now working.

Good luck!

Atlas7
  • 2,726
  • 4
  • 27
  • 36
  • 5
    Brilliant answer! Note that if you're using conda environments, `cv2.pyd` should be added to the environment's `site-packages` folder (e.g. `C:\Users\cod3monk3y\Anaconda\envs\foo\Lib\site-packages\cv2.pyd`). Also worth noting, the .pyd file is [just a Windows DLL](http://stackoverflow.com/q/8822335/1174169) with a specific interface to play nicely with Python. – cod3monk3y Nov 04 '15 at 06:22
  • the code you posted above prints out true, true for me but the output is an empty 6kb video file. However, the code below writes properly to a file. (how do i get line breaks in these comments?) – aquagremlin Apr 06 '16 at 05:50
  • `import cv2` `cap = cv2.VideoCapture("BBunny_360x240_1mb.mp4")` `print cap.isOpened() ` `fourcc = cv2.VideoWriter_fourcc(*'XVID')` `out = cv2.VideoWriter("output_video.avi", fourcc, 30.0, (320, 240))` `print out.isOpened() ` `while True:` ` ret, frame = cap.read()` ` out.write(frame)` ` cv2.imshow('frame',frame)` ` if cv2.waitKey(1) & 0xFF ==ord('q'):` ` break` `cap.release()` `out.release()` `cv2.destroyAllWindows()` – aquagremlin Apr 06 '16 at 05:51
  • the bunny video i used came from here [http://www.sample-videos.com/video/mp4/240/big_buck_bunny_240p_1mb.mp4] – aquagremlin Apr 06 '16 at 06:02
  • Thank you, your post helped me a lot. I am getting an error with cv2.VideoWriter_fourcc ('module' object has no attribute 'VideoWriter_fourcc'), however I successfully read from a file which is what I wanted for a start. Do you have any clue what might be causing the above error? Thanks again. – opetroch May 15 '16 at 23:01
  • I get two times false. Don't know why. I did install FFmpeg and added it to the path. Using the "new version" code, but with the older line fourcc = cv2.cv.CV_FOURCC(*'XVID') because print cv2.isOpened() generates an Attribute error: 'module'object has no attribute 'isOpened'. Also, the cap.release in the "old version" does not make sense as it is not defined. Win 7, 64 bit, Anaconda, VS15 – jdelange Aug 02 '16 at 20:14
  • 1
    If i could up it more i would, best answer ever. I would just add, if you've installed python, uninstall it, conda has its own – Mickey Perlstein Oct 31 '16 at 16:33
  • 1
    Thanks for this! One more suggested test that took me awhile to figure out. After confirming that the video can be opened, I wanted to confirm that I could extract a frame (frame 100) as an array. In openCV 2.4.9, the command for this was cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, 100). In openCV 3.2.0, the equivalent command has changed to cap.set(cv2.CAP_PROP_POS_FRAMES, 100) – ollerend May 09 '17 at 15:05
  • 1
    Thank you sir for this awesome and well explained answer. You are making the life easier for many newbies sir ! – Ibrahim Amer Jul 06 '17 at 23:35
  • Working on Windows 10 – mixedbag99 Jul 25 '19 at 06:51
  • 2
    This is how to install opencv WITHOUT using conda. -1 – john k Aug 22 '19 at 15:41
31

It doesn't seem like the page you linked includes opencv any more. (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.

  1. Download OpenCV from https://opencv.org/releases/ and extract
  2. 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
  3. 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 file 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.

a.t.
  • 2,002
  • 3
  • 26
  • 66
eculeus
  • 1,230
  • 12
  • 17
  • 7
    Somebody also created a binstar package, which you should be able to download through Conda now: https://binstar.org/menpo/opencv/files – Ivo Flipse Aug 02 '14 at 15:09
  • can you tell me how to download opencv via conda ? I downloaded the mac package in the above link what do I do from there? – venuktan Aug 22 '14 at 07:55
  • @venuktan conda install opencv should do it. – freespace Sep 04 '14 at 11:54
  • 4
    @eculeus could you elaborate a little more on verifying ffmpeg. I can read from my webcam but am having trouble reading/writing video files. I looked in that directory 3rdparty/ffmpeg and renmaed dll to: opencv_ffmpeg300_64.dll You mention that ffmpeg should be in path. Do you mean path of windows PATH or of sys.path in python? – Paul May 14 '15 at 14:11
  • Note that you may need to import sys, then do a sys.path.append("C:/Anaconda/Lib/site-packages"). The above had suddenly stopped working and this solution came from here: http://stackoverflow.com/questions/19876079/opencv-cannot-find-module-cv2 – user391339 Jun 09 '15 at 20:08
  • Can someone please elaborate the part for the ffmpeg DLL? I found it, but I have no clue what to rename it to, where to put it, and what's meant by "path". Is that `sys.path` or `PATH`? – exhuma Jan 30 '16 at 11:47
  • 1
    Worked for me. Thanks – Indrajit Mar 23 '16 at 12:43
31

You can install OpenCV by running these commands in the Anaconda command prompt:

conda config --add channels conda-forge

conda install libopencv opencv py-opencv

Source:

https://github.com/conda-forge/opencv-feedstock

Thiago Falcao
  • 4,463
  • 39
  • 34
  • 3
    (this is duplicate existing answer, w/ edits, not sure which came first) menpo repo no longer supported, https://github.com/menpo/conda-opencv3 ; now recommended to use conda-forge https://github.com/conda-forge/opencv-feedstock – michael Sep 19 '18 at 01:34
  • 1
    If using miniconda under ubuntu 18.04; the "Solving environment" stage will take very long if you have the newest anaconda package installed. create a clean miniconda env without the anaconda bulk (e.g. `conda create --name abc python=2.7` ) – Oliver Zendel Feb 04 '19 at 19:30
25

In May 2019, the answer is as follows for MacOS:

conda update freetype
conda install opencv -c conda-forge

This updates libfreetype.6.dylib to 24.0.0 compatibility and installs OpenCV 4.1.0.

Adam Erickson
  • 6,027
  • 2
  • 46
  • 33
  • will this install "cv2" ? – Suisse May 22 '19 at 12:39
  • @AdamErickson was this in Anaconda Prompt run as Admin? – mLstudent33 May 25 '19 at 13:57
  • 2
    @mLstudent33 The command was run in Bash and the Anaconda installation was in a local folder, so no `sudo` was necessary. I would avoid running `conda` as `sudo` unless you manage a system-wide install for multiple users, which I also do. In that case, I am usually careful to activate the conda environment and then `sudo ./conda ...` from the `/bin` folder of that environment as an extra precaution, since `sudo` changes your `PATH` even when using the `-E` flag. – Adam Erickson May 26 '19 at 02:15
  • @AdamErickson this worked for me, but I got v3.4.2 instead. Any ideas? – Brett Jul 01 '19 at 14:46
  • 1
    update to above: I think I already had an earlier version of `libopencv` installed already. After removing it running `conda install opencv` installed the latest version. – Brett Jul 01 '19 at 15:17
20

The following installs opencv from conda-forge (note: tried on Windows)

conda config --add channels conda-forge
conda install opencv
michael
  • 9,161
  • 2
  • 52
  • 49
John Sick
  • 427
  • 6
  • 15
  • 4
    menpo repo no longer supported, https://github.com/menpo/conda-opencv3 ; now recommended to use conda-forge https://github.com/conda-forge/opencv-feedstock – michael Sep 19 '18 at 01:32
  • 2
    @michael really the conda-forge is the right answer at this point and it is pretty much buried in antiquated answers. :( – eric Apr 25 '19 at 12:43
  • I went ahead & added an update to this answer to remove `menpo`, and updated the syntax for installing `opencv` as per the `conda-forge` instructions. The answer itself is not fundamentally changed, as `menpo` was just for installing "newer" opencv3, and we're already on opencv4. – michael Apr 27 '19 at 02:04
16

To install OpenCV in Anaconda, start up the Anaconda command prompt and install 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__
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Taiwotman
  • 885
  • 14
  • 27
9

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 
Crapsy
  • 356
  • 2
  • 10
9

In my case, I've python 3.9 I did this in my conda env and it works :

pip install opencv-python==3.4.15.55
pip install opencv-contrib-python==3.4.15.55
  • On MacOS with Python3.9 I didn't need to qualify. I used `pip install opencv-python; pip install opencv-contrib-python` – Rian Rizvi Aug 15 '22 at 17:14
8

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   sudo   on a Unix-Based or Mac system 
#to have the needed permissions)

conda install -c conda-forge opencv
emmanuelsa
  • 657
  • 6
  • 9
  • Unless "System Administrator" is your job title and you are managing environments for others to use, you shouldn't be elevating privileges to use Conda. It only complicates the installation. – merv Jan 25 '22 at 17:51
8

it works on anaconda3 windows 10 I have already downloaded it at 5 December 2019.

Firstly, using this command:

pip install opencv-contrib-python

after that windows will ask for permission and try again:

pip install opencv-contrib-python --user

look at this it works!!

openCV using anaconda openCV using anaconda

Community
  • 1
  • 1
Kamal Hurnala
  • 99
  • 1
  • 3
  • 2
    Is there any reason to do this instead of simply installing it from conda-forge? By the way, using the base environment for development is a bad idea, just don't touch it. – AMC Apr 16 '20 at 00:54
  • As of May 2022, using Anaconda 3 or newer, installing with conda-forge is the most straightforward. – Jesuisme May 03 '22 at 15:23
7

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:

http://www.lfd.uci.edu/~gohlke/pythonlibs/

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.

OnTheContrary
  • 204
  • 4
  • 5
6

I entered the following command in the command prompt:

conda install -c menpo opencv=2.4.11

This worked for me!!!

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
aavos
  • 137
  • 2
  • 8
  • **Outdated answer.** The `menpo` channel is no longer actively maintained. Please consider updating. – merv Jan 25 '22 at 17:53
6

To insatll opencv without downgrading just hit out this in terminal particularly the user of linux mint or Ubuntu based distro

conda install -c conda-forge opencv

Hence go with it I am pretty sure that this will not disappoint you.

Aman Thakur
  • 114
  • 1
  • 2
5

On Linux, as discussed here, 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 here

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 here. There is a PR on its way to solve the issue."

oulenz
  • 1,199
  • 1
  • 15
  • 24
  • 1
    +1 came here just to post this. However, one has to keep in mind that there is an issue with loopbio's OpenCV version 3.2.0 as of today. 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 [here](https://github.com/loopbio/opencv-feedstock/issues/6). There is a PR on its way to solve the issue. – Yamaneko Jun 14 '17 at 03:49
  • 1
    This information is mostly outdated. Conda Forge coverage is more reliable these days and should be sufficient. – merv Jan 25 '22 at 17:56
5

I just installed conda 4.7.11 in Windows 10. OpenCV can be easily installed in Anaconda Navigator. After launching Navigator, click on Environments on the left panel. In the top drop-down, select Not installed. Then search for py-opencv, tick on the left checkbox to install it. It will install the dependent package libopencv. I use import cv2 in Spyder to access it.

enter image description here

kiatng
  • 2,847
  • 32
  • 39
4

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:

https://anaconda.org/menpo/opencv3

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!

dancow
  • 3,228
  • 2
  • 26
  • 28
4

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 http://opencv.org/downloads.html, follow the install instruction in http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html, (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 -j4
make install

You will find cv2.so in anaconda/lib/python2.7/site-packages.

Then:

import cv2
print cv2.__version__

It will print out 2.4.10.

My environment is GCC 4.4.6, Python 2.7 (anaconda), and opencv-2.4.10.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
liuyuyuil
  • 41
  • 1
4

To install the OpenCV package with conda, run:

conda install -c menpo opencv3=3.1.0

https://anaconda.org/menpo/opencv3

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    (this is duplicate existing answer, w/ edits, not sure which came first) menpo repo no longer supported, https://github.com/menpo/conda-opencv3 ; now recommended to use conda-forge https://github.com/conda-forge/opencv-feedstock – michael Sep 19 '18 at 01:36
  • **Outdated answer.** The `menpo` channel is no longer actively maintained. – merv Jan 25 '22 at 17:58
4

Windows only solution. OpenCV 3.x pip install for Python 3.x

Download .whl 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 is here.

FooBar167
  • 2,721
  • 1
  • 26
  • 37
4

Although not through Conda, and this is specific to Ubuntu, the easiest way to install OpenCV with all its contrib modules built in such as SIFT and SURF is to use:

pip install opencv-contrib-python

It supports Python2.7+ and Python 3.4+

If you choose not to have the contribs built in, you could instead do:

pip install opencv-python

You need to install the following dependencies below before you run those commands though:

sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

This is a good tutorial on setting OpenCV on Ubuntu: https://medium.com/@debugvn/installing-opencv-3-3-0-on-ubuntu-16-04-lts-7db376f93961

BoreBoar
  • 2,619
  • 4
  • 24
  • 39
3

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 
mallet
  • 2,454
  • 3
  • 37
  • 64
2

The correct command for installing the current version of OpenCV 3.3 in Anaconda windows:

conda install -c conda-forge opencv

or

conda install -c conda-forge/label/broken opencv

For 3.2 use this:

conda install -c menpo opencv3
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sean
  • 680
  • 7
  • 10
  • 1
    **Do not use the `broken` tag!** That opens up the solver to use every package that has been tagged as `broken`. Conda package maintainers use that tag to *prevent* packages that have identified bugs in their build from being installed. – merv Jan 25 '22 at 18:04
2

I tried following command and it works fine

conda install -c conda-forge opencv

once you hit the command it will ask for yes or no

enter image description here

If you select yes it will proceed and install all the required packages

Deepak
  • 137
  • 2
  • 8
  • This adds nothing that hasn't already been recommended previously (e.g., [this answer](https://stackoverflow.com/a/42027257/570918)). – merv Jan 25 '22 at 18:05
2

One thing i think nobody mentioned is that if you are using Windows N or KN edition, please install Media Feature Pack that is used for OpenCV. Also make sure you have Visual C++ Distributable installed. And then you can use command

conda install -c menpo opencv
Shahir Ansari
  • 1,682
  • 15
  • 21
  • 1
    The opencv package from menpo is unmaintained since late 2017, possibly earlier. Combined with the fact that both the official/main Anaconda channel and conda-forge offer opencv, there hasn't been a reason to use this in years. – AMC Apr 16 '20 at 20:20
  • The above method worked for me when i was installing it about 6 months ago. `conda-forge` didn't worked for me.But thanks for providing the details info. – Shahir Ansari Apr 17 '20 at 06:40
1

The following command works for me too. I am using an embedded IPython Notebook in Anaconda.

conda install -c https://conda.binstar.org/menpo opencv
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Felicia.H
  • 361
  • 6
  • 15
1

I have just tried on two Win32 Python 3.5 computers. At 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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hum3
  • 1,563
  • 1
  • 14
  • 21
1

I had steps in repo that will help you properly install OpenCV.

If the links if broken, see this.

For requirements and prerequisites follow this instructions:

YOU NEED TO HAVE WINDOWS OS to begin this project.

windows 7 to 10

Follow the steps carefully

  1. Install Anaconda2 32/64 python 2

    or

    Install Anaconda3 32/64 python 3

    It Depends on System Information

    Along the way you must check all the options ignore the red text

    Then wait till installation completed ...

    Open CMD admin when finish installed

    Make sure that the anaconda-python is configured in CMD admin

  2. Type: python

    This will come out:

    C:\WINDOWS\system32>python Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:27:45)

    [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.

  3. Next is to install packages:

    Numpy

    OpenCV

    PyMySQL or pymysql

    Install and Import OpenCV

    conda install -c conda-forge opencv = (version optional)

    (click yes if there's updates)

    Install and Import Numpy

    pip install numpy

    (click yes if there's updates)

    Install and Import PyMySQL/pymysql

    pip install PyMySQL or pymysql

  4. When all the package is completely installed, then you must Reboot it.

    go to CMD admin again

    type "python"

    import cv2 (cv2 is OpenCV)

    import Numpy or numpy

    import pymysql

    import os

    from PIL from Image

    if all of then is ok.. your ready to run the code!!

    After you finish all the steps, you can now test the code in SPYDER python that I use

OxLearnIT
  • 37
  • 9
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/18622902) – Yaron Jan 25 '18 at 11:11
  • @Yaron. that's probably will happen in the future. – OxLearnIT Jan 25 '18 at 11:18
  • This seems unnecessarily lengthy. More importantly, why would you use pip to install libraries like NumPy? – AMC Apr 16 '20 at 20:18
1

If you want to install opencv 3.4.0, there, unfortunately, is not this version inside conda. You need to use pip instead.

pip install opencv-python==3.4.0.12

Scott Yang
  • 339
  • 3
  • 6
0
conda install -c https://conda.anaconda.org/menpo opencv

Try this

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Kanwar Malik
  • 632
  • 5
  • 4
  • **Outdated answer.** The `menpo` channel is no longer actively maintained. Please considering updating answer. – merv Jan 25 '22 at 18:06
0

You just copy the cv2.pyd file to the C:\Users\USERNAME\Anaconda2\Lib directory.

You get the cv2.pyd file at this link (https://sourceforge.net/projects/opencvlibrary/files/).

The cv2.pyd is located at C:\Users\USERNAME\Desktop\opencv\build\python\2.7\x64.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Please do not casually recommend manually changing anything within Conda. Conda tightly orchestrates the management of dependencies. Manual tinkering can lead to undefined behavior. – merv Jan 25 '22 at 18:09
0

I just wanted to update the brilliant answer by Atlas7.

If you're using OpenCV 3, 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()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Phoenix666
  • 183
  • 1
  • 14
0

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, https://anaconda.org/ 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 https://anaconda.org, 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, you just might need to resolve some package version conflicts.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3731622
  • 4,844
  • 8
  • 45
  • 84
  • Why was this answer downvoted? It offers helpful advice beyond the numerous silly dupes of "conda install --c menpo opencv" that keep getting upvoted. May I suggest to remove or review the Caution part, because it does not add value, only clutter. – ehecatl Feb 23 '17 at 18:06
0

Try following these commands in this order:-

conda update anaconda-navigator
conda update navigator-updater
pip install opencv-python
Alisha Maini
  • 99
  • 2
  • 12
-1

I faced the same problem, but I have solved it now. This is what I did:

First enter conda install -c https://conda.binstar.org/menpo opencv in the command prompt and then find the 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mrbean
  • 171
  • 2
  • 15
  • First, `menpo` is no longer maintained, so this answer is outdated. Second, please do not casually recommend manually adding files into Conda environments. Conda tightly orchestrates the management of environments. Manual tinkering can lead to undefined behavior. – merv Jan 25 '22 at 18:14
-1

I installed it like this:

$ conda install --channel https://conda.anaconda.org/conda-forge opencv

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 does not work either. I found finally that conda-forge/opencv works for Python 3.5.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
James KR
  • 59
  • 1
  • 2
  • This information was already present in earlier answers. Please consider updating and commenting on those instead. – merv Jan 25 '22 at 18:16
-1

Windows 7, conda 4.4.10 python 2.7.6 Downloaded opencv package from Unofficial Windows Binaries for python extensions packages. (picked python 2.4 AMD 64) cd Download pip install opencv_python... python run_some.py (where import cv2 ...) seems to work. YMMV

jGc
  • 29
  • 2
-1

i was on MAC machine inside one of anaconda virutal environment. For me,

conda install -c conda-forge opencv

worked fine.

It installed opencv version 3.4.4

Hope it helps.

Anmol Saraf
  • 15,075
  • 10
  • 50
  • 60
  • This information was already available in earlier answers. Please consider upvoting those instead. – merv Jan 25 '22 at 18:17
-2

Following command adds a different anaconda channel for opencv3, you should be able to pull from it.

conda install --channel  https://mirrors.ustc.edu.cn/anaconda/cloud/menpo opencv3
ndrwnaguib
  • 5,623
  • 3
  • 28
  • 51
David
  • 35
  • 11
  • The opencv package from menpo is unmaintained since late 2017, possibly earlier. Combined with the fact that both the official/main Anaconda channel and conda-forge offer opencv, there hasn't been a reason to use this in years. – AMC Apr 16 '20 at 20:21
-4

Using Wheel files is an easier approach. If you cannot install Wheel files from the command prompt, you can use an executable pip file which exists in the <Anaconda path>/Scripts folder.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131