Hello OpenCV with C++, using Visual Studio 2017 and VcPkg

In this post, I will show how to do your first steps with OpenCV quickly using Visual Studio 2017 and VcPkg.

What is OpenCV?

OpenCV (Open Source Computer Vision) is an open-source and cross-platform library mainly aimed at real-time computer vision. Originally, it was developed by Intel. It is free for use under the BSD license.

What is VcPkg?

Acquiring native libraries on Windows is a critical part of the application development process, and it used to be a nightmare. VcPkg is a VC++ Packaging Tool that helps to get C and C++ libraries on Windows.

Getting started with VcPkg

If you want to use VcPkg (I strongly recommend it if you are planning to develop software for Windows using C++), you will need:

  1. Clone VcPkg GitHub repository;
  2. Run the VcPkg bootstrapping process
  3. Make all installed packages available to all VS projects.
    .vcpkg integrate install

Done! Now you can get OpenCV in a very easy way.

.vcpkg install opencv

Hello, OpenCV

We are ready to create our application using OpenCV. There is no need to any configuration – we just need #include it.

#include <opencv2/opencv.hpp>
#include <iostream>

int main()
	cv::namedWindow("raw", cv::WINDOW_AUTOSIZE);
	cv::namedWindow("gray", cv::WINDOW_AUTOSIZE);
	cv::namedWindow("canny", cv::WINDOW_AUTOSIZE);

	cv::VideoCapture cap;

	if (!cap.isOpened())
		std::cerr << "Couldn't open capture." << std::endl;
		return -1;
	cv::UMat bgr_frame, gray, canny;

	for (;;) 
		cap >> bgr_frame;
		if (bgr_frame.empty()) break;

		cv::imshow("raw", bgr_frame);

		cv::cvtColor(bgr_frame, gray, cv::COLOR_BGR2GRAY);
		cv::imshow("gray", gray);

		cv::Canny(gray, canny, 10, 100, 3, true);
		cv::imshow("canny", canny);

		char c = cv::waitKey(10);
		if (c == 27) break;

	return 0;

This code starts the camera, capturing images, applying filters on it and displaying the results in three different windows.

That’s all folks.

Elemar Júnior

Microsoft Regional Director and Microsoft MVP. I have been working for more than two decades developing world-class business software. I had the privilege to help to change the way Brazil sells, designs and produces furniture. Today, my technical interests are scalable architectures, database engines, and integration tools. Also, I am crazy about exponential organizations and business strategy.

You might also like

Loading posts...
  1. Andre

    Thanks for this! After years of (on and off) fussing with trying to compile OpenCV for C++ this is the ONLY time I have it actually running. Over the years I resorted to using Opencv with python because it was the only way I could get up and running with it. Finally I can code in C++ again for my computer vision projects.

    there is only one thing. Your sample code ultimately works for me, but the console gives this error

    load opencv_videoio_gstreamer411.dll => FAILED

    I don’t understand why the exe example even works. I look forward to your reply if you have any ideas on how to fix the error or why its even working with the error.

  2. Gokhan

    Hello Elemar,

    Thank you for your clean and simple explanation. How can we build OpenCV with CUDA and IPP support ? It there an easy way to install it with .vcpkg?

Leave a Reply

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