We have already seen that OpenCV provides the cv2.calcHist() function to calculate histograms. Additionally, NumPy and Matplotlib offer similar functions for the creation of histograms. In the comparing_opencv_numpy_mpl_hist.py script, we are comparing these functions for performance purposes. In this sense, we are going to see how to create histograms with OpenCV, NumPy, and Matplotlib, and then measure the execution time for each one and plot the results in a figure.
With the purpose of measuring the execution time, we are using timeit.default_timer because it provides the best clock available on your platform and version of Python automatically. In this way, we import it at the beginning of the script:
from timeit import default_timer as timer
The way we use the timer is summarized here:
start = timer()
# ...
end = timer()
execution_time...