Skip to content

Commit 2ad49ae

Browse files
committed
Use bincount rather than histogram for comparing images on Numpy >= 1.6. Continue to use histogram for earlier Numpy versions.
1 parent b58aa94 commit 2ad49ae

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

lib/matplotlib/testing/compare.py

+30-10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from matplotlib.testing.noseclasses import ImageComparisonFailure
1111
from matplotlib.testing import image_util
1212
from matplotlib import _png
13+
from distutils import version
1314
import math
1415
import operator
1516
import os
@@ -225,20 +226,39 @@ def compare_images( expected, actual, tol, in_decorator=False ):
225226
actualImage, expectedImage = crop_to_same(actual, actualImage, expected, expectedImage)
226227

227228
# normalize the images
228-
expectedImage = image_util.autocontrast( expectedImage, 2 )
229-
actualImage = image_util.autocontrast( actualImage, 2 )
229+
# expectedImage = image_util.autocontrast( expectedImage, 2 )
230+
# actualImage = image_util.autocontrast( actualImage, 2 )
230231

231232
# compare the resulting image histogram functions
232-
rms = 0
233-
bins = np.arange(257)
234-
for i in xrange(0, 3):
235-
h1p = expectedImage[:,:,i]
236-
h2p = actualImage[:,:,i]
233+
expected_version = version.LooseVersion("1.6")
234+
found_version = version.LooseVersion(np.__version__)
237235

238-
h1h = np.histogram(h1p, bins=bins)[0]
239-
h2h = np.histogram(h2p, bins=bins)[0]
236+
# On Numpy 1.6, we can use bincount with minlength, which is much faster than
237+
# using histogram
238+
if found_version >= expected_version:
239+
rms = 0
240+
241+
for i in xrange(0, 3):
242+
h1p = expectedImage[:,:,i]
243+
h2p = actualImage[:,:,i]
244+
245+
h1h = np.bincount(h1p.ravel(), minlength=256)
246+
h2h = np.bincount(h2p.ravel(), minlength=256)
247+
248+
rms += np.sum(np.power((h1h-h2h), 2))
249+
else:
250+
rms = 0
251+
ns = np.arange(257)
252+
253+
for i in xrange(0, 3):
254+
h1p = expectedImage[:,:,i]
255+
h2p = actualImage[:,:,i]
256+
257+
h1h = np.histogram(h1p, bins=bins)[0]
258+
h2h = np.histogram(h2p, bins=bins)[0]
259+
260+
rms += np.sum(np.power((h1h-h2h), 2))
240261

241-
rms += np.sum(np.power((h1h-h2h), 2))
242262
rms = np.sqrt(rms / (256 * 3))
243263

244264
diff_image = make_test_filename(actual, 'failed-diff')

0 commit comments

Comments
 (0)