Ask Your Question

cv2 calculate histogram 3x slower than pillow

asked 2019-11-26 01:01:39 -0600

coincheung gravatar image

updated 2019-11-26 01:02:24 -0600


My test code is like this:

n_test = 1000
t1 = time.time()
for i in range(n_test):
histogram = im.histogram()
t2 = time.time()
for i in range(n_test):
for c in range(3):
hist2 = cv2.calcHist([imcv[:, :, c]], [0], None, [256], [0, 256]).reshape(-1)
t3 = time.time()
for i in range(n_test):
for c in range(3):
hist3 = np.histogram(imcv[:,:,c].ravel(), bins=256, range=(0, 256))
t4 = time.time()
print('pil hist time: {}'.format(t2 - t1))
print('cv2 hist time: {}'.format(t3 - t2))
print('np hist time: {}'.format(t4 - t3))

The result is that cv2 implementation is around 3x slower than pillow. Did I make any mistake here, how could I use cv2/numpy to make it as fast as pillow?

edit retag flag offensive close merge delete


don't use time.time() for profiling code (wall time), but some cpu clock, like cv2.getTickCount()

berak gravatar imageberak ( 2019-11-26 01:27:00 -0600 )edit

1 answer

Sort by » oldest newest most voted

answered 2019-11-26 01:41:02 -0600

coincheung gravatar image

Thanks for replying, after change time.time() into cv2.getTickCount(), the cv2 code is still slower than pil. Do I have a faster way to do it?

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2019-11-26 01:01:39 -0600

Seen: 381 times

Last updated: Nov 26 '19