Comparison between Open CV and MATLAB Performance in Real Time Applications

The most important operation in the image processing field is detect the objects inside an image or video, which it is used in many applications, especially in real time systems. Real time image processing in modern systems demands fast technologies, and there are variety methods to achieve this goal. This paper compare between the performance of (MATLAB) and (Open CV) to detect circular shapes that have specific color (blue) from live video in a real time. The input is a live video with resolution (640*480) pixels, taken by a web camera, and then processed by a sequence of image processing operations. The results refer to a program written in (Open CV) speed up the system up to 174% more than the same program written in (MATLAB) to do the same job.


INTRODUCTION
Nowadays, images and videos are all over the world, and the demand for high-speed and convenient real time processing for these images and videos is increased, especially after the development in digital communications and digital images devices. An efficient real time system is needed to extract more information from images and videos.
Matuska et al. in 2012 used images with various resolution, to calculate CPU Time consumption for image processing algorithm in MATLAB and Open CV. The results of their work show that Open CV is faster than MATLAB in some algorithm from 4 to 30 times and in some case up to 100 times [1]. Sharmila et al. in 2015 propose image conversion function, and test it on various images for data conversion and contrast stretching by using Open CV and MATLAB. The final calculation reveals the MATLAB code consumes more time for the same conversion. Moreover, the total time taken for the conversion in Open CV falls less than three seconds [2].
In this paper, the input frame from the camera is segmented to pick up the target color (blue), after that smooth the resultant image to reduce noise, and at the end circles detect. The rest of the paper is structured as follows: Section 2 discusses image processing steps, starting by frame reading, segmentation, smoothing, and ended by circular Hough transforms. Section 3 introduces Steps of work, in Section 4 the results and calculation of the comparison between MATLAB and OpenCV approaches. The conclusions are introduced in Section 5. Section 6 indicates References. At the time of writing, the latest release is Open CV v.2.4.7 and MATLAB R2013a (8.1.0.604) where available and used in this paper.

IMAGE PROCESSING
The block diagram in Figure (1) describes the sequence of image processing system, which is used to process live video from web camera. First, the system read a frame form input device (web camera). Then separate a specific color by using histogram segmentation and multi-level thresholding. After that, smooth the resultant frame by Gaussian filter. Finally, apply a circular Hough transform function to detect circular object inside the frame.

Read a Frame
The first step in any system that interact with real time video processing is reading the frame from an input device, the decrease in reading time leads to increase system efficiency. This paper is started by calculating the time conception in reading the frame from input device. Then made a comparison to find out, which is the best programing language (MATLAB or Open CV) to do that. The camera specifications that used as input device are listed in table (1).

Image Segmentation
Image segmentation is the separation of an image into categories or regions, which belong to different objects or parts of items. Every pixel in an image is classified to one of a number of these categories, a segmentation should meet the following requirements: 1. Pixels in the same category have analogous grayscale of multivariate values and form a connected region. 2. Adjacent pixels, which have different categories, have different values. Segmentation often is the significant step in image analysis, there is something should be taken in consideration, each pixel as a unit of observation to working with objects (or parts of objects) in the image, composed of numerous pixels. If segmentation is done fine then all other stages in image analysis are made simpler [5].

