Logo

dev-resources.site

for different kinds of informations.

Integrating OpenCV with Visual Studio C++

Published at
11/23/2024
Categories
opencv
cpp
visualstudio
tutorial
Author
tuttelikz
Categories
4 categories in total
opencv
open
cpp
open
visualstudio
open
tutorial
open
Author
9 person written this
tuttelikz
open
Integrating OpenCV with Visual Studio C++

Integrating OpenCV with Visual Studio C++
To build an application with OpenCV we need to do two things, as stated in official docs:

  • Tell to the compiler how the OpenCV library looks;

We can do this by setting the path where the library files are and specify in which one of them to look.

  • Tell to the linker from where to get the functions or data structures of OpenCV;

During the build the linker will look into these libraries and add the definitions and implementation of all used functions and data structures to the executable file.

Outline

  1. Get OpenCV
  2. Configure C++ project
  3. Run code

Source code: https://github.com/tuttelikz/notes/tree/main/opencv-vs/OpenCVApp


Get OpenCV

Download OpenCV

Download latest OpenCV from the official Github repository: https://github.com/opencv/opencv/releases/tag/4.10.0, choose the one which ends with "windows.exe"

Download OpenCV

Extract downloaded archive

Specify a folder to extract (We will further call it "source-dir") > Extract

Extract downloaded archive

Confirm the extraction is completed to "source-dir"

Confirm the extraction


Configure CPP project

Create a new cpp project

Open Visual Studio > Create a new project > Set "C++", "Windows" and "Console" filters to find the template > click Next

Create a new C++ Project

Give a project name

Set any name to the project > click Create

Give a project name

Set configuration

Solution configuration should be set same as follows: Configuration > "Release", Platform > "x64"

Set configuration

Specify Include directories

Navigate to Project > Properties

Then C/C++ > Additional Include Directories > Edit

Specify Include directories

Specify the build directory including the parent "source-dir", where OpenCV was extracted, eg. [source-dir]\opencv\build\include > OK

Specify the build directory

Modify Linkers

In the same properties menu choose Linker > Input

Then C/C++ > Additional Dependencies > Edit

Modify Linkers

Specify all ".lib" dependencies including the parent "source-dir", where OpenCV was extracted, eg. [source-dir]\opencv\build\x64\vc16\lib*.lib > OK

Specify all lib dependencies

Define Post build events

If Windows path variable related to OpenCV is not set, the code execution cannot proceed.
Path not set
Therefore, we must make sure that the required DLL files from OpenCV are copied to the project’s build directory. Instead of manually copying and pasting files after every build, this process can be automated using a post-build event. To do that, in properties menu, go to Build Events > Post-Build

Then, Command Line > Edit

Post-build events

Insert the following command, which tells Visual Studio to copy the OpenCV DLL into the appropriate build directory of your project:

xcopy [source-dir]\opencv\build\x64\vc16\bin\opencv_world4100.dll $(SolutionDir)$(Platform)\$(Configuration)\ /c /y > OK

Post-build command

After setting these parameters, make sure to click Apply > OK

Apply changes


Run code

Add code to OpenCVApp.cpp and Run

To test the imported library in C++ project, we defined a few functions from OpenCV to draw basic shapes. Add the following code to OpenCVApp.cpp

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

int main() {
    // Create a blank image (black image) of size 400x400 with 3 color channels (RGB)
    cv::Mat image = cv::Mat::zeros(400, 400, CV_8UC3);

    // Draw a red circle at the center of the image
    cv::Point center(200, 200);  // Coordinates of the center
    int radius = 50;             // Radius of the circle
    cv::Scalar color(0, 0, 255); // Red color in BGR format (Blue, Green, Red)
    int thickness = -1;          // Thickness = -1 means filled circle
    cv::circle(image, center, radius, color, thickness);

    // Draw a green rectangle on the image
    cv::Rect rect(50, 50, 300, 100); // Rect(x, y, width, height)
    cv::Scalar rectColor(0, 255, 0); // Green color in BGR
    cv::rectangle(image, rect, rectColor, 2);

    // Display the image
    cv::imshow("Generated Image", image);

    // Wait for a key press
    cv::waitKey(0);

    // Clean up and close windows   
    cv::destroyAllWindows();

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Press Start debugging to see the result

Start debugging


Thanks for reading! Stay tuned for more content, and feel free to share your thoughts and feedback! Your reactions help me improve and create even more useful posts πŸ™‚

Alternate URL: https://github.com/tuttelikz/notes/tree/main/opencv-vs

visualstudio Article's
30 articles in total
Favicon
Can Skater's Secure Depot of Private Keys be used as a tool designed for securely storing and managing private keys?
Favicon
Code Protection
Favicon
How to use OpenMP to parallelize C++ using Visual studio
Favicon
Getting started with .Net Aspire
Favicon
Integrating OpenCV with Visual Studio C++
Favicon
Version 9 of .Net is here
Favicon
EasyTdd 0.5.0: Streamlining Mocking with Incremental FluentMock
Favicon
From Legacy to .NET 8: Migrating with NDepend
Favicon
LocalDb Dev on Windows11 ARM
Favicon
How to create custom snippets in Visual Studio 2022
Favicon
Builder: Your Buddy in Test-Driven Development (TDD)
Favicon
AutoEntityGenerator – my first visual studio extension.
Favicon
C# | Visual Studio Extensions for C# Developers
Favicon
SonarQube | Working with SonarLint and SonarQube in Visual Studio
Favicon
Migrating a project from Visual studio to Rider
Favicon
How to embed your git bash into Visual Studio
Favicon
.NET Monthly Roundup - May 2024 - .NET at Build, .NET Aspire GA, and more!
Favicon
Quick Tipp: Dev Tunnels with Visual Studio
Favicon
EasyTdd 0.4.0 Release: Introducing the Incremental Builder
Favicon
Troubleshooting β€œThe File Does Not Have an App Associated with It” Error in Visual Studio
Favicon
Help Needed: "Object reference not set to an instance of an object" Error in Visual Studio
Favicon
Debugging with Precision: Conditional Breakpoints
Favicon
Simple Way To CreateYour NuGet Package: Step-by-Step Guide
Favicon
Easy Self-Hosted Git Installation on Ubuntu Server
Favicon
Effortless Remote Debugging with Dev Tunnel in Visual Studio 2022
Favicon
How to Use GitHub Copilot in Visual Studio 2024
Favicon
How to Enable Windows 11 Developer Mode?
Favicon
Visual Studio Live Share: Complete Guide 2024
Favicon
Gitlens Tutorial - Visual Studio Extension 2024
Favicon
Top 8 Best Visual Studio Extensions in 2024

Featured ones: