Vous êtes sur la page 1sur 4

The Comparison of CPU Time Consumption for

Image Processing Algorithm in Matlab and


OpenCV
Slavomir Matuska *, Robert Hudec  and Miroslav Benco*
* University of Zilina/ Department of Telecommunication and Multimedia, Zilina, Slovakia, e-mail:

 

 
    
Python.
Ruby.
Matlab.
And other.
At the time of writing, the latest release is 2.3.1 (August
2011) [1, 2, 3].
A graphical interface can be created by Qt crossplatform application and UI framework with APIs for
C++ programming and Qt Quick for rapid UI creation.

Abstract In order to fill gap of growing demand for high


efficient image and video processing, open source computer
vision library (OpenCv) is way to deals with this task.
Hence, this paper is about basic algorithm for image
processing and their CPU time consumption in Matlab
comparing with OpenCv. Algorithms are tested on images
with resolution 3264x2448, 1920x1080, 1024x768 and
220x260. Multi-processors computer and multi-threading
programs are used to improve processing efficiency.
KeywordsOpenCV, Matlab, computer vision, mutlithreading.

B. Working with Multi-Processor Station


In order to improve efficiency of multi-core computer
use, it is necessary to write optimized codes. Using more
threads or worker.
The workers in Matlab are provided by function
matlabpool. This function opens and closes pool of
Matlab session for parallel computation. Matlabpool
enables the parallel language features by starting a
parallel job that connects this Matlab client with number
of workers. Maximum number of workers is determinated
by number of processors [6].
Multi-threading in OpenCv can be achieved in
different ways. The Threading Building Blocks (TBB),
offered by Intel, is one of the ways. This solution offers a
rich and complete approach to expressing parallelism in a
C++ program [7]. Other way is use of OpenMP (Open
Multi-Processing).
In this work is used another way, following POSIX
(Portable Operating System Interface) threads standard.
Threads are multiple strands of execution in a single
program. A more precise definition is that a thread is a
sequence of control within a process. POSIX Threads,
usually referred to as Pthreads, defines a set of C
programming language types, functions and constants. It
is implemented with a pthread.h header and a thread
library. Function to create a new thread is pthread_create:
int pthread_create (pthread_t *thread, pthread_attr_t

I. INTRODUCTION
In todays digital word, images and videos are
everywhere, and with the advent of powerful and
affordable computer device, it has been never easier to do
computer vision [1]. Development of ICT technologies
(Information and Communication Technology) and their
implementation in common live expand demand for more
efficient way to work with images and videos. Especially
in real-time application, it is necessary to use the most
efficient computer vision. In result CPU time consuming
of basic algorithm in computer vision is compared.
A. OpenCv
OpenCv is an open source computer vision library
containing more than 500 optimized algorithms for image
and video analysis, including factory product inspection,
medical imaging, security, user interface, camera
calibration, stereo vision and robotics. Since its
introduction in 1999, it has been largely adopted as the
primary development tool by the community of
researchers and developers in computer vision [1, 3].
The library is written in optimized C and takes
advantage of multi-core processors. Version 1.0 was
launched in 2006 and second major release occurred in
2009 with launch of OpenCv 2 that proposed important
changes, especially the new C++ interface. This new
interface seeks to reduce the number of lines of code
necessary to code up vision functionality as well as
reduce common programming errors such as memory
leaks (through automatic data allocation and deallocation) that can arise when using OpenCV in C.
Recently, the most of new developments and
algorithms in OpenCV are developed in the C++
interface. There is active development on interfaces for:

978-1-4673-1179-3/12/$31.00 2012 IEEE

*attr, void *(*start_routine)(void *), void *arg).


The first argument is a pointer to prhread_t. Second
sets thread attributes, in most case, this argument is set to
NULL. Next argument is a thread function that is
executing with thread start. Last argument contains data,
which are passed to the thread function. When a thread
terminates, the pthread_exit function is called. In main

75

process, pthread_join is a function, that processes use to


collect child processes [4].

void cvPyrUp(*src, *dst, filter).


Arguments src and dst are source and destination images,
filter supports only 5-by-5 Gaussian kernel [3, 5].

II. IMAGE PROCESSING


This section presents some basic algorithms used in
low-level vision processes and also used for testing CPU
time consuming.

