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
    .bootstrap-vcpkg.bat
  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;
	cap.open(0);

	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;
	}

	cap.release();
	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.

Compartilhe este insight:

Elemar Júnior

Sou fundador e CEO da EximiaCo e atuo como tech trusted advisor ajudando diversas empresas a gerar mais resultados através da tecnologia.

Elemar Júnior

Sou fundador e CEO da EximiaCo e atuo como tech trusted advisor ajudando diversas empresas a gerar mais resultados através da tecnologia.

Mais insights para o seu negócio

Veja mais alguns estudos e reflexões que podem gerar alguns insights para o seu negócio:

Um dos benefícios mais bacanas do programa MVP é o direito a participação no MVP Global Summit – evento anual,...
Compete ao arquiteto corporativo a responsabilidade de descrever, analisar e comunicar os diversos aspectos da arquitetura corporativa, bem como registrar...
In this post, I would like to share my current reading list. If you are reading some of these books,...
Reduction operations are those that reduce a collection of values to a single value. In this post, I will share...
Hoje completo 39 anos! Estou em Uberlândia, em um hotel, descansando após um dia de muito trabalho (e um bocado...
Tentando ser “Júnior” Minha carreira como amador remunerado em programação começou em 1992. Eu tinha quase 13 anos e era...

Inscrição realizada com sucesso!

No dia da masterclass você receberá um e-mail com um link para acompanhar a aula ao vivo. Até lá!

A sua subscrição foi enviada com sucesso!

Aguarde, em breve entraremos em contato com você para lhe fornecer mais informações sobre como participar da mentoria.

Crie sua conta

Preencha os dados para iniciar o seu cadastro no plano anual do Clube de Estudos:

Crie sua conta

Preencha os dados para iniciar o seu cadastro no plano mensal do Clube de Estudos:

× Precisa de ajuda?