Install OpenCV for Visual Studio C++ development

 

1. Get software:

2. Install Visual Studio; Unpack OpenCV somethere and add enviromental variable

OPENCV_DIR = C:\opencv-3.4.1-vc14_vc15\opencv\build\x64\vc15

screen_RapidEE_57.png

3. Create Windows Console Application in Visual Studio

screen_devenv_58.png

4. Add OpenCV to a project property

  • Add $(OPENCV_DIR)\..\..\include to C/C++ – General – Additional Include Directories

screen_devenv_59.png

  • Add $(OPENCV_DIR)\lib  to Linker – General – Additional Library Directories

screen_devenv_61.png

  • Add opencv_world341.lib and  opencv_world341d.lib to Linker – Input – Additional Dependencies

screen_devenv_62.png

5. Add code (add path to you image at 13-th line):

#include "stdafx.h"

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    string imageName("Z:/OneDrive/projects/science/code/vision_test1/vision_test1/lune.jpg"); // by default
    if (argc > 1) // run with argument
        imageName = argv[1];
    Mat image = imread(imageName.c_str(), IMREAD_COLOR); // Read the file
    if (image.empty())// Check for invalid input
    {
        cout << "Could not open or find the image" << std::endl;
        return -1;
    }
    namedWindow("Display window", WINDOW_KEEPRATIO); // Create a window for display.
    imshow("Display window", image); // Show our image inside it.
    
    waitKey(0); // Wait for a keystroke in the window
    return 0;
}

6. Build and RUN!

screen_devenv_63.png

Add a Comment

Your email address will not be published. Required fields are marked *