D. Edge Detection
Edge detection is an important pre-processing step in
image analysis. In general, the most common operator
used to represent edges is the Sobel derivative operator.
OpenCV provides function:
cvSobel(*src, *dst, xorder, yorder, aparature_size).

A. Smoothing
Smoothing is a simple and frequently used image
processing operation. There are many reasons for
smoothing, but it is usually done to reduce noise, camera
artifacts or help finding edges. OpenCV offers five
different smoothing operations, which are supported
through one function:
void cvSmooth( *src, *dst, smoothtype, p1, p2, p3, p4).

Arguments src and dst are input and output image, xorder
and yorder are the orders of derivate. Typical values are
0,1 or at most 2. The aperture_size parameter should be
odd and determine the width of the square filter. Sobel
derivatives have the nice property that they can be
defined for kernels of any size, and those kernels can be
constructed quickly and iteratively. The larger kernels
give a better approximation to the derivative because the
smaller kernels are very sensitive to noise.
Canny edge detector is an edge detection operator that
uses a multi-stage algorithm to detect a wide range of
edges in images. The first derivatives are computed in x
and y and then combined into four directional derivatives.
The points where these directional derivatives are local
maximum are then candidates for assembling into edges.
OpenCv provides function:
void cvCanny(*img, *edges, low_thresh, high_thresh,

The src and dst arguments are source and destination


images for smooth operation. Type of smoothing
operation is dedicated by smoothtype parameter, which
can be: CV_BLUR (mean filter), CV_MEDIAN (median
value),
CV_BLUR_NO_SCALE
(summation),
CV_GAUSSIAN (Gaussian filter) and CV_BILATERAL
(bilateral 3x3). The meaning of parameters p1, p2, p3, p4
depends on the value of smoothtype [3, 5].
B. Image Morphology
The basic morphological transformations are called
dilation and erosion, and they arise in a wide variety of
contexts such as removing noise, isolating individual
elements, and joining disparate elements in an image.
Dilation is a convolution of some image with some
kernel. The kernel, which can be any shape or size, has a
single defined anchor point. Most often, the kernel is a
small solid square or disk with the anchor point at the
center. OpenCv provides function:
void cvErode(*src, *dst,*kernel, iteration).

opertureSize).
Function expects an input image, which has to be
grayscale, and an output image, which has to be also
grayscale. The next two arguments are the low and high
thresholds, and the last argument is another aperture. As
usual, this is the aperture used by the Sobel derivative
operators that are called inside of the implementation of
cvCanny() [3, 5, 8].

Erode is equivalent to computing a local minimum over


the area of the kernel. OpenCv provides function:
void cvDilate(*src, *dst,*kernel, iteration).

III. EXPERIMANTAL RESULTS


Algorithms were tested on 16-cores work station, 16
Gb of RAM memory and Linux based operation system.
The resolution of used source images are 220x260,
1024x768, 1920x1080 and 3264x2448.
Matlab supports up to 8 workers. In OpenCv and
Linux based system is only limitation the total number of
processes on the system, which can be found in
/proc/sys/kernel/threads-max. In experiment is used up to
64 threads. Every algorithm was tested 1000 times in
OpenCV and 100 times in Matlab, and this was repeated
10 times.
Average computing time per one image is presented
and was calculated as follows:

The src and dst are source and destination images. The
third argument is the kernel, which default value is
NULL. In the NULL case, the kernel used is a 3-by-3
kernel with the anchor at its center. You can make your
own custom morphological kernel using IplConvKernel.
Last argument is the number of iterations with default
value set to 1 [3, 5, 8].
C. Image Pyramids
Image pyramids are heavily used in a wide variety of
vision applications. An image pyramid is a collection of
images, all arising from a single original image, that are
successively down-sampled until some desired stopping
point is reached. They are two kinds of image pyramids:
the Gaussian and Laplacian pyramids. The Gaussian
pyramid is used to down-sample images, and the
Laplacian pyramid is required when is needed to
reconstruct an up-sampled image from an image lower in
the pyramid. OpenCv provides function:
void cvPyrDown(*src, *dst, filter).

(1)

where is average computation time of particular


algorithm and expresses number of images.
The computing times per one image for smoothing
algorithm in OpenCv and Matlab are shown in Table I
and Table II. In comparison of CPU time consumption,