2.2.1.Color image segmentation by multi-level thresholding
Global histogram of a digital image is a popular tool for real-time image processing due to its simplicity in implementation. It serves as an important basis of statistical approaches in image processing by producing the global description of the image's information. In color image segmentation, color of a pixel is given as three values corresponding to the three component images red, green and blue (RGB). Component histograms provide additional information about the intensity distribution within the individual color channels. When computing component histograms, each color channel is considered a separate intensity image and each histogram is computed independently of the other channels [6].
Thresholding is the simplest and most generally used way for segmentation, specified a threshold value (T), or two values ( and for multi-level thresholding. The pixel located at pattern position (i,j), thresholding creates a binary image b(i,j) from an intensity image I(i,j), The multi-level thresholding is described in the following equation [3] [7].

……………. (1)
In this paper, histogram segmentation by multilevel threshold is applied directly to each component of a color space, and then the results is combined to obtain a final segmentation result. The threshold values of each color space, which is used in this paper, are shown in table (2).

Image Smoothing
Smoothing, also called blurring, is a simple and normally used image processing operation. There are many reasons for smoothing, but it is typically done to reduce noise or camera artifacts. Smoothing is also important to reduce the resolution of an image in a principled way. In practice, some different types of noise usually companied in image. Noise is a pixel that vary from its actual value in an image. Noise happen in digital image, while transmit image on computer networks, or pixel value in image obtained by image input devices that does not express the actual intensity from real scene produces noise [8].
Therefore, a respectable image smoothing algorithm should be able to deal with different types of noise. However, blur and offsets of the edges regularly caused by image smoothing. In image analysis and interpretation the edge information is much imperative. So, the precision of edge's position in image should be considered in smoothing process. [9].

2.3.1.Gaussian smoothing filter.
This type of smoothing used to 'blur' images to remove noise by a two dimensions convolution operation, as a 'point-spread' function. The need to produce a discrete approximation to the Gaussian function before convolution can performed, because of the image is stored as a collection of separate pixels. The Gaussian smoothing can be applied using standard convolution methods, the function of Gaussian smoothing showed below [8] [10]:

……………….. (2)
Where x and y are the spatial dimensions, while is the standard deviation of the Gaussian distribution. In this paper, the Gaussian filter is used as a smoothing filter, with values; x =9, y = 9, and = 0.1.

Circular Hough Transforms
One of the most frequently used algorithms to recognize different shapes in an image is Hough Transform. Hough Transform was invented by Paul Hough in 1962 and patented by IBM. Later then in 1972 Hough transform was modified, which is used commonly in the present day under the name Generalized Hough Transform. An extended form of General Hough Transform, used to identify circles is Circular Hough Transform. A flow-chart of circular Hough transform is shown in Figure (2). The edge detected from the Canny edge detector forms the input to extract the circle using the circular Hough transform [11].

2.4.2.Accumulator:
As shown in Figure (3) the black circles represents a set of edge points within the image. Each edge point contributes a circle of radius to an output accumulator space indicated by the dotted circles. The output accumulator space has a peak where these contributed circles with desired radius overlap at the center of the original circle [13].  Read frame from camera A ball with a blue color as shown in Figure (5 -a) is used as a target object in this paper. The position and distance of the ball are altered every time during the computing of the time for each step in random form. The tic-toc instructions in MATLAB were used to calculate the time of the execution in the space of seconds. While in Open CV (C++ language) clock_t function from time.h header was used to store the processor time, then the starting time was subtracted from the ending time to get the execution time, this had been done for each step of the processing.
The time taken in the space of milliseconds in C++, so the results obtained from MATLAB converted to milliseconds to achieve maximum accuracy. In every stage of the process, the execution time was calculated for hundreds of iterations then average time was found in milliseconds for each step in both programs (MATLAB and Open CV). Finally the two average times of each processing stage for MATLAB and Open CV was compared.

RESULTS AND CALCULATION
Comparison between MATLAB and Open CV: reading the frame from input device and histogram segmentation by multi-level thresholding was applied. After that, the resultant image will be smoothed by Gaussian filter, and then processed by Hough transform filter. The average time was calculated in millisecond for each step of execution, and the results are showed in Table (3) and Figure (6). Table (3): The results

Figure (6): Illustration of results
The results show that, the Open CV is faster than the MATLAB by about: A. 10% in reading frame from the camera. B. 2.6% in segmentation step. C. 94% in smoothing step. D. 68% in Hough Transform step.
Hence, the overall speedup ratio is about 174%. The main reason of the OpenCV processing speed in image processing is that, in MATLAB it is not necessary to care about memory allocation and memory leak, but it is very important task in every processing stage in Open CV. The effect of memory allocation is showed clearly in tasks that require more memory to process like smoothing and circle Hough transform process.
In addition, MATLAB is a pretty high-level scripting language, and it is built on Java, and Java is built upon C. Therefore, when run the MATLAB program, the computer is busy trying to interpret all that MATLAB code. Then turns it into Java, and then finally executes the code. Open CV is basically a library of functions written in C. Hence it is closer to directly provide machine language code to the computer to execute. Thus more image processing done at computers processing cycles, and not more interpreting. Because of that, programs written in Open CV run much faster than similar programs written in MATLAB.