Friday, May 4, 2012

Install OpenCV in Windows for Python

Hi Friends,

Here I will tell you how to install OpenCV 2.4x in Windows  for Python 2.7.

Pre-requisites ( need to be downloaded ) :

Python : Download latest version of Python 2.7 from Python site.
Numpy : Download Numpy for Python 2.7 from here.
OpenCV 2.4 : Download OpenCV for windows from here.

Install:


1)  First install Python 2.7. Leave all settings as default. In that case, Python will be installed in default folder C:\Python27\


2) Now install Numpy. Again leave everything default. Numpy will find Python directory and will be installed to most appropriate folder.

3) Now double-click OpenCV.exe. It will ask for extraction folder. Give it as just C:\. It will extract all files to C:\opencv\  . Wait until everything is extracted.


4) Now copy everything in the folder C:\opencv\build\python\x86\2.7\ ( most probably, there will be only one file cv2.pyd ) and paste it in the folder C:\Python27\Lib\site-packages\


5) Now open your "Python IDLE" ( from Start > All Programmes > Python 2.7 > Python IDLE ) and just type following :
import cv2


If everything OK, it will import cv2 module, otherwise an error message will be shown.

So it is very simple procedure. Try it yourself and let me know how it goes.

NB : Even if you are using 64-bit windows, do the same procedure. ( Better don't go for 64-bit Python and Numpy )

With Regards,
ARK


54 comments:

  1. I will try later

    ReplyDelete
  2. worked perfectly. thanks very much!!

    ReplyDelete
  3. Thank you! The OpenCV page says there should be a binary download on sourceforge, but it only extracts files. This is exactly what I needed

    ReplyDelete
  4. Thank you, it works fine, but I still cannot open video files(
    I'm trying to read it frame by frame, but cv2.VideoCapture(file name).grab() always return false.

    ReplyDelete
  5. None of the programs work though and all give the "cv is not defined error" I am a newbie and help on this will be appreciated !

    ReplyDelete
    Replies
    1. why cv? All the programmes uses cv2. So use import cv2.

      Delete
  6. You are awesome! Thanks so much for the update. Can't find this anywhere. Saved me a load of frustration!

    ReplyDelete
  7. Hey Abid,
    Thanks for the post! I tried following your guide and was able to iport cv2. But I was not able to import cv and got an error "No module named cv". Can you help me with this?

    Thanks

    ReplyDelete
    Replies
    1. "import cv" no more supported. Try this instead "import cv2.cv as cv".

      Delete
  8. Hi,

    I am getting the following errors trying to install OpenCV 2.4 on Python 2.7 - please help. Thank you!

    Traceback (most recent call last):
    File "", line 1, in
    import cv
    ImportError: No module named cv
    >>> import cv2

    Traceback (most recent call last):
    File "", line 1, in
    import cv2
    ImportError: DLL load failed: %1 is not a valid Win32 application.
    >>> import cv2.cv as cv

    Traceback (most recent call last):
    File "", line 1, in
    import cv2.cv as cv
    ImportError: DLL load failed: %1 is not a valid Win32 application.
    >>> import cv2.pyd as cv

    Traceback (most recent call last):
    File "", line 1, in
    import cv2.pyd as cv
    ImportError: DLL load failed: %1 is not a valid Win32 application.

    ReplyDelete
    Replies
    1. Are you by chance running 64-bit Python? I'm not 100%, but this may be the problem.

      Delete
    2. I have the same error, perhaps the problem could be Windows64 :/

      Delete
  9. I too am getting the Import Error: DLL load failed: 1% is not a valid Win32 application. I Believe I followed all of your steps correctly though.

    ReplyDelete
  10. Please help! , What can I do?
    Traceback (most recent call last):
    File "", line 1, in
    import cv2
    ImportError: DLL load failed: %1 no es una aplicación Win32 válida.

    ReplyDelete
  11. i did that but when i m
    i write that code
    import cv2.cv as cv
    >>> from opencv.cv import*
    and i got that error

    Traceback (most recent call last):
    File "", line 1, in
    from opencv.cv import*
    ImportError: No module named opencv.cv
    help me out plz

    ReplyDelete
  12. I am working on win7 64bit and do all your steps
    I 'm tring to write this code:
    "import cv2
    import numpy as np
    import sys

    image = cv2.imread('D:\Test\work.jpg')

    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)


    cv2.imshow('color_image',image)
    cv2.imshow('gray_image',gray_image)

    cv2.waitKey(0)
    cv2.destroyAllWindows()

    "

    But i give me that error:
    "Traceback (most recent call last):
    File "D:\Test\Test", line 8, in
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    error: ..\..\..\src\opencv\modules\imgproc\src\color.cpp:3326: error: (-215) scn == 3 || scn == 4"

    And i'm tring alot of images with different extension but it give me the same error so how can i solve it??

    ReplyDelete
    Replies
    1. error says, your input image should have 3 or 4 channels. So try to do all these commands in python terminal one by one, print shape of 'image' and make sure it is loaded and has 3 channels.

      Delete
    2. what do you mean by 3 or 4 channels? I'm getting the same error as Nour khashan and I'm not sure what's going wrong.

      Delete
    3. 3 channels means RGB image. 4 means RGBA image. Just load the image and try to print pixel values, like 'print image'.
      Or print image shape, like 'print image.shape' etc. It should give you some values. Then image is loaded. Otherwise image is not loaded. The problem doesn't seems to be with installation, since 'import cv2' works.

      Delete
    4. I am getting the same error and on doing print image i get "None". and on doing print image.shape i get this.. "AttributeError: 'NoneType' object has no attribute 'shape'".
      Can you please tell me how correct this.

      Delete
    5. It means your image is not loaded correctly. Please give correct path to image.

      Delete
  13. I'm working on win7 64bit and I do all your steps and write that code :
    "
    import cv2
    import numpy as np
    import sys

    image = cv2.imread('D:\Test\work.jpg')

    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    #cv2.imwrite('gray_image.jpg',gray_image)

    cv2.imshow('color_image',image)
    cv2.imshow('gray_image',gray_image)

    cv2.waitKey(0)
    cv2.destroyAllWindows()

    "
    but i give me that error
    "Traceback (most recent call last):
    File "D:\Test\Test", line 8, in
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    error: ..\..\..\src\opencv\modules\imgproc\src\color.cpp:3326: error: (-215) scn == 3 || scn == 4"

    i'm tring alot of image with different extension but it give me the sane error
    so how can i solve it?

    ReplyDelete
  14. Love python more than everything and when it comes to using opncv C++ sucks, I mean with all the efficiency thing.


    Thanks for sharing

    ReplyDelete
  15. Works great !
    Let's start video creation !!

    ReplyDelete
  16. why python 2.7 can't import "cv.pyd" what can i do ? i can only use "cv2.pyd" but all the examples is already working with cv.pyd ===> "import cv" !! ??

    ReplyDelete
    Replies
    1. Earlier OpenCV had only "import cv". Later "cv2" was released which is much better compared to "cv" in all sense. Now, OpenCV has exported "cv" as a submodule inside "cv2". So you can use "import cv2.cv as cv" to access "cv". And in future, that also will be removed (i mean "cv" will be removed) and only "cv2" will be left. So I recommend you to shift to "cv2" as soon as possible. And it is lot better, you will feel it once you use it.

      Delete
  17. cv2.VideoCapture(0) work for camera, but
    cv2.VideoCapture(videofile) return False!!
    any advance???

    ReplyDelete
  18. it is better to add for example "C\OpenCV2.4\build\x86\vc10\bin" to system PATH. -> it is fix reading video file !!!

    ReplyDelete
  19. i getting the error

    Traceback (most recent call last):
    File "C:/Users/........../color.py", line 6, in
    from opencv.highgui import *
    ImportError: No module named opencv.highgui

    please help on this, thank ... =)

    ReplyDelete
    Replies
    1. I think that library is no more supported. If you followed above procedure, try "import cv2". that is the supported one.

      Delete
  20. Man you are great !!!
    Thanks for the help and this great endeavor :)

    ReplyDelete
  21. I am getting this error
    ImportError: numpy.core.multiarray failed to import

    ReplyDelete
    Replies
    1. try "import numpy" and check if any error. If not, then "import cv2".

      Delete
  22. You definitely deserve more than thanks.
    Salute!

    ReplyDelete
  23. I don't have admin rights on machine, but am able to use portable python to program. Can i extract OpenCV in another machine and copy it to the original machine(without admin rights) in location specified you but in portable python install directory?? If not it will be very helpful if you could suggest me any other way out..

    ReplyDelete
    Replies
    1. I am not sure, I didn't try that. If you can't copy cv2.pyd file to specified folder, just copy it somewhere you can(for example,in the folder D:\opencv\), then add following lines at the beginning of your code.

      import sys
      sys.path.append('D:\opencv')

      Delete
    2. please let me know if it works.

      Delete
    3. I am having a similar issue, and this fix did not work for me. I am using portable python, running off a USB.
      Code:

      import sys
      sys.path.append('G:\Python\opencv')
      import cv2
      cv2.NamedWindow("example", 1)

      The error returned is:
      AttributeError: 'module' object has no attribute 'NamedWindow'

      cv2 has been imported has a module clearly, but it doesnt seem to have any attribute!
      Any ideas?

      Delete
    4. It is 'cv2.namedWindow()', not 'N'. It is case-sensitive. I hope it is the problem. If cv2 is imported clearly, then it should work.

      Please let me know if this method works. I need to add this issue to my blog article since it is a common issue.

      Delete
    5. Hi, Thanks for your reply. I could copy the file and import cv2 using IDLE. but for a simple display program as below:

      cv2.imread('image.jpg')
      cv2.namedWindow('Display Window')
      cv2.imshow('Display Window',img)

      I am getting the following error:

      error: C:\slave\WinInstallerMegaPack\src\opencv\modules\core\src\array.cpp:2482: error: (-206) Unrecognized or unsupported array type

      Kindly help.

      Delete
    6. This seems your image is not loaded correctly. give correct path to image, or put your image in the same folder as your script

      Delete
  24. Any point in keeping C:\opencv\ once you've copied the cv2.pyd file?

    ReplyDelete
    Replies
    1. I didn't find any problem after deleting it. So I hope it would be OK.

      Delete
    2. Hi I have downloaded opencv and extracted it in a folder. I am using it with python however i wish to use it with visual studio2010 ultimate edition and eclipse. the opencv site details is bit confusing about using visual studio. Can any body help me how to use opencv in visual studio 2010 ultimate edition.

      Delete
    3. try this : http://stackoverflow.com/questions/7011238/opencv-2-3-c-visual-studio-2010, or you can find more in SOF.

      Delete
  25. When I try to import cv2 as follows
    import cv2
    I get the following error:
    ImportError: DLL load failed: with error code

    ReplyDelete
  26. When I try to import cv2 as follows
    import cv2
    I get the following error:

    ImportError: DLL load failed with error code -1073741795

    Someone help me please!!

    ReplyDelete
  27. Hi Mr.Abid

    Your blog helped me to use Python. Thanks a lot!!!
    I have a doubt regarding the location to store images.
    I have installed Python 2.7 in C: drive. I have installed PIL too.
    My code:

    import cv2
    from PIL import Image
    im = Image.open("Tulips.jpg")
    im.rotate(45).show()

    I have loaded the TULIPS image in C://Python2.7. But I could not get proper output. When using "imread", I am getting error. I have even specified the location of the file. Please help me to locate the error.

    ReplyDelete
    Replies
    1. you should put the image in working directory. If your image is in C:\Python2.7, your script file also should be in same location. Otherwise you have to give the full path of the image, like "C://Python2.7//Tulips.jpg" or whatever is your path.

      Delete
  28. Thanks Abid. This post helped a lot :)

    ReplyDelete
  29. Thanks for the guide, but I am having some problems.

    The original code i have use "import cv" but I followed your install then when i ran it, it says:
    "camera = cv.CreateCameraCapture(0)
    NameError: name 'cv' is not defined"


    So i changed "camera = cv2.CreateCameraCapture(0" and got a new error:
    "camera = cv2.CreateCameraCapture(0)
    AttributeError: 'module' object has no attribute 'CreateCameraCapture'"

    Can someone tell me what am i doing wrong? Thanks!!

    ReplyDelete
    Replies
    1. Actually you are mixing up cv and cv2.

      if you want to use cv2, use:
      "import cv2
      cap = cv2.VideoCapture(0)"

      If you want to use cv, use :
      "import cv2.cv as cv
      cap = cv.CaptureFromCAM(0)"

      check docs, "www.docs.opencv.org" for details about the function.

      Delete