Outline
Source code: https://github.com/tuttelikz/notes/tree/main/opencv-vs/OpenCVApp
Download 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"
Extract downloaded archive
Specify a folder to extract (We will further call it "source-dir"
) > Extract
Confirm the extraction is completed to "source-dir"
Configure CPP project
Create a new C++ Project
Open Visual Studio > Create a new project > Set "C++", "Windows" and "Console" filters to find the template > click Next
Give a project name
Set any name to the project > click Create
Set configuration
Solution configuration should be set same as follows: Configuration > "Release", Platform > "x64"
Specify Include directories
Navigate to Project > Properties
Then C/C++ > Additional Include Directories > Edit
Specify the build directory including the parent "source-dir", where OpenCV was extracted, eg. [source-dir]\opencv\build\include
> OK
Modify Linkers
In the same properties menu choose Linker > Input
Then C/C++ > Additional Dependencies > Edit
Specify all ".lib" dependencies including the parent "source-dir", where OpenCV was extracted, eg. [source-dir]\opencv\build\x64\vc16\lib*.lib
> OK
Configure Build events
In properties menu, go to Build Events > Post-Build
Then, Command Line > Edit
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
After setting these parameters, make sure to click Apply > OK
Run code
Add code to OpenCVApp.cpp
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;
}
Press Start debugging to see the result
Thanks for reading, hope this post was useful. Stay tuned, more to come 🙂
Alternate URL: https://github.com/tuttelikz/notes/tree/main/opencv-vs
Top comments (0)