76

algorithms written with OpenCv library had achieved


much better results.

As is shown in the Table VI, using more workers didnt


improve computing time per image in Matlab except
image with resolution 220x260.

TABLE I.
OPENCV - SMOOTHING
Number
of threads
1
2
4
8
16
32
64

220x260
(ms)
0.5
0.95
0.56
0.42
0.36
0.31
0.27

1024x768
(ms)
4.37
5.03
3.35
2.24
1.55
1.46
1.15

1920x1080
(ms)
20.97
16.56
8.79
5.42
4.53
4.31
4.13

TABLE V.
OPENCV IMAGE PYRAMID
3264x2448
(ms)
77.31
57.27
31.83
21.26
18.8
17.9
18.1

Number
of threads
1
2
4
8
16
32
64

TABLE II.
MATLAB SMOOTHING
Number
of threads
1
2
4
8

220x260
(ms)
21.45
8.58
4.94
3.11

1024x768
(ms)
163.2
69.25
35.86
20.45

1920x1080
(ms)
672.93
307.2
157.61
92.99

Number
of threads
1
2
4
8

1
2
4
8
16
32
64

1024x768
(ms)
1.05
2.22
1.57
1.07
0.95
1.08
0.83

1920x1080
(ms)
6.66
8.02
5.32
3.17
2.32
2.52
2.31

1
2
4
8

220x260
(ms)
21.46
20.12
10.77
6.92

1024x768
(ms)
148.57
146.22
82.89
52.26

1920x1080
(ms)
541.84
675.64
368.97
232.27

3264x2448
(ms)
35.67
23.3
13.08
7.41
5.16
5.72
6.16

220x260
(ms)
2.86
3.24
2.1
1.5

1024x768
(ms)
5.33
14.1
8.03
5.05

1920x1080
(ms)
14.16
57.35
30.9
18.37

3264x2448
(ms)
39.23
213.3
111.26
65.24

TABLE VII.
OPENCV SOBEL DERIVATE OPERATOR
Number
of threads
1
2
4
8
16
32
64

3264x2448
(ms)
27.02
24.74
14.36
10.04
8.93
9.2
9.05

220x260
(ms)
0.81
1.15
0.71
0.45
0.35
0.39
0.33

1024x768
(ms)
6.13
6.14
3.88
2.45
1.57
1.61
1.5

1920x1080
(ms)
28.12
18.65
10.68
6.06
3.91
4.07
4.44

3264x2448
(ms)
107.16
66.3
35.95
19.97
14.04
13.67
14.01

TABLE VIII.
MATLAB SOBEL DERIVATE OPERATOR
Number
of threads

TABLE IV.
MATLAB EROSION
Number
of threads

1920x1080
(ms)
8.47
9.05
6.3
3.87
2.5
2.61
2.64

Sobel derivate operator and Canny edge detector has


achieved relatively good results for Matlbab. Sobel
operator was from 4 to 15 times slower in Matlab. In both
algorithms (Matlab and OpenCV) function for convert 3channels image to gray-scale image were included. The
computing times per one image for Sobel derivate
operator are shown in Table VII and Table VIII.

TABLE III.
OPENCV EROSION
220x260
(ms)
0.15
0.43
0.27
0.25
0.28
0.22
0.18

1024x768
(ms)
1.83
2.55
1.54
1.03
0.81
0.92
0.81

TABLE VI.
MATLAB IMAGE PYRAMID
3264x2448
(ms)
2755.45
1313.3
739.95
482.48

Smoothing algorithm in OpenCv with one thread was


from 30 to 40 times faster than algorithm in Matlab. With
more threads, OpenCV was from 8 to 24 times faster.
OpenCv wasnt achieved much better result using more
threads processing image with lower resolution, because
smoothing is too simple operation and most CPU time
consume main handler function. In most tested algorithm,
best results were achieved using 16 threads.
Results from Erosion algorithm are shown in Table III
and Table IV. In Erosion algorithm, Matlab achieved
worst results. OpenCV was faster from 24 to 100 times

Number
of threads

220x260
(ms)
0.24
0.43
0.27
0.22
0.18
0.15
0.13

1
2
4
8

3264x2448
(ms)
2060.95
2658.17
1500.85
965.92

