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:

Comentários

Participe deixando seu comentário sobre este artigo a seguir:

Subscribe
Notify of
guest
2 Comentários
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Andre
Andre
4 anos atrás

Hello,
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.

Gokhan
Gokhan
3 anos atrás

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?

AUTOR

Elemar Júnior
Fundador e CEO da EximiaCo atua como tech trusted advisor ajudando empresas e profissionais a gerar mais resultados através da tecnologia.

NOVOS HORIZONTES PARA O SEU NEGÓCIO

Nosso time está preparado para superar junto com você grandes desafios tecnológicos.

Entre em contato e vamos juntos utilizar a tecnologia do jeito certo para gerar mais resultados.

Insights EximiaCo

Confira os conteúdos de negócios e tecnologia desenvolvidos pelos nossos consultores:

Arquivo

Pós-pandemia, trabalho remoto e a retenção dos profissionais de TI

CTO Consulting e Especialista em Execução em TI
EximiaCo 2024 - Todos os direitos reservados
2
0
Queremos saber a sua opinião, deixe seu comentáriox
()
x

Muito obrigado!

Deu tudo certo com seu envio!
Logo entraremos em contato

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

Para se candidatar nesta turma aberta, preencha o formulário a seguir:

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

Para se candidatar nesta turma aberta, preencha o formulário a seguir:

Condição especial de pré-venda: R$ 14.000,00 - contratando a mentoria até até 31/01/2023 e R$ 15.000,00 - contratando a mentoria a partir de 01/02/2023, em até 12x com taxas.

Tenho interesse nessa capacitação

Para solicitar mais informações sobre essa capacitação para a sua empresa, preencha o formulário a seguir:

Tenho interesse em conversar

Se você está querendo gerar resultados através da tecnologia, preencha este formulário que um de nossos consultores entrará em contato com você:

O seu insight foi excluído com sucesso!

O seu insight foi excluído e não está mais disponível.

O seu insight foi salvo com sucesso!

Ele está na fila de espera, aguardando ser revisado para ter sua publicação programada.

Tenho interesse em conversar

Se você está querendo gerar resultados através da tecnologia, preencha este formulário que um de nossos consultores entrará em contato com você:

Tenho interesse nessa solução

Se você está procurando este tipo de solução para o seu negócio, preencha este formulário que um de nossos consultores entrará em contato com você:

Tenho interesse neste serviço

Se você está procurando este tipo de solução para o seu negócio, preencha este formulário que um de nossos consultores entrará em contato com você:

× Precisa de ajuda?