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
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
I will try later
ReplyDeleteworked perfectly. thanks very much!!
ReplyDeleteThank you! The OpenCV page says there should be a binary download on sourceforge, but it only extracts files. This is exactly what I needed
ReplyDeleteThank you, it works fine, but I still cannot open video files(
ReplyDeleteI'm trying to read it frame by frame, but cv2.VideoCapture(file name).grab() always return false.
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 !
ReplyDeletewhy cv? All the programmes uses cv2. So use import cv2.
DeleteYou are awesome! Thanks so much for the update. Can't find this anywhere. Saved me a load of frustration!
ReplyDeleteHey Abid,
ReplyDeleteThanks 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
"import cv" no more supported. Try this instead "import cv2.cv as cv".
DeleteHi,
ReplyDeleteI 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.
Are you by chance running 64-bit Python? I'm not 100%, but this may be the problem.
DeleteI have the same error, perhaps the problem could be Windows64 :/
DeleteI 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.
ReplyDeletePlease help! , What can I do?
ReplyDeleteTraceback (most recent call last):
File "", line 1, in
import cv2
ImportError: DLL load failed: %1 no es una aplicación Win32 válida.
i did that but when i m
ReplyDeletei 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
It works fine
ReplyDeleteThanks :)
I am working on win7 64bit and do all your steps
ReplyDeleteI '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??
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.
Deletewhat 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.
Delete3 channels means RGB image. 4 means RGBA image. Just load the image and try to print pixel values, like 'print image'.
DeleteOr 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.
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'".
DeleteCan you please tell me how correct this.
It means your image is not loaded correctly. Please give correct path to image.
DeleteI'm working on win7 64bit and I do all your steps and write that code :
ReplyDelete"
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?
Love python more than everything and when it comes to using opncv C++ sucks, I mean with all the efficiency thing.
ReplyDeleteThanks for sharing
Works great !
ReplyDeleteLet's start video creation !!
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" !! ??
ReplyDeleteEarlier 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.
Deletecv2.VideoCapture(0) work for camera, but
ReplyDeletecv2.VideoCapture(videofile) return False!!
any advance???
it is better to add for example "C\OpenCV2.4\build\x86\vc10\bin" to system PATH. -> it is fix reading video file !!!
ReplyDeletei getting the error
ReplyDeleteTraceback (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 ... =)
I think that library is no more supported. If you followed above procedure, try "import cv2". that is the supported one.
DeleteMan you are great !!!
ReplyDeleteThanks for the help and this great endeavor :)
I am getting this error
ReplyDeleteImportError: numpy.core.multiarray failed to import
try "import numpy" and check if any error. If not, then "import cv2".
DeleteYou definitely deserve more than thanks.
ReplyDeleteSalute!
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..
ReplyDeleteI 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.
Deleteimport sys
sys.path.append('D:\opencv')
please let me know if it works.
DeleteI am having a similar issue, and this fix did not work for me. I am using portable python, running off a USB.
DeleteCode:
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?
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.
DeletePlease let me know if this method works. I need to add this issue to my blog article since it is a common issue.
Hi, Thanks for your reply. I could copy the file and import cv2 using IDLE. but for a simple display program as below:
Deletecv2.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.
This seems your image is not loaded correctly. give correct path to image, or put your image in the same folder as your script
DeleteGreat! Thanks
ReplyDeleteAny point in keeping C:\opencv\ once you've copied the cv2.pyd file?
ReplyDeleteI didn't find any problem after deleting it. So I hope it would be OK.
DeleteHi 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.
Deletetry this : http://stackoverflow.com/questions/7011238/opencv-2-3-c-visual-studio-2010, or you can find more in SOF.
DeleteWhen I try to import cv2 as follows
ReplyDeleteimport cv2
I get the following error:
ImportError: DLL load failed: with error code
When I try to import cv2 as follows
ReplyDeleteimport cv2
I get the following error:
ImportError: DLL load failed with error code -1073741795
Someone help me please!!
Hi Mr.Abid
ReplyDeleteYour 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.
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.
DeleteThanks Abid. This post helped a lot :)
ReplyDeleteThanks for the guide, but I am having some problems.
ReplyDeleteThe 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!!
Actually you are mixing up cv and cv2.
Deleteif 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.