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