hdr.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import cv2
  2. import numpy as np
  3. def readImagesAndTimes():
  4. times = np.array([ 1/30.0, 0.25, 2.5, 15.0 ], dtype=np.float32)
  5. filenames = ["img_0.033.jpg", "img_0.25.jpg", "img_2.5.jpg", "img_15.jpg"]
  6. images = []
  7. for filename in filenames:
  8. im = cv2.imread(filename)
  9. images.append(im)
  10. return images, times
  11. if __name__ == '__main__':
  12. # Read images and exposure times
  13. print("Reading images ... ")
  14. images, times = readImagesAndTimes()
  15. # Align input images
  16. print("Aligning images ... ")
  17. alignMTB = cv2.createAlignMTB()
  18. alignMTB.process(images, images)
  19. # Obtain Camera Response Function (CRF)
  20. print("Calculating Camera Response Function (CRF) ... ")
  21. calibrateDebevec = cv2.createCalibrateDebevec()
  22. responseDebevec = calibrateDebevec.process(images, times)
  23. # Merge images into an HDR linear image
  24. print("Merging images into one HDR image ... ")
  25. mergeDebevec = cv2.createMergeDebevec()
  26. hdrDebevec = mergeDebevec.process(images, times, responseDebevec)
  27. # Save HDR image.
  28. cv2.imwrite("hdrDebevec.hdr", hdrDebevec)
  29. print("saved hdrDebevec.hdr ")
  30. # Tonemap using Drago's method to obtain 24-bit color image
  31. print("Tonemaping using Drago's method ... ")
  32. tonemapDrago = cv2.createTonemapDrago(1.0, 0.7)
  33. ldrDrago = tonemapDrago.process(hdrDebevec)
  34. ldrDrago = 3 * ldrDrago
  35. cv2.imwrite("ldr-Drago.jpg", ldrDrago * 255)
  36. print("saved ldr-Drago.jpg")
  37. # Tonemap using Durand's method obtain 24-bit color image
  38. print("Tonemaping using Durand's method ... ")
  39. tonemapDurand = cv2.createTonemapDurand(1.5,4,1.0,1,1)
  40. ldrDurand = tonemapDurand.process(hdrDebevec)
  41. ldrDurand = 3 * ldrDurand
  42. cv2.imwrite("ldr-Durand.jpg", ldrDurand * 255)
  43. print("saved ldr-Durand.jpg")
  44. # Tonemap using Reinhard's method to obtain 24-bit color image
  45. print("Tonemaping using Reinhard's method ... ")
  46. tonemapReinhard = cv2.createTonemapReinhard(1.5, 0,0,0)
  47. ldrReinhard = tonemapReinhard.process(hdrDebevec)
  48. cv2.imwrite("ldr-Reinhard.jpg", ldrReinhard * 255)
  49. print("saved ldr-Reinhard.jpg")
  50. # Tonemap using Mantiuk's method to obtain 24-bit color image
  51. print("Tonemaping using Mantiuk's method ... ")
  52. tonemapMantiuk = cv2.createTonemapMantiuk(2.2,0.85, 1.2)
  53. ldrMantiuk = tonemapMantiuk.process(hdrDebevec)
  54. ldrMantiuk = 3 * ldrMantiuk
  55. cv2.imwrite("ldr-Mantiuk.jpg", ldrMantiuk * 255)
  56. print("saved ldr-Mantiuk.jpg")