In a previous tutorial we set up OpenCV with Visual Studio.
This time we are going to make our first Hello world OpenCV project.
Let's see that.
First of all
In this tutorial we'll change the color model of our image.
Choose an image with colors because we are going to change them into a grayscale model.
We'll be able to come back to the original image colors.
To achieve this 2 keys will be used:
- c for colors
- g for grayscale
So click either "c" or "g" from your keyboard to change the model color.
You can also click ESC to quit the program.
Note that in this tutorial there is an image to load.
So use the image of your choice of course and put it at the root of your project, that is to say where there is your .CPP file.
Let's code a bit
#include <iostream>
#include <opencv2/core/mat.hpp> // cv::Mat
#include <opencv2/imgcodecs.hpp> // cv::imread
#include <opencv2/highgui.hpp> // cv::imshow
// badprog.com
// ============================================================================
//
// ============================================================================
int main() {
//
std::string image_path = cv::samples::findFile("sky.jpg"); // this file must exist
cv::Mat bpMatrix = cv::imread(image_path, cv::IMREAD_GRAYSCALE); // first init
//
while (1337) {
//
int key = cv::waitKey(0);
//
if ('g' == key) {
//
bpMatrix = cv::imread(image_path, cv::IMREAD_GRAYSCALE);
} else if ('c' == key) {
//
bpMatrix = cv::imread(image_path, cv::IMREAD_COLOR);
} else if (27 == key) {
//
break;
}
//
cv::imshow("Badprog: Hello world :D", bpMatrix);
}
//
std::cout << "Bybye!" << std::endl;
//
return 0;
}
Conclusion
An interesting first example to enter in the OpenCV world.
Good job, you did it.
Add new comment