Basics of OpenCV API
Basics of OpenCV API
Header files
These are some of important OpenCV header files for C++ interface. As a beginner, you will need few of these
header files for your application. In my following lessons, I will include only necessary header files to my
example programs. If you are not sure what to include, include them all. No any penalty incurred for including
all this header file except for a fact that the length of your source code will increased by few lines
than necessary.
o #include "opencv2/core/core.hpp"
o #include "opencv2/flann/miniflann.hpp"
o #include "opencv2/imgproc/imgproc.hpp"
o #include "opencv2/photo/photo.hpp"
o #include "opencv2/video/video.hpp"
o #include "opencv2/features2d/features2d.hpp"
o #include "opencv2/objdetect/objdetect.hpp"
o #include "opencv2/calib3d/calib3d.hpp"
o #include "opencv2/ml/ml.hpp"
o #include "opencv2/highgui/highgui.hpp"
o #include "opencv2/contrib/contrib.hpp"
o #include "opencv2/core/core_c.h"
o #include "opencv2/highgui/highgui_c.h"
o #include "opencv2/imgproc/imgproc_c.h"
Namespace
All OpenCV classes and functions are in cv namespace. So, you have to do one of following
Add the 'using namespace cv' line just after including your header files (I have used this method in all
my sample programs)
e.g.
#include "opencv2/core/core.hpp"
using namespace cv;
int main()
{
Mat frame = cvQueryFrame( capture );
imshow( "Video", frame );
}
append the cv:: specifier at the beginning of every OpenCV classes, functions and data structures in
your source code
e.g.
#include "opencv2/core/core.hpp"
int main()
{
cv::Mat frame = cvQueryFrame( capture );
cv::imshow( "Video", frame );
}
Data Types for Arrays
Data type of an array defines the number of bits allocated for each element of array (pixels in an image) and
how the value is represented using those bits. Any array elements should have one of following data types.
We can define all of above data types for multi channel arrays (supports up to 512 channels). Here I am going to
show you how to define CV_8U data type for multi channel arrays.
CV_8UC1 (single channel array with 8 bit unsigned integers)
CV_8UC2 (2 channel array with 8 bit unsigned integers)
CV_8UC3 (3 channel array with 8 bit unsigned integers)
CV_8UC4 (4 channel array with 8 bit unsigned integers)
CV_8UC(n) (n channel array with 8 bit unsigned integers (n can be from 1 to 512) )
e.g. 1 : Here I have illustrated a 3 channel array with 8 bit unsigned integers. As the datatype is 8 bit unsigned
integers, each element should have a value from 0 to 255. Because this is a 3 channel array, array consists of
tuples with 3 elements. The first tuple is {54, 0, 34}, second tuple is {58, 78, 185} and so on.
3 Channel Arrays
e.g. 2 : Here I have illustrated a 2 channel array with 8 bit signed integers. As the datatype is 8 bit signed
integers, each element should have a value from -128 to 127. Because this is a 2 channel array, array consists of
tuples with 2 elements. The first tuple is {-85, -127}, second tuple is {25, 23} and so on.
2 Channel Array
Example Usage :
o Mat img1(3, 5, CV_32F ); //3 x 5 single-channel array with 32 bit floating point numbers
o Mat img2(23, 53, CV_64FC(5) ); //23 x 53 5-channel array with 64 bit floating point numbers
o Mat img3(Size(100, 200), CV_16UC2 ); //100 x 200 2-channel array with 16 bit unsigned
integers
Remember :
Some OpenCV functions can handle only a subset of above data types. So, be careful, when using OpenCV
functions.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Before you run this program, put any image file (MyPic.JPG) into the folder where your c++ file is.Otherwise
you have to change the first argument of imread() function and give the absolute path to your image file.
You can download this OpenCV visual c++ project from here. (The downloaded file is a compressed .rar folder.
So, you have to extract it using Winrar or other suitable software)
Explanation
#include "opencv2/highgui/highgui.hpp"
imread(), namedWindow(), imshow() and waitKey() functions are declared in the above header file.
So you must include it.
We are using Mat data structure in the above program. It is declared in "opencv2/core/core.hpp" header
file. Then why don't we include this? It's because "opencv2/highgui/highgui.hpp" header file include that
header file inside it. So, we don't need to include it again in our program.
'#include <iostream>' and 'using namespace std' are added because we are using 'cout' to display
some strings in the console. This is basic C++ and you should be familiar with this.
imread() is a function declared in "opencv2/highgui/highgui.hpp" header file. It loads an image from a file and
stores it in Mat data structure.
You can combine these above parameters to get desired image output.
e.g -
CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR - image-depth=unchanged, no. of
channels=unchanged
CV_LOAD_IMAGE_COLOR | CV_LOAD_IMAGE_ANYDEPTH - image-depth=unchanged, no. of
channels=3
If you are not sure what to do, use CV_LOAD_IMAGE_COLOR as the 2nd parameter of imread()
function.
To understand image-depth and concept of channels, you should be familiar with theory of image
processing. So, let's discuss little bit of theory of image processing.
Any digital image consists of pixels. Any pixel should have some value. The minimum value for a pixel
is 0 and it represents black.When the value of the pixel is increased, the intensity of that pixel is also
increased. In a computer memory, ). In decimal, it is 255. fixed number of bits are allocated for every
pixel. Say the number of allocated bits per pixel is 8. Then the maximum number that a pixel can have
is 255 (11111111 in binary)
Now what is image-depth? The image-depth means the number of bits allocated for each pixel. If it is
8, each pixel can have a value between 0 and 255. If it is 4, each pixel can have a value between 0 to
15 (1111 in binary).
Here is a simple model of a image with image-depth of 8 bits. Each small box represents a pixel. So,
each box may contain a value between 0 to 255.
Following image is a simple model of a color image. Color image should consist of at least 3 planes;
Red, Green and Blue. Any color can be created using a particular combination of these 3 colors. Any
pixel is a combination of three 3 values. (255, 0, 0) represent pure red. (0, 255, 0) represent pure green.
(255, 0, 255) represents pure violate. In the same way, you can create many color. Image-depth is 24
because each pixel is represented with 8 x 3 bits (8 bits from each channel).
In the above model, top left pixel is (23, 231, 46). It will be shown as a greenish color because the green
value(231) of that pixel is larger than the red(23) and blue(46) value.
if (img.empty())
If imread() function fails to load the image, 'img' will not be loaded any data. Therefore 'img.empty()'
should return true. It's a good practice to check whether the image is loaded successfully and if not exit
the program. Otherwise your program will crash when executing imshow()function.
bool Mat::empty()
This function returns true, if Mat::data==NULL or Mat::total() == 0
//system("pause");
If you are using Visual Studio, it's better to uncomment this line because it will pause the program until
user press any key. If we don't uncomment it, the program will exit immediately so that user will not see
the error message.
void namedWindow(const string& winname, int flags = WINDOW_AUTOSIZE);
This function creates a window.
Parameters -
o winname - Title of the window. That name will display in the title bar of the newly
created window
o flags - determine the size of the window. There are two options
WINDOW_AUTOSIZE - User cannot resize the image. Image will
be displayed in its original size
CV_WINDOW_NORMAL - Image will resized if you resize the the window
Parameters -
o winname - Title of the window. This name is used to identify the window created by
namedWindow() function.
o mat - hold the image data
Summary
When running this program, the image of 'MyPic.JPG' is loaded into the variable, 'img' of type Mat. Then a
window named 'MyWindow' is opened. After that 'img' is loaded to that window. The window with the image
will be displayed until any key is pressed.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Before you run this program, put any image file (MyPic.JPG) into the folder where your c++ file is.Otherwise
you have to change the first argument of imread() function and give the absolute path to your image file.
You can download this OpenCV visual c++ project from here. (The downloaded file is a compressed .rar folder.
So, you have to extract it using Winrar or other suitable software)
Parameters :
o rows - Number of rows in the 2D array ( height of the image in pixels)
o cols - Number of columns in the 2D array ( width of the image in pixels)
o type - specify the bit depth, data type and number of channels of the image. I
gave CV_8UC3 and it specify 8 bit unsigned integers with 3 channels. Here are some of possible inputs for this
parameter
CV_8UC1 - 8 bit unsigned integers with single channel
CV_8UC3 - 8 bit unsigned integers with 3 channels
CV_64FC1 - 64 bit floating point value with 1 channels
If you want more details about this, please refer to Data Types for Arrays in the Basics of OpenCV API
o s - Initialize each array element with the value given by s. In the above application, I
gave Scalar(0,0,100). So, it initialize my first channel (Blue plane) with 0, 2nd channel (Green plane) with 0 and
3rd channel (Red Plane) with 100. So, my final image is red. You can try different combinations of these three
and see the output image.
Summary
In this program, I created a 3 channel image with 500 height and 1000 width. 8 bit unsigned integer is allocated
for each pixel in each channel. (8x3 = 24 bits per each pixel) And each pixel is assigned with (0,0,100) scalar
value. That means 1st channel is all zero, 2nd channel is also all zero and the 3rd channel is all 100. Therefore we
can see a red image as the output of the program.