-
Notifications
You must be signed in to change notification settings - Fork 3
/
python-image-processing-libraries-performance-opencv-scipy-scikit-image.html
270 lines (210 loc) · 8.9 KB
/
python-image-processing-libraries-performance-opencv-scipy-scikit-image.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<!DOCTYPE html>
<html lang="en" itemscope itemtype="http://schema.org/Article">
<head>
<title>Python image processing libraries performance: OpenCV vs Scipy vs Scikit-Image</title>
<meta charset="utf-8">
<meta property="og:title" content="Python image processing libraries performance: OpenCV vs Scipy vs Scikit-Image">
<meta property="og:site_name" content="Modesto Mas | Blog">
<meta property="og:image" content="https://mmas.github.io/images/profile.jpg">
<meta property="og:image:width" content="200">
<meta property="og:image:height" content="200">
<meta property="og:url" content="https://mmas.github.io/python-image-processing-libraries-performance-opencv-scipy-scikit-image">
<meta property="og:locale" content="en_GB">
<meta name="twitter:image" content="https://mmas.github.io/images/profile.jpg">
<meta name="twitter:url" content="https://mmas.github.io/python-image-processing-libraries-performance-opencv-scipy-scikit-image">
<meta name="twitter:card" content="summary">
<meta name="twitter:domain" content="mmas.github.io">
<meta name="twitter:title" content="Python image processing libraries performance: OpenCV vs Scipy vs Scikit-Image">
<meta name="description" content="We are going to compare the performance of different methods of image processing using three Python libraries (scipy, opencv and scikit-image). All th...">
<meta name="twitter:description" content="We are going to compare the performance of different methods of image processing using three Python libraries (scipy, opencv and scikit-image). All th...">
<meta property="og:description" content="We are going to compare the performance of different methods of image processing using three Python libraries (scipy, opencv and scikit-image). All th...">
<meta name="keywords" content="image-processing,numpy,opencv,python,scikit-image,scipy">
<meta property="og:type" content="blog">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta property="og:type" content="article">
<meta property="article:author" content="https://github.com/mmas">
<meta property="article:section" content="image-processing">
<meta property="article:tag" content="image-processing,numpy,opencv,python,scikit-image,scipy">
<meta property="article:published_time" content="2015-02-16">
<meta property="article:modified_time" content="2015-02-16">
<link rel="stylesheet" type="text/css" href="/css/main.css">
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
CommonHTML: {
scale: 93,
showMathMenu: false
},
tex2jax: {
"inlineMath": [["$","$"], ["\\(","\\)"]]
}
});
</script>
<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML"></script>
</head>
<body class="entry-detail">
<header>
<div>
<img src="https://mmas.github.io/images/profile.jpg">
<a class="brand" href="/">Modesto Mas</a>
<span>Data/Python/DevOps Engineer</span>
<nav>
<ul>
<li><a href="/tags">Tags</a></li>
<li><a href="https://github.com/mmas/mmas.github.io/issues" target="_blank">Issues</a></li>
</ul>
</nav>
</div>
</header>
<section id="content" role="main">
<article>
<header>
<h1><a href="/python-image-processing-libraries-performance-opencv-scipy-scikit-image">Python image processing libraries performance: OpenCV vs Scipy vs Scikit-Image</a></h1>
<time datetime="2015-02-16">Feb 16, 2015</time>
<a class="tag" href="/tags?tag=image-processing">image-processing</a>
<a class="tag" href="/tags?tag=numpy">numpy</a>
<a class="tag" href="/tags?tag=opencv">opencv</a>
<a class="tag" href="/tags?tag=python">python</a>
<a class="tag" href="/tags?tag=scikit-image">scikit-image</a>
<a class="tag" href="/tags?tag=scipy">scipy</a>
</header>
<aside id="article-nav"></aside>
<section class="body">
<p>We are going to compare the performance of different methods of image processing using three Python libraries (<a href="http://www.scipy.org/scipylib/index.html" target="_blank">scipy</a>, <a href="http://opencv.org/" target="_blank">opencv</a> and <a href="http://scikit-image.org/" target="_blank">scikit-image</a>). All the tests will be done using <a href="https://docs.python.org/2/library/timeit.html" target="_blank">timeit</a>. Also, in the case of OpenCV the tests will be done with the optimization enabled (by default if supported by CPU) and disabled:</p>
<pre>
>>> cv2.useOptimized()
True
>>> cv2.setUseOptimized(False)
>>> cv2.useOptimized()
False
</pre>
<h2>Requirements</h2>
<pre>
% sudo apt-get install -y build-essential libblas-dev liblapack-dev gfortran cmake git \
libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev python-dev \
python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
% sudo pip install scipy scikit-image
% mkdir tmp && cd tmp
% git clone https://github.com/Itseez/opencv.git
% git clone https://github.com/Itseez/opencv_contrib.git
% cd opencv
% mkdir build && cd build
% cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
% make -j7
% sudo make install
% bin/opencv_test_core
</pre>
<h2>Linear filter: gaussian (3-channel)</h2>
<pre>
import numpy as np
import scipy.ndimage as nd
from skimage import data, morphology, filter as imfilter
import cv2
</pre>
<pre>
img = data.lena()
# scipy
dst = np.zeros(img.shape, img.dtype)
for i in xrange(img.shape[2]):
dst[:, :, i] = nd.gaussian_filter(img[:, :, i], 5)
# opencv
dst = cv2.GaussianBlur(img, (0, 0), 5)
# scikit-image
dst = imfilter.gaussian_filter(img, 5, multichannel=True)
</pre>
<img src="/images/lena_gaussian.png" alt="image filter: gaussian filter">
<h2>Linear filter: sobel (1-channel)</h2>
<pre>
img = data.lena()[:, :, 1]
# scipy
dst = nd.sobel(img, 1)
# opencv
dst = cv2.Sobel(img, cv2.CV_8U, 1, 0)
# scikit-image
dst = imfilter.hsobel(img)
</pre>
<img src="/images/lena_sobel.png" alt="image filter: sobel filter">
<h2>Non-linear filter: median (3-channel)</h2>
<pre>
img = data.lena()
# scipy
dst = np.zeros(img.shape, img.dtype)
for i in xrange(img.shape[2]):
dst[:, :, i] = nd.median_filter(img[:, :, i], 5)
# opencv
dst = cv2.medianBlur(img, 5)
</pre>
<img src="/images/lena_median.png" alt="image filter: median filter">
<h2>Morphological operation: dilation (1-channel)</h2>
<pre>
img = np.where(data.coins() > 128, 1, 0).astype(np.uint8)
kernel = np.ones((5,5), np.uint8)
# opencv
dst = cv2.dilate(img, kernel, iterations=1)
# scipy
dst = nd.binary_dilation(img, kernel, iterations=1)
# scikit-image
dst = morphology.binary_dilation(img, kernel)
</pre>
<img src="/images/coins_dilate.png" alt="image morphology: dilation">
<h2>Results</h2>
<p>We can see a general better performance in OpenCV (with optimization enabled), specially in a non-linear operation (median filter); a light difference in an easy computable filter as the sobel; and the scikit-image in the last position of performance.</p>
<table>
<thead>
<tr>
<th></th>
<th>gaussian filter (rgb)</th>
<th>sobel filter (gray)</th>
<th>median filter (rgb)</th>
<th>dilation (binary)</th>
</tr>
</thead>
<tbody>
<tr>
<th style="text-align:right;">scipy</th>
<td>0.0626</td><td>0.00419</td><td>0.568</td><td>0.00452</td>
</tr>
<tr>
<th style="text-align:right;">opencv*</th>
<td>0.0588</td><td>0.00592</td><td>0.173</td><td>0.00118</td>
</tr>
<tr>
<th style="text-align:right;">opencv</th>
<td>0.0139</td><td>0.00684</td><td>0.0052</td><td>0.000104</td>
</tr>
<tr>
<th style="text-align:right;">scikit-image</th>
<td>0.0729</td><td>0.00746</td><td></td><td>0.0154</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="5" style="text-align:right">
Time (in seconds) used per process for each of the four libraries (the less the better)<br>
* OpenCV with optimization disabled
</td>
</tr>
</tfoot>
</table>
<pre>
import pandas as pd
import matplotlib.pyplot as plt
data = pd.DataFrame(
[[0.0626, 0.00419, 0.568, 0.00452],
[0.0588, 0.00592, 0.173, 0.00118],
[0.0139, 0.00684, 0.005, 0.000104],
[0.0729, 0.00746, np.nan, 0.0154]],
index=['scipy', 'opencv*', 'opencv', 'scikit-image'],
columns=['gaussian_filter', 'sober_filter', 'median_filter', 'dilation'])
data.T.plot(kind='barh', grid=False)
plt.xlabel('seconds')
plt.axes().xaxis.grid(True)
plt.show()
</pre>
<img src="/images/image_processing_performance_plot.png" alt="python image processing performance plot">
</section>
</article>
</section>
<footer></footer>
<script type="text/javascript" src="/js/article.js"></script>
</body>
</html>