Appendix Ii. Image Processing Using Opencv Library
Appendix Ii. Image Processing Using Opencv Library
Appendix Ii. Image Processing Using Opencv Library
1. Introduction
A framework based on the OpenCV library, called OpenCVApplication is available (you can
download it from the personal web pages of the teaching staff). The framework is
personalized for several versions of the Visual Studio (C++) development environments and
several editions of the OpenCV library. The application includes some basic examples for
opening and processing images and video streams. It can be used independently (without
installing the OpenCV kit).
In the following, some basic data structures and image processing functions of the library are
presented, as described in the 2.4.x API documentation (2.x API is essentially a C++ API).
In the OpenCV API, all the classes and functions are placed into the cv namespace. To access
them from your code, use the cv:: specifier or using namespace cv; directive:
...
cv::Mat src_gray, dst;
cv::equalizeHist(src_gray, dst);
...
or
using namespace cv;
...
Mat src_gray, dst;
equalizeHist(src_gray, dst);
...
Some of the current or future OpenCV external names may conflict with STL or other
libraries. In this case, use explicit namespace specifiers to resolve the name conflicts:
Point_ is a template class that specifies a 2D point by coordinates x and y. You can perform
most of the unary and binary operations between Point type operators:
114
Appendix II. Image processing using OpenCV library
pt1 *= a;
double value = norm(pt); // L2 norm
pt1 == pt2;
pt1 != pt2;
You can use specific types for the coordinates and there is a cast operator to convert point
coordinates to the specified type. The following type aliases are defined:
Point3_ is a template class that specifies a 3D point by coordinates x, y and z. It supports all
the vector arithmetic and comparison operations (same as for the Point_ class). You can use
specific types for the coordinates and there is a cast operator to convert point coordinates to
the specified type. The following type aliases are defined:
Size_ is a template class that specifies the size of an image or rectangle. The class includes
two members called width and height. The same arithmetic and comparison operations as
for Point_ class are available.
Rect_ is the template class for 2D rectangles, described by the following parameters:
Coordinates of the top-left corner: Rect_::x and Rect_::y
Rectangle width (Rect_::width) and height (Rect_::height).
115
Appendix II. Image processing using OpenCV library
The Vec class is commonly used to describe pixel types of multi-channel arrays. For example
to describe a RGB 24 image pixel the following type can be used:
typedef Vec<uchar, 3> Vec3b;
v1 = v2 + v3
v1 = v2 - v3
v1 = v2 * scale
v1 = scale * v2
v1 = -v2
v1 += v2 and other augmenting operations
v1 == v2, v1 != v2
norm(v1) (Euclidean norm)
Examples:
Mat m1,m2;
// create a 100x100 3 channel byte matrix
m1.create(100,100,CV_8UC(3));
// create a 5x5 complex matrix filled with (1-2j)
Mat m2(5,5,CV_32FC2,Scalar(1,-2));
116
Appendix II. Image processing using OpenCV library
Accessing the elements of a matrix can be done in several ways. Supposing that src is a gray-
scale image with 480 rows and 640 columns (initialized by opening an image from the disk),
the examples below are presenting how a simple processing like the image negative can be
implemented:
or
or
117
Appendix II. Image processing using OpenCV library
Element-wise minimum and maximum: min(A, B), min(A, alpha), max(A, B),
max(A, alpha)
In order to get a region of interest (ROI) from a matrix defined by a Rect Structure use the
following statements:
Mat image;
Rect ROI_rect;
Mat roi=image(ROI_rect);
To open an image file stored on the disk the imread function can be used. It can
handle/decode the most common image formats (bmp, jpg, gif, png etc.):
The input parameters are the filename and an optional flags parameter specifying the color
type of a loaded image:
118
Appendix II. Image processing using OpenCV library
The output is a Mat object containing the image for successful completion of the operation.
Otherwise the function returns an empty matrix ( Mat::data==NULL ).
In order to control the size and position of the display window the following functions can be
used:
void namedWindow(const string& winname, int flags=WINDOW_AUTOSIZE )
void moveWindow(const string& winname, int x, int y)
To save the image on to the disk the imwrite function can be used. The image format is
chosen based on the filename extension.
The following example illustrates the above mentioned image handling functions by opening
a color image, converting it to grayscale and saving it to the disk and displaying the source
image and the destination/result image in a separate windows:
void testImageOpenAndSave()
{
Mat src, dst;
src = imread("Images/Lena_24bits.bmp", CV_LOAD_IMAGE_COLOR); //Read the image
//Display window
const char* WIN_SRC = "Src"; //window for the source image
namedWindow(WIN_SRC, CV_WINDOW_AUTOSIZE);
cvMoveWindow(WIN_SRC, 0, 0);
119
Appendix II. Image processing using OpenCV library
const char* WIN_DST = "Dst"; //window for the destination (processed) image
namedWindow(WIN_DST, CV_WINDOW_AUTOSIZE);
cvMoveWindow(WIN_DST, src_size.width + 10, 0);
imshow(WIN_SRC, src);
imshow(WIN_DST, dst);
The VideoCapture class provides the C++ API for capturing video from cameras or for
reading video files. In the following example is shown how you can use it (i.e. performing
canny edge detection on every frame and displaying the result in a destination window; the
example also shows how you can compute the processing time).
void testVideoSequence()
{
VideoCapture cap("Videos/rubic.avi"); // open a video file from disk
//VideoCapture cap(0); // open the default camera (i.e. the built in web cam)
if (!cap.isOpened()) // opening the video device failed
{
printf("Cannot open video capture device\n");
return;
}
// video resolution
Size capS = Size((int)cap.get(CV_CAP_PROP_FRAME_WIDTH),
(int)cap.get(CV_CAP_PROP_FRAME_HEIGHT));
const char* WIN_DST = "Dst"; //window for the destination (processed) frame
namedWindow(WIN_DST, CV_WINDOW_AUTOSIZE);
cvMoveWindow(WIN_DST, capS.width + 10, 0);
char c;
int frameNum = -1;
for (;;)
{
cap >> frame; // get a new frame from camera
if (frame.empty())
{
printf("End of the video file\n");
break;
}
++frameNum;
120
Appendix II. Image processing using OpenCV library
// Get the current time again and compute the time difference [s]
t = ((double)getTickCount() - t) / getTickFrequency();
The operations are generic for array type objects (i.e. Mat) and if the array is initialized with
an image, the result is a pixel level operation. In the case of multi-channel arrays, each
channel is processed independently. Some functions allow the specification of an optional
mask used to select a sub-array.
121
Appendix II. Image processing using OpenCV library
5. Morphological operations
In OpenCV the morphological operations work on both binary and grayscale images. Each
morphological operation requires a structuring element. This can be created using
getStructuringElement function:
his function creates a structuring element of given shape and dimension. The shape parameter
controls its shape and can take the following constant values:
CV_SHAPE_RECT
CV_SHAPE_CROSS
CV_SHAPE_ELLIPSE
The anchor parameter specifies the anchor position within the element. The default value (-1,
-1) means that the anchor is at the center. Note that only the shape of a cross-shaped element
depends on the anchor position. In other cases the anchor just regulates how much the result
of the morphological operation is shifted.
122
Appendix II. Image processing using OpenCV library
void morphologyEx(InputArray src, OutputArray dst, int op, InputArray kernel, Point
anchor=Point(-1,-1), int iterations=1, int borderType=BORDER_CONSTANT, const
Scalar& borderValue=morphologyDefaultBorderValue() )
6. Thresholding
The thresholding operation in OpenCV are performed with threshold function:
The operation is applied on the src image and the resulted binary image is stored in dst array.
The threshold value is specified by thresh parameter and the thresholding method is specified
by type parameter. The type parameter can take one of the following constant values:
( )
THRESH_BINARY ( ) {
( )
THRESH_BINARY_INV ( ) {
( )
THRESH_TRUNC ( ) {
( )
( ) ( )
THRESH_TOZERO ( ) {
( )
THRESH_TOZERO_INV ( ) {
( )
The above values can be combined with THRESH_OTSU. In this case, the function computes the
optimal threshold value using the Otsu’s algorithm (the implementation works only for 8-bit
123
Appendix II. Image processing using OpenCV library
images) which is used instead of the specified thresh parameter. The function returns the
computed optimal threshold value.
7. Filters
In OpenCV there are some optimized function designed to perform the filtering operations.
These optimizations depend however on the hardware and software architecture of the system.
void medianBlur(InputArray src, OutputArray dst, int ksize) – implements the median
filter
[ ]
void Sobel(InputArray src, OutputArray dst, int ddepth, int dx, int dy, int
ksize=3, double scale=1, double delta=0, int borderType=BORDER_DEFAULT )- computes
the first, second, third, or mixed image derivatives (dx and dy parameters) using an extended
Sobel operator. Most often, the function is called with (xorder = 1, yorder = 0, ksize = 3)
or (xorder = 0, yorder = 1, ksize = 3) to calculate the first x- or y- image derivative F (see
chapter 12 for more details).
void filter2D(InputArray src, OutputArray dst, int ddepth, InputArray kernel, Point
anchor=Point(-1,-1), double delta=0, int borderType=BORDER_DEFAULT )- this is a
generalized function designed to apply the filtering operation with a custom convolution
kernel. The kernel elements are defined in kernel parameter as a matrix. Default value (-1,-1)
for the anchor parameter means that the anchor is at the kernel center.
8. References
[1] OpenCV on-line documentation, http://docs.opencv.org/ , cited Dec. 2015.
124