Skip to content

Instantly share code, notes, and snippets.

@demid5111
Last active April 29, 2024 04:04
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save demid5111/6faa590e4fc5813550dd95cc1c538893 to your computer and use it in GitHub Desktop.
Save demid5111/6faa590e4fc5813550dd95cc1c538893 to your computer and use it in GitHub Desktop.
Instructions for installing opencv on Windows 10

Install OpenCV 4.5 on Windows 10

Pre-configured OpenCV for Python from PyPi

  1. open the terminal (Ctrl+R + cmd)
  2. check Python3 installation: py --version
  3. go to official website to learn details: https://pypi.org/project/opencv-python/
  4. choose the most complete package and run: py -m pip install opencv-contrib-python
  5. check installation by entering the Python REPL: py
  6. import tha library: import cv2

Pre-configured OpenCV for Python and C++ from GitHub releases

  1. Download opencv-4.5.1-vc14_vc15.exe from latest release: https://github.com/opencv/opencv/releases/tag/4.5.1
  2. unpack to C:\opencv
  3. edit environment variables. Win search "Environment variables"
  4. append to Path the path to opencv: C:\opencv\bin
  5. restart computer
  6. open cmd
  7. echo %Path% output should contain new path

Build OpenCV for Python and C++ from sources

  1. Install cmake. To check: cmake --version
  2. Download Source code (zip) from latest release: https://github.com/opencv/opencv/releases/tag/4.5.1
  3. Download Source code (zip) from latest release of opencv-contrib sources: https://github.com/opencv/opencv_contrib/releases/tag/4.5.1
  4. Unzip both archives Run cmake-gui
  5. choose the directory with opencv sources
  6. specify the directory for storing building binaries and Visual Studio project. it would better if you create a new one and specify it here
  7. press 'Configure'
  8. use standard settings and proceed with Finish button
  9. once project is prepared, review all the flags suggested
  10. check BUILD_WITH_DEBUG_INFO
  11. important to specify: OPENCV_EXTRA_MODULES_PATH. We need to specify path to contrib folder we extracted earlier
  12. change CMAKE_INSTALL_PREFIX to a directory you want opencv to install to. for example C:/opencv-custom
  13. when you are ready, press Generate.
  14. go to build directory
  15. double click on OpenCV.sln
  16. build BUILD_ALL by right click on the corresponding project in Release configuration
  17. Then build INSTALL by right click on the corresponding project to finally install in Release configuration
  18. edit environment variables. Win search "Environment variables"
  19. append to Path the path to opencv: C:\opencv-custom\bin
  20. restart computer
  21. open cmd
  22. echo %Path% output should contain new path

Configure Visual Studio project to work with opencv

Go to the Visual Studio and create the new empty C++ project.

  1. We will call it "HelloOpenCV".
  2. Set the target to Debug and x64. This is very important to configure it for x64.
  3. Paste the main.cpp code to the main.cpp

As we can see there is unknown include statement of the opencv package. We need to fix that.

  1. For that we need to configure a project so that compiler could know where to take headers and library from. Open the Project->HelloOpenCV Properties and go to VC++ Directories.

  2. Change the Include Directories to contain corresponding include folder in the opencv installation. Insert it to the beginning of the value: C:\opencv\opencv\build\include;

  3. Change the Library directories to include C:\opencv\opencv\build\x64\vc15\lib

  4. Change the Linker->Input->Additional dependencies to

    1. include opencv_world451d.lib if we are talking about pre-configured package installation
    2. include opencv_core451d.lib;opencv_highgui451d.lib;opencv_videoio451d.lib;opencv_imgcodecs451d.lib; if we are talking about building from sources. In future, this list might be bigger containing all the libraries and extensions that you use from opencv and opencv-contrib.
  5. Apply and close the window

    To name the few more available libraries that can be included with the following linker flags (for Windows remove -l and add 451d.lib to the end if the version of opencv is 4.5.1:

    -lopencv_gapi -lopencv_stitching -lopencv_alphamat \
    -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired \
    -lopencv_ccalib -lopencv_dnn_objdetect -lopencv_dnn_superres \
    -lopencv_dpm -lopencv_face -lopencv_freetype \
    -lopencv_fuzzy -lopencv_hfs -lopencv_img_hash \
    -lopencv_intensity_transform -lopencv_line_descriptor -lopencv_mcc \
    -lopencv_quality -lopencv_rapid -lopencv_reg \
    -lopencv_rgbd -lopencv_saliency -lopencv_sfm \
    -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping \
    -lopencv_superres -lopencv_optflow -lopencv_surface_matching \
    -lopencv_tracking -lopencv_highgui -lopencv_datasets \
    -lopencv_text -lopencv_plot -lopencv_videostab \
    -lopencv_videoio -lopencv_viz -lopencv_xfeatures2d \
    -lopencv_shape -lopencv_ml -lopencv_ximgproc \
    -lopencv_video -lopencv_dnn -lopencv_xobjdetect \
    -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs \
    -lopencv_features2d -lopencv_flann -lopencv_xphoto \
    -lopencv_photo -lopencv_imgproc -lopencv_core
    
#include "opencv2/opencv.hpp"
#include <opencv2/highgui/highgui.hpp>
#include "iostream"
int showImage() {
std::string path = "img/test.jpeg";
cv::Mat img = cv::imread(path);
imshow("Portrait", img);
cv::waitKey(0);
return 0;
}
int showWebCameraContent() {
// open the first webcam plugged in the computer
cv::VideoCapture camera(0);
if (!camera.isOpened()) {
std::cerr << "ERROR: Could not open camera" << std::endl;
return 1;
}
// create a window to display the images from the webcam
cv::namedWindow("Webcam");
// this will contain the image from the webcam
cv::Mat frame;
// display the frame until you press a key
while (1) {
// capture the next frame from the webcam
camera >> frame;
// show the image on the window
cv::imshow("Webcam", frame);
// wait (10ms) for a key to be pressed
if (cv::waitKey(10) >= 0) {
break;
}
}
return 0;
}
int main(int, char**) {
showImage();
// showWebCameraContent();
}
@Digital2Slave
Copy link

Thanks for sharing 🤝

@cyberwolf93
Copy link

Thanks bro helped me a lot

@yangwang201911
Copy link

Thanks for your sharing. May I ask you a little question that if there is a way to build&install OpenCV without GUI?

@Silverlan
Copy link

Small correction for step 11:
OPENCV_EXTRA_MODULES_PATH has to point to the "modules" sub-directory of the extracted files, otherwise you get the error "No modules has been found:" when configuring OpenCV.

@bonbonsiregar
Copy link

I don't find bin folder in C:\opencv\bin but in C:\opencv\builds\bin and that folder only contain one file named opencv_videoio_ffmpeg481_64.dll. Is this the only file?

@rahul-lard
Copy link

Thanks bro

@tilahunbekele
Copy link

how to use opencv for php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment