Please see the following blog post for more details about this code High Dynamic Range (HDR) Imaging using OpenCV (C++/Python)