OpenCv
(Open-source Computer Vision) is a library that you can use to
modify, manage, up to take a decision from images on computer. It was
designed for computational efficiency and with a strong focus on
real-time applications.
For
short, the following is (we may call it) a “hello world”
application that we use OpenCv’s library to display an image on a
computer screen. Here, I use OpenCv 4.1.1 version with C++11 compiler
running on lubuntu 16.04 operating system.
#include <iostream>
#include <opencv4/opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat im = imread("../fruits.jpg");
namedWindow("Hello World");
imshow("Hello World", im);
cout << "Press 'q' to quit..." << endl;
while(static_cast<char>(waitKey(1)) != 'q');
return 0;
}
For
simplicity, I’ll break the code down into chunks to explain.Mat im = imread("../fruits.jpg");
This
creates a variable im of type Mat (we don’t need to write
cv::Mat here because we have used using directive: namespace
cv; making names from cv directly accessible).
The
argument of the function imread(), ../fruits.jpg
where ‘../’ indicates the location of the file, in this case in
‘build’ folder and fruits.jpg is the file name.
namedWindow("Hello World");
imshow("Hello", im);
![]() |
Figure 1. Result of the code |
The
first line
creates a window called Hello
World (note:
Hello World
is also displayed in the
title bar of the window) and then shows the image stored in im
in the window. And the second
line waits for a key event infinitely; when user press ‘q’ the
program will exit. This function returns an ASCII code of the pressed
key or -1 if no key was pressed before the specified time elapses.
Note that waitKey() works only if an OpenCV GUI window is open and in
focus.
That’s
it!, our “hello world” program for opencv. If there is something
incorrect please comment for me to correct or if there is something
you don’t understand please ask and share :)
0 Comments
Post a Comment