diff --git a/README.md b/README.md index f9ebc0f..54487be 100644 --- a/README.md +++ b/README.md @@ -29,10 +29,15 @@ usage: main.py [-h] [--test_path] ``` ``` -optional arguments: -h, --help show this help message and exit - --test_path path to test images - --tonemap choose tonemap method - --cmap save radiance map +usage: gui.py +``` + +``` +optional arguments: -h, --help show this help message and exit + --test_path path to test images + --tonemap choose tonemap method + --cmap save radiance map + --hdr_radiance_path in case of non-empty parameter hdr radiance map will be loaded from a file, if file does not exists then it will be calculate and stored ``` #### Result diff --git a/gui.py b/gui.py new file mode 100644 index 0000000..a5b75fb --- /dev/null +++ b/gui.py @@ -0,0 +1,107 @@ +import os +import glob +import cv2 +import argparse +import numpy as np +from utils import * + +gamma = 130 +tonemapType = 0 + +def proc_hdr(): + if tonemapType == 0: + output1 = np.uint8(globalTonemap(hdr_img, gamma / 100.) * 255.) + elif tonemapType == 1: + tm = cv2.createTonemapMantiuk(gamma / 100.) + output1 = np.uint8(255. * tm.process((hdr_img/255.).astype(np.float32))) + elif tonemapType == 2: + tm = cv2.createTonemapReinhard(gamma / 100.) + output1 = np.uint8(255. * tm.process((hdr_img/255.).astype(np.float32))) + elif tonemapType == 3: + tm = cv2.createTonemapDrago(gamma / 100.) + output1 = np.uint8(255. * tm.process((hdr_img/255.).astype(np.float32))) + #elif tonemapType == 4: + #merge_mertens = cv2.createMergeMertens() + #output1 = merge_mertens.process(stack) + + #template = stack[len(stack)//2] + #image_tuned = intensityAdjustment(output, template) + + output = cv2.normalize(output1, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX) + cv2.imshow('image', output.astype(np.uint8)) + return output + +def change_gamma(value): + global gamma + gamma = max(value, 1) + proc_hdr() + +def change_tonemap(value): + global tonemapType + tonemapType = value + proc_hdr() + +def dump(): + from matplotlib.pylab import cm + colorize = cm.jet + cmap = np.float32(cv2.cvtColor(np.uint8(hdr_img), cv2.COLOR_BGR2GRAY)/255.) + cmap = colorize(cmap) + cv2.imwrite(os.path.join(config.test_path, 'cmap.jpg'), np.uint8(cmap*255.)) + cv2.imwrite(os.path.join(config.test_path, 'hdr_radiance.tiff'), hdr_img) + file_name = "hdr" + "_" + str(gamma) + "_" + str(tonemapType) + ".jpg" + cv2.imwrite(os.path.join(config.test_path, file_name), proc_hdr()) + +def main(config): + list_file, exps = load(config.test_path) + global stack + stack = read(list_file) + + num_channels = stack.shape[-1] + global hdr_img + hdr_img = np.zeros(stack[0].shape, dtype=np.float64) + + if config.hdr_radiance_path == '': + for c in range(num_channels): + layer_stack = [img[:,:,c] for img in stack] + + sample = sample_intensity(layer_stack) + + curve = estimate_curve(sample, exps, 100.) + + img_rad = computeRadiance(np.array(layer_stack), exps, curve) + + hdr_img[:,:, c] = cv2.normalize(img_rad, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX) + else: + hdr_img = cv2.imread(config.hdr_radiance_path, -1) + + proc_hdr() + + +if __name__ == "__main__": + + parser = argparse.ArgumentParser() + + parser.add_argument('--test_path', type=str, default="imgs2/") + parser.add_argument('--hdr_radiance_path', type=str, default="imgs2/hdr_radiance_map.tiff") + + config = parser.parse_args() + main(config) + + cv2.createTrackbar('gamma', 'image', 130, 256, change_gamma) + cv2.createTrackbar('tonemap', 'image', 0, 3, change_tonemap) + + while(1): + k = cv2.waitKey(10) + if k==27: # Esc key to stop + break + elif k==32: # Space key to dump the image + dump() + elif k==-1: # normally -1 returned,so don't print it + continue + else: + #print k # else print its value + continue + + + #cv2.waitKey(0) + cv2.destroyAllWindows() \ No newline at end of file diff --git a/main.py b/main.py index 84bcc6c..91d1364 100644 --- a/main.py +++ b/main.py @@ -12,16 +12,19 @@ def main(config): num_channels = stack.shape[-1] hdr_img = np.zeros(stack[0].shape, dtype=np.float64) - for c in range(num_channels): - layer_stack = [img[:,:,c] for img in stack] - - sample = sample_intensity(layer_stack) + if config.hdr_radiance_path == '': + for c in range(num_channels): + layer_stack = [img[:,:,c] for img in stack] - curve = estimate_curve(sample, exps, 100.) + sample = sample_intensity(layer_stack) - img_rad = computeRadiance(np.array(layer_stack), exps, curve) + curve = estimate_curve(sample, exps, 100.) - hdr_img[:,:, c] = cv2.normalize(img_rad, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX) + img_rad = computeRadiance(np.array(layer_stack), exps, curve) + + hdr_img[:,:, c] = cv2.normalize(img_rad, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX) + else: + hdr_img = cv2.imread(config.hdr_radiance_path, -1) if config.tonemap =='gamma': output = np.uint8(globalTonemap(hdr_img, 1.3) * 255.) @@ -34,7 +37,9 @@ def main(config): colorize = cm.jet cmap = np.float32(cv2.cvtColor(np.uint8(hdr_img), cv2.COLOR_BGR2GRAY)/255.) cmap = colorize(cmap) + cv2.imwrite(os.path.join(config.test_path, 'cmap.hdr'), cmap[:,:,0:3]) cv2.imwrite(os.path.join(config.test_path, 'cmap.jpg'), np.uint8(cmap*255.)) + cv2.imwrite(os.path.join(config.test_path, 'hdr_radiance.tiff'), hdr_img) template = stack[len(stack)//2] image_tuned = intensityAdjustment(output, template) @@ -47,6 +52,7 @@ def main(config): parser = argparse.ArgumentParser() parser.add_argument('--test_path', type=str, default="imgs/night01/") + parser.add_argument('--hdr_radiance_path', type=str, default='') parser.add_argument('--tonemap', type=str, default=' ') parser.add_argument('--cmap', type=bool, default=False) diff --git a/utils.py b/utils.py index 31cdbf6..87f50c8 100644 --- a/utils.py +++ b/utils.py @@ -4,18 +4,21 @@ import random import numpy as np +#I_min, I_max = 0., 65535. +I_min, I_max = 0., 255. +#I_min, I_max = 0., 1023. +#I_min, I_max = 0., 4095. +n = int(I_max) def weight(I): - I_min, I_max = 0., 255. if I <= (I_min + I_max) /2: return I - I_min return I_max - I def sample_intensity(stack): - I_min, I_max = 0., 255. num_intensities = int(I_max - I_min + 1) num_images = len(stack) - sample = np.zeros((num_intensities, num_images), dtype=np.uint8) + sample = np.zeros((num_intensities, num_images), dtype=np.float16) mid_img = stack[num_images // 2] @@ -30,8 +33,6 @@ def sample_intensity(stack): def estimate_curve(sample, exps, l): - I_min, I_max = 0., 255. - n = 255 A = np.zeros((sample.shape[0] * sample.shape[1] + n, n + sample.shape[0] + 1), dtype=np.float64) b = np.zeros((A.shape[0], 1), dtype=np.float64) @@ -40,7 +41,7 @@ def estimate_curve(sample, exps, l): #1. data fitting for i in range(sample.shape[0]): for j in range(sample.shape[1]): - I_ij = sample[i,j] + I_ij = int(sample[i,j]) w_ij = weight(I_ij) A[k, I_ij] = w_ij A[k, n + 1 + i] = -w_ij @@ -115,6 +116,6 @@ def read(path_list): stack = np.zeros((len(path_list), shape[0], shape[1], shape[2])) for i in path_list: - im = cv2.imread(i) + im = cv2.imread(i, -1)[:,:,0:3] stack[path_list.index(i), :, :, :] = im return stack \ No newline at end of file