220x260
(ms)
6.54
4.73
2.95
2.15

1024x768
(ms)
34.5
34.24
18.91
11.03

1920x1080
(ms)
144.64
167.13
85.12
49.62

3264x2448
(ms)
487.56
709.51
370.61
207.85

Canny edge detection was from 10 to 40 times slower


in Matlab than the same algorithm in OpenCv. This
results are shown in Table IX and Table X.

and with one thread up to 140 times. In comparing to


Image pyramid, Matlab was slower only from 6 to 12
times. This results are shown in Table V and Table VI.

77

TABLE IX.
OPENCV CANNY EDGE DETECTOR
Number
of threads

220x260
(ms)
1.16
1.23
1.03
0.69
0.55
0.68
0.56

1
2
4
8
16
32
64

1024x768
(ms)
16.75
11.12
6.81
4.38
3.31
3.09
3

1920x1080
(ms)
41.39
25.03
15.12
9.28
6.95
7.72
8.87

very important task in OpenCV. This allocation and


releasing memory inserts lines into the code. Matlab
needs for smoothing algorithm 2 lines, but OpenCv needs
5 lines to write source code with the same functionality.
Using more threads or worker is simpler in Matlab,
only one function is needed to start parallel computing
and one function to stop. In multi-threading following
POSIX standard is necessary to write own handle
function. In our case, this function have about 30 lines of
codes, but it is possible to use as many threads, as is
needed.

3264x2448
(ms)
215.28
150.51
66.99
38.91
27.38
27.66
28.26

ACKNOWLEDGMENT
This contribution/publication is the result of the project
implementation: Centre of excellence for systems and
services of intelligent transport, ITMS 26220120050
supported by the Research & Development Operational
Programme funded by the ERDF.

TABLE X.
MATLAB CANNY EDGE DETECTOR
Number
of threads

220x260
(ms)
34.62
18.89
10.35
6.17

1
2
4
8

1024x768
(ms)
266.01
155.21
79.99
49.58

1920x1080
(ms)
1070.89
682.93
358.13
223.98

3264x2448
(ms)
4419.55
2743.17
1563.46
1138.89

REFERENCES
[1]

For graphical point of view, comparison of time


consuption of Sobel algorithm for image with resolution
1024x768 in OpenCV and Matlab is shown in Fig. 1.
OpenCv with 8 threads was faster than Matlab with 8
workers 4.5 times and with 16 threads against 8 workers
7 times. More threads then 16 in OpenCv didnt
improved computing time of Sobel algorithm.

[2]
[3]
[4]

[5]
[6]

Computing Time ( ms)

40

OpenCv Sobel

35

[7]

Matlab Sobel

30

[8]

25
20
15
10
5
0
1

16

32

64

Number of threads

Fig. 1. CPU time consuption in Matlab and OpenCv, Sobel operator.

IV. CONCLUSION
In this paper, we have presented basic algorithm for
image processing, focusing for their CPU time
consuption in Matlab and OpenCV. Results showed, that
OpenCv is faster than Matlab in some algorithm from 4
to 30 times and in case of Erosion algorithm up to 100
times. On the other hand, Matlab environment is relative
simply and friendly to use, and provides various sort of
function and algorithm. It isnt necessary to care about
memory allocation and memory leak in Matlab, but it is

78

Robert Laganiere, OpenCV 2 Computer Vision Application


Programming Cookbook, May 2011.
J. R.Parker, Algorithms for Image Processing and Computer
vision, second edition, Indianapolis, 2011.
Gary Bradski & Adrian Kaebler, Learning OpencCV, September
2008.
Neil Matthew and Richard Stones, Linux Zanme
programovat, preklad 4. Vydan, Computer Press, Brno, 2008,
pp. 540-595.
OpenCV Reference Manual, v 2.1, march 18, 2010
MathWorks documentation,
http://www.mathworks.com/help/toolbox/distcomp/matlabpool.ht
ml , available online on 20.3.2012,
OpenCv on-line documentation,
http://opencv.willowgarage.com/wiki/TBB , available online on
20.3.2012
John C. Russ, The Image Processing Handbook,, 5rd ed., Taylor &
Francis Group, 2007, pp.460480, 270-300.

Vous aimerez peut-être aussi