OpenCV released OpenCV-3.4.4 and OpenCV-4.0.0 on 20th November. There have been a lot of bug fixes and other changes in these versions. The release highlights are as follows:
- OpenCV is now C++11 library and requires C++11-compliant compiler. Minimum required CMake version has been raised to 3.5.1.
- A lot of C API from OpenCV 1.x has been removed.
- Persistence (storing and loading structured data to/from XML, YAML or JSON) in the core module has been completely reimplemented in C++ and lost the C API as well.
- New module G-API has been added, it acts as an engine for very efficient graph-based image procesing pipelines.
- dnn module now includes experimental Vulkan backend and supports networks in ONNX format.
- The popular Kinect Fusion algorithm has been implemented and optimized for CPU and GPU (OpenCL)
QR code detector and decoder have been added to the objdetect module. - Very efficient and yet high-quality DIS dense optical flow algorithm has been moved from opencv_contrib to the video module.
In this post, we will provide an installation script to install OpenCV 4.0 (C++ and Python) on Windows.
We will be using command prompt throughout the process.
Before we jump into the technical stuff, let’s make sure we have all the right tools available.
Step 0.1: Install Visual Studio
Download and install Visual Studio 2017 community edition from https://visualstudio.microsoft.com/downloads/. Run the installer and click on Continue.
Once the download is complete, the installer state would look like the following.
Next, we select the packages. We will select Desktop development with C++.
Finally, click on Install while downloading and wait while Visual Studio is installed.
Step 0.2: Install CMake
Please note that the version mentioned in the screenshots might be different from the latest versions available on the website. Please download the latest versions and treat the screenshots as reference.
Download and install CMake v3.12.3 from https://cmake.org/download/.
During installation select “Add CMake to system PATH“
Step 0.3: Install Anaconda (a python distribution)
Download and install Anaconda 64-bit version from https://www.anaconda.com/download/#windows.
While installing Anaconda make sure that you check both options:
- Add Anaconda to my PATH environment variable
- Register Anaconda as my default Python
Step 0.4: Install Git for Windows
Finally, we are going to install Git 64 bit for Windows from https://github.com/git-for-windows/git/releases/download/v2.19.0.windows.1/Git-2.19.0-64-bit.exe. Choose “Use Git from the Windows Command Promt“.
Restart your system at this point before moving on with further steps.
Download all the installation scripts provided in the Downloads section and put them in the same directory.
Step 1: Install OpenCV
Now it’s time to run that command line magic :).
Open Windows PowerShell/ Command Prompt and go to the windows folder.
Type python main.py and press Enter.
The script will create a new batch script: installOpenCV_modified.bat.
First, make sure that runScript.bat file is NOT present in the directory. Run del runScript.bat from command prompt to remove the file if it’s present.
Next, type installOpenCV_modified.bat and press Enter.
After completion you will get something similar to this.
This will create a new batch script runScript.bat which will have all the necessary paths and instructions defined. But, these paths are in Windows format and CMake takes the paths in Unix format.
So, we run, python modifyBatchScript.py to modify the path format.
Finally, type finalScript.bat and press Enter.
This step will take some time to build opencv. After completion you will get something similar to this.
This will install the version of OpenCV you selected initially.
Step 2: Update user environment variable – OPENCV_DIR
Press Start, type Environment variables and click on Edit the system environment variables.
In the popup window, click on Environment Variables.
Under System Variables, select Path and click edit.
Add the complete path to the directory where OpenCV was installed. This can be found at as:
Click New, and give path to OPENCV_PATH\Installation\x64\vc14\bin and click Ok. Depending upon where you have kept opencv folder and what version of Visual Studio you used to compile OpenCV, this path would be different.
In my case full path is:
C:\Users\Administrator\Documents\OpenCV_Installation_4\opencv-master\Installation\x64\vc14\bin
Click OK to save. Do NOT close the Environment Variables window.
In User variables, click on New, under Variable name:, enter OPENCV_DIR and under Variable value:, enter the path to the install directory of OpenCV.
In my case variable value is:
C:\Users\Administrator\Documents\OpenCV_Installation_4\opencv-master\Installation\x64\vc14\staticlib
This directory contains file “OpenCVConfig.cmake”. This is used by CMake to configure OpenCV_LIBS and OpenCV_INCLUDE_DIRS variables to generate project files.
Click on OK, and click on OK again to close Environment Variables window.
If you have an open Command Prompt/Power Shell window before these values were updated, you have to close and open a new Command Prompt/Power Shell window again.
Step 3: Test installation for Python 3
To test your installation, press Start, enter Command Line, and type:
activate OpenCV-master-py3
Once you have activated the Python environment, we start an IPython prompt.
ipython
In IPython prompt, enter:
import cv2
cv2.__version__
Hope you enjoy this script 🙂 In case of any queries, feel free to comment below and we will get back to you as soon as possible.
I am getting an error upon typing import cv2 in the ipython environment. The error is ModuleNotFoundError: No module named ‘cv2’
Make sure you have set OpenCV_DIR variable.
Vishwesh, I have already set the OpenCV_DIR variable in the user environment variables section as
C:Userst68Desktopwindowsopencv-3.4.1opencvbuildinstall
Hi Munesh
Can you please make sure that you didn’t get an error during the process? It looks like something went wrong during the installation.
Vishwesh, I did not get any errors during installation though there were quite a few warnings which I assume is ok. By the way I just want to configure the VS2015 for OpenCV as I want to embed some OpenCV C++ code in CUDA C++ program.
Well this shouldn’t happen. Can you try this again or see if build folder was made or not?
If you try again, please make sure that you take screenshots so that we can follow to make sure nothing went wrong.
Hi Munesh, have you fixed this “ImportError: No module named cv2” issue?
I successfully followed all the step without a problem till this test step.
Confirm I have added the “OpenCV_DIR” and “OpenCV_PATH” as Vishwesh highlighted, and also restarted the computer.
ps: I installed VS2017 and have changed the .bat files accordingly before setup. I think that is not the problem.
any comment is appreciated..
Chui,
Hi chui
Can you try and run this?
conda env list
Let me know what output you are getting.
thank you vishwesh for the quick response. here is the result as you request:
# conda environments:
#
base * C:ProgramDataAnaconda2
OpenCV-3.4.1-py2 C:ProgramDataAnaconda2envsOpenCV-3.4.1-py2
OpenCV-3.4.1-py3 C:ProgramDataAnaconda2envsOpenCV-3.4.1-py3
thx
Can you try activating the environment and see if the following work?
source activate OpenCV-3.4.1-py3
import numpy
import cv2
thank and really appreciate your time. Here is the result as run in Powershell.
PS C:Users> activate OpenCV-3.4.1-py2
PS C:Users> python
Python 2.7.15 |Anaconda, Inc.| (default, May 1 2018, 18:37:09) [MSC v.1500 64 bit (AMD64)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import numpy
>>> import cv2
Traceback (most recent call last):
File “”, line 1, in
ImportError: No module named cv2
:: ——–(also tried “activate OpenCV-3.4.1-py3”, with same result)———-
PS C:Users> source OpenCV-3.4.1-py3
The term ‘source’ is not recognized as the name of a cmdlet, function, script file, or operable program. …
:: ———-Is the following error message relevant? —————
PS C:Users> conda activate OpenCV-3.4.1-py2
CommandNotFoundError: Your shell has not been properly configured to use ‘conda activate’.
If your shell is Bash or a Bourne variant, enable conda for the current user with
$ echo “. C:ProgramDataAnaconda2/etc/profile.d/conda.sh” >> ~/.bashrc
or, for all users, enable conda with
$ sudo ln -s C:ProgramDataAnaconda2/etc/profile.d/conda.sh /etc/profile.d/conda.sh
The options above will permanently enable the ‘conda’ command, but they do NOT
put conda’s base (root) environment on PATH. To do so, run
$ conda activate
in your terminal, or to put the base environment on PATH permanently, run
$ echo “conda activate” >> ~/.bashrc
Previous to conda 4.4, the recommended way to activate conda was to modify PATH in
your ~/.bashrc file. You should manually remove the line that looks like
export PATH=”C:ProgramDataAnaconda2/bin:$PATH”
^^^ The above line should NO LONGER be in your ~/.bashrc file! ^^^ “`
Hi!
Okay so a couple of things:
1. source activate is used with conda environments. That’s why only source won’t work. That’s why the error is irrelevant.
2. Looks like an environment is being created. That means either OpenCV building wasn’t successful or cv2.so file copying part failed.
Can you do the following for me please?
First, go to the folder where you had the scripts. Go to the opencv folder, go to the build directory and see what files are present. A screenshot would help.
thx.
1. it was a typo, I run “source activate OpenCV-3.4.1-py”, which reported the above error message of “‘source’ is not recognized…”
2. I can see a ‘cv2.cpp’ file in ‘…opencvmodulespythonsrc2’ folder, but no ‘cv2.so’. the build folder content is attached. https://uploads.disquscdn.com/images/0377fd2c542d229ecfe6957d1c5737a0d00d55e5e2eb9652822e137bce82a07a.png
Hmmm well the code worked then. Must be some issue with cv2.so copying part. Can you perhaps remove the opencv and opencv-contrib folders and try again? Make sure that you take screenshots of the output of the script. That would help me in finding out what went wrong.
Hi vishwesh, I rerun the scripts and come with the same error as above. Also still no cv2.so in the folder.
I upload all the screenshot to https://drive.google.com/open?id=1dGIuRt5vNVMVrld3hGPmUJaWal_K6Dn5. as I can only upload one photo here.
thx.
Can you try to compile a C++ example? See if that’s working or not.
Sorry for the delay.
1. I tried to run example: “FaceDetectionComparison”, failed. When I follow the README, it failed at the 1st line when I run “cmake .”, with the error msg in the attached screenshot:
“CMake Warning at D:/OpenCV/learnopencv/InstallScripts/Windows/opencv-3.4.1/openc
v/build/install/OpenCVConfig.cmake:140 (message):
Found OpenCV Windows Pack but it has no binaries compatible with your configuration.
You should manually point CMake variable OpenCV_DIR to your build of OpenCV library.
Call Stack (most recent call first):
CMakeLists.txt:5 (find_package)
CMake Error at CMakeLists.txt:5 (find_package):
Found package configuration file:
D:/OpenCV/learnopencv/InstallScripts/Windows/opencv-3.4.1/opencv/build/install/OpenCVConfig.cmake
but it set OpenCV_FOUND to FALSE so package “OpenCV” is considered to be NOT FOUND.
— Configuring incomplete, errors occurred!”
2. is “cv2.so” is only available in Linux environment thus it is correct to not have it under Windows envi? I tried the binary file “cv2.pyd” for python 2.7 (downloaded from: https://opencv.org/opencv-4-0-0-rc.html), it works to import cv library to python. screenshot attached.
I also noticed the downloaded binary from the website is about 70Mb, while the one generated is after compiling is ~5.8Mb.
3. the path to “OpenCV_DIR” and “OpenCV_PATH” should be correct as you listed in the instruction.
screenshot (https://drive.google.com/file/d/1WIwe1mvU8iOhydiPN0eXapU4Yf7fvgon/view?usp=sharing)
Can you try manually setting up OpenCV_DIR in CMakeLists?
Hi I manually set the OPENCV_DIR to “…opencv-3.4.1opencvbuild”, and it compiles file. When I tried to run one of the compiled .py file, then it pops up with the same error, not able to “import cv2”, the 2nd line of each generated .py files. (I cannot upload screenshot evenif I logged in)
I think I will about to give up, and try the binary installation of an older version, or compile from source under Linux.
Thank you for the effort to try to help.
Sorry for the late reply. By the way, I have given up hope of installing OpenCV on Windows. Really wasted days trying installing. I have reverted back to the Ubuntu environment which is more flexible and easier. Thanks Chui and also Vishwesh for the support.
I have the same problem…During the installation I have one different part, I dont have in OPENCV_PATHbuild folder with name “install” so environment variable “Path” was configured to OPENCV_PATHbuildx64vc14bin and OPENCV_DIR was configured to OPENCV_PATHbuild because there were cmake files. Maybe because of this I have such problem…
Hello ,
I would really request for help . I
have ben trying to install Visual Studio for last 2 days but its showing
same error “”SyStem Packages missing” –
I followed every step minutely , you have conveyed here but its still not getting installed
https://uploads.disquscdn.com/images/275754f1e90f4416572e325c2b635842a4d7f2957c5453612a3fec07dd89885c.jpg
You will have to download the packages.
can you kindly tell what packages and where to find them?
In the screenshot you have attached, please select Download packages from the internet. I think that should do.
I have one query here, so far I have been working on Ubuntu and did not face any problems compiling and running OpenCV codes. In windows after installation, how do I configure VS2015 for compiling any OpenCV C++ and python code? Do I have to manually make settings in the Linker properties of the project?
You can use the CMakeLists.txt to use cmake like this:
# Create build directory to store all compiled binaries
mkdir build
cd build
# Configure for compilation
cmake -G “Visual Studio 14 2015 Win64” ..
# Compile the code
cmake –build . –config Release
cd ..
Thanks Vishwesh for your reply. Is it possible to directly configure Visual Studio 2015 and write, edit, build and run opencv c++ code in VS2015 editor. I mean without using Cmake.
Hi,
I have problem in the middle of installation…
In my folder C:Windows there is not main.py…
https://uploads.disquscdn.com/images/f2c53eae5867aeda0e2d9ecbf4ad054d31175457f36ca200cc9ea01d051ab74c.png
I am struggling installing OpenCV c++ for windows. Following your instruction, open cv for python 2 and 3 are installed in the newly created venv by conda, I suppose. However, when it comes to install it for cpp, I get the following error shown in the screenshot.
Do I need to delete the ‘opencv’ and ‘opencv_contrib’ from the ‘Windows’ folder and let it to clone them once again?
To be mentioned that I got this message right at the beginning:
Installing OpenCV-3.4.1
A subdirectory or file opencv-3.4.1 already exists.
https://uploads.disquscdn.com/images/2ee33c4ac33f6717b820b57f19819952a6f16fc0eff5c9531de6d94537526f0c.png
Remove the build directory in opencv and then try again.
Removing the build folder did not work. Since the error was regarding the existence of opencv and opencv_contrib folder here “Windowsopencv-3.4.1”. I deleted them and let them to be downloaded again. Doing so I could reach to the step in which I got finalScript.bat.
But again then I got these errors:
https://uploads.disquscdn.com/images/bfb78c7ec8d4b814e6c802e45040434097971a714de01b8f95f4db4f27bd1faf.png
Looks like either you have not installed CMake or it has not been installed in PATH. Please refer to the instructions for CMake Installation in the post.
First of all, I appreciate your time and effort to reply. Your web site helped me a lot so far and I am thankful.
I finally installed everything and my final outcome were exactly as yours. Now the main issue is I cannot use opencv_contrib modules although they were installed as extra modules. When I want to use ( for example) the “face” module, I get a linking error. It seems after adding the face.hpp, I only have the description in my main.cpp but there is no definition linking to the functions used in the main.
On the other hand, I cannot find any *.lib file related to “contrib modules” in order to link them in visual studio. I tried whatever I could possibly think of, but still not able to solve this problem.
Hi Sam
It’s great to hear that the website has been helpful for you.
Coming to your query. Can you try to run a simple script which uses the module in Python?
Also, my suggestion to you would be to use cmake instead of Visual Studio for the C++ codes. For example, if you notice, in most of our posts we provide CMakeLists.txt files. With cmake, it’s really easy to use them as follows:
# Create build directory to store all compiled binaries
mkdir build
cd build
# Configure for compilation
cmake -G “Visual Studio 14 2015 Win64” ..
# Compile the code
cmake –build . –config Release
cd ..
Hi vishwesh,
Could you please help me to find my mistake in the following CMakelists.txt? I put the .hpp files in a folder called include and .cpp files in a folder called src and I am running this from a RaspberryPi.
Although I’ve checked the existence of face.hpp and face folder including all the header files in my usr/local/include directory, but I get an error saying:
undefined reference to cv::face::FacemarkLBF::params::params()
undefined reference to cv::face::FacemarkLBF::create(cv::face::FacemarkLBF::params const&)
It seems, cmake is not able to find FacemarkLBF from the face folder!
here is the CMakelists.txt:
cmake_minimum_required(VERSION 3.7.2)
project(landmark)
set(CMAKE_CXX_STANDARD 14)
set(OpenCV_LIBS “${/usr/local/lib}”)
find_package(OpenCV REQUIRED)
file( GLOB source_files “src/*.cpp”)
add_executable(landmark ${source_files})
include_directories(
/usr/local/include
include)
link_directories(/usr/local/lib)
target_link_libraries(landmark ${OpenCV_LIBS})
Hi!
Can you follow the following format and see if that works?
CMakeLists.txt file
==================================================
cmake_minimum_required(VERSION 2.8.12)
# Replace the path with the path to the OpenCVConfig.cmake file
# the command should look like this:
# SET(OpenCV_DIR path)
SET(OpenCV_DIR /home/hp/OpenCV_installation/installation/OpenCV-master/share/OpenCV/)
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS})
==================================================
You can make changes in rest of the file as you see fit.
I ended up with something like this but still the same error:
I also tried without commenting out the the following lines, still didn’t work 🙁
cmake_minimum_required(VERSION 2.8.12)
project(landmark)
#set(CMAKE_CXX_STANDARD 14)
#set(OpenCV_LIBS “${/usr/local/lib}”)
file( GLOB source_files “src/*.cpp”)
SET(OpenCV_DIR /usr/local/share/OpenCV)
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} include)
add_executable(landmark ${source_files})
#link_directories(/usr/local/lib)
target_link_libraries(landmark ${OpenCV_LIBS})
Hi Sam
Can you send me the complete code? I will check it out and get back to you asap.
Sorry, what I missed? powershall says: ” Cannot find path ‘C:UsersAZdesktopwindows’ because it does not exist.”
Sorry, what I missed? powershall says: ” Cannot find path ‘C:UsersAZdesktopwindows’ because it does not exist.”
Hi!
I’m getting an error that A setup is missing or damage.
at 1st step installing Visual Studio installation.
Please help me.
Hi!
I’m getting an error that a setup package is missing or damaged.
Please help me. https://uploads.disquscdn.com/images/9cb8cc8ef48cb802b769b52ab512df0442820cd59cc0d456718676b9092f20a4.png
I am trying to install open CV 4 in windows using visual studio and successful till installation of git. but after that the below statement is little confusing me.
Download all the installation scripts provided in the Downloads section and put them in the same directory.
i am confused, from where can i download the scripts , please help me.
Did you ever fine where to obtain these files? I have not found them.
no i am waiting for some one who can answer this.
Hi sir,
at Step 1: Install OpenCV
after extracting the opencv package, i couldn’t find the additional files as
1. Installopencv.bat
2. main.py
3. modifybatchscript.py
where can I find these files ? pls provide the url.
Did you ever find any URL to obtain these files?
This page entitled “Learn OpenCV” might should be better entitled “Learn How to Not Install OpenCV 4 on Windows”
The files are in the github repo. Click on download code for that.
You can also access the files at:
https://github.com/spmallick/learnopencv/tree/master/InstallScripts
If you need to install OpenCV 3/4 on Windows using pre-build libraries with single pip install command you may follow this blog post.
https://gettocode.com/2017/02/07/opencv-installation-on-windows-and-maybe-linux/
There is nothing at the provided link that relates to OpenCV 4.
Just before Step 1 is shown “Download all the installation scripts provided in the Downloads section and put them in the same directory.”
Where would one find the “Downloads section”?
I tried to solve this error in whole day but no solutions are found, do you have any idea?
— Configuring incomplete, errors occurred!
See also “C:/Users/ABC/Desktop/ABC/OpenCV/learnopencv/InstallScripts/Windows-4/opencv-master/opencv/build/CMakeFiles/CMakeOutput.log”.
See also “C:/Users/ABC/Desktop/ABC/OpenCV/learnopencv/InstallScripts/Windows-4/opencv-master/opencv/build/CMakeFiles/CMakeError.log”.
Microsoft (R) Build Engine version 15.9.21+g9802d43bc3 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
MSBUILD : error MSB1009: Project file does not exist.
Switch: INSTALL.vcxproj
The system cannot find the file specified.