Skip to content

Instantly share code, notes, and snippets.

@saccadic
Created February 4, 2019 06:33
Show Gist options
  • Save saccadic/67e463fbfafb05b0facecb9a590bee0b to your computer and use it in GitHub Desktop.
Save saccadic/67e463fbfafb05b0facecb9a590bee0b to your computer and use it in GitHub Desktop.
ディレクトリ内にある画像をすべて合成して三種類の方式でHDR画像を作成するスクリプト
# coding: utf-8
# In[1]:
import os
from pathlib import Path
from tqdm import tqdm
import pandas as pd
import numpy as np
import cv2
# In[2]:
# Create HDR Image Function
def MakeHDR(imagePathList, expTime, savePath, name):
# 画像ファイルを読み込む
img_list = [cv2.imread(str(fn)) for fn in imagePathList]
# 露出時間をセット
exposure_times = np.array(expTime, dtype=np.float32)
# Debevec法によるHDR合成
merge_debvec = cv2.createMergeDebevec()
hdr_debvec = merge_debvec.process(img_list, times=exposure_times.copy())
tonemap1 = cv2.createTonemapDurand(gamma=2.2)
res_debvec = tonemap1.process(hdr_debvec.copy())
# Robertson法によるHDR合成
merge_robertson = cv2.createMergeRobertson()
hdr_robertson = merge_robertson.process(img_list, times=exposure_times.copy())
tonemap2 = cv2.createTonemapDurand(gamma=2.2)
res_robertson = tonemap2.process(hdr_robertson.copy())
# Mertens法によるHDR合成
merge_mertens = cv2.createMergeMertens()
res_mertens = merge_mertens.process(img_list)
# 8ビットデータに変換
res_debvec_8bit = np.clip(res_debvec*255, 0, 255).astype('uint8')
res_robertson_8bit = np.clip(res_robertson*255, 0, 255).astype('uint8')
res_mertens_8bit = np.clip(res_mertens*255, 0, 255).astype('uint8')
# 画像をファイルに保存
cv2.imwrite(savePath + "/ldr_debvec/" + name + "_ldr_debvec.jpg", res_debvec_8bit)
cv2.imwrite(savePath + "/ldr_robertson/" + name + "_ldr_robertson.jpg", res_robertson_8bit)
cv2.imwrite(savePath + "/fusion_mertens/" + name + "_fusion_mertens.jpg", res_mertens_8bit)
# In[3]:
#Setup pathlib
currentPath = Path()
#Set target path
currentPath = currentPath / '2018-10-26-17-24-17-544/'
#Declare dataset list
grupPath = []
#Searth image paths
pathList = list(currentPath.glob('**/*.png'))
parentList = []
for i in range(len(pathList)):
parentList.append(pathList[i].parent)
#Remove duplication
parentList = list(set(parentList))
#Make dataset list
for i in range(len(parentList)):
parentPath = parentList[i]
if(str(parentPath).find('test') == -1 and str(parentPath).find('result') == -1):
imagePathList = [p.resolve() for p in parentPath.iterdir()]
expTimeList = []
for t in range(len(imagePathList)):
filename = str(imagePathList[t].stem)
nameStruct = filename.split('_')
expTimeList.append(float(nameStruct[1]))
data=[parentPath.absolute(), imagePathList, expTimeList]
grupPath.append(data)
#Output path
for i in range(len(grupPath)):
print(" + " + grupPath[i][0].name)
for t in range(len(grupPath[i][1])):
print(" |-- " + grupPath[i][1][t].name + ", " + str(grupPath[i][2][t]))
# In[4]:
# For convert to csv
# data = dict()
# data['path'] = grupPath[i][1]
# data['time'] = grupPath[i][2]
# d = pd.DataFrame.from_dict(data)
# d.to_csv('./time.csv')
#Main processing
for i in tqdm(range(len(grupPath))):
MakeHDR(grupPath[i][1],grupPath[i][2],str(grupPath[i][0].parent), grupPath[i][0].name)
saveDirPath = str(grupPath[i][0].parent) + "/" + "result"
#Make save directory
try:
os.mkdir(saveDirPath)
os.mkdir(saveDirPath+"/ldr_robertson")
os.mkdir(saveDirPath+"/ldr_debvec")
os.mkdir(saveDirPath+"/fusion_mertens")
except FileExistsError:
print("Exists")
MakeHDR(grupPath[i][1],grupPath[i][2],saveDirPath,grupPath[i][0].name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment