def generate_training_data(self, imageDirPathList): merge_mertens = cv2.createMergeMertens() for scene_path in imageDirPathList: img_path_list = glob.glob(scene_path + '/input*.ppm') img_path_list.sort() cnt = 0 temp_image_list = [] for image_path in img_path_list: im = cv2.imread(image_path, flags=cv2.IMREAD_ANYDEPTH) y_channel = self.__getYChannel(image_path) image_str = scene_path + '/exposure' + str(cnt) + '.png' # y_channel = cv2.resize(y_channel, (512, 512), interpolation=cv2.INTER_CUBIC) cv2.imwrite(image_str, y_channel) print(image_str + ' has been generated!') temp_image_list.append(im) cnt += 1 # rgb_gt = merge_mertens.process(temp_image_list) # rgb_gt*=255 # rgb_gt_path = scene_path + '/rgb_gt.png' # cv2.imwrite(rgb_gt_path, rgb_gt) gt = cv2.imread(scene_path + '/GT(clamp).hdr', flags=cv2.IMREAD_ANYDEPTH) tonemapDrago = cv2.createTonemapDrago(1.0, 0.7) ldrDrago = tonemapDrago.process(gt) ldrDrago = 3 * ldrDrago cv2.imwrite(scene_path + "/rgb_gt.png", ldrDrago * 255) y_channel_of_gt = self.__getYChannel(scene_path + '/rgb_gt.png') gt_path = scene_path + '/gt.png' # y_channel_of_gt = cv2.resize(y_channel_of_gt, (512, 512), interpolation=cv2.INTER_CUBIC) cv2.imwrite(gt_path, y_channel_of_gt) print(gt_path + ' has been generated!')
def photographic_tonemapping(image_name, key, sharp): sharp = float(sharp) key = float(key) hdr = cv2.imread(image_name, cv2.IMREAD_ANYDEPTH) print(hdr.dtype, hdr.shape) #for color image only if (hdr.dtype != np.float32) and (hdr.shape[2] == 3): print("image should have be in np.float32 format.\ And it should have three channels") exit(-1) #build a luminence image based on colored hdr lumi = make_luminence(hdr) lumi_original = copy.deepcopy(lumi) average, max_lumi = calculate_average_and_max(lumi) print(max_lumi) #global_operator(lumi,key,average,max_lumi) local_dodging_and_burning(lumi, key, average, max_lumi, sharp) ldr_name = image_name produce_new_image(lumi, lumi_original, hdr, image_name) tonemapDrago = cv2.createTonemapDrago(1.0, 0.7) ldrDrago = tonemapDrago.process(hdr) ldrDrago = 3 * ldrDrago cv2.imwrite(ldr_name[:-4] + "_cv2.jpg", ldrDrago * 255)
def display_hdr_image(hdr_image: np.ndarray): ''' Given HDR image, display by tonemapping Args: - hdr_image: HxWxC HDR float32 image ''' # copy data before display hdr_image = hdr_image.copy().astype(np.float32) # for nan values, set them to 0 nan_mask = hdr_image != hdr_image hdr_image[nan_mask] = 0 # set 0 values to min value that is over 0 if ((hdr_image <= 0).sum() > 0) or ((hdr_image == float('inf')).sum() > 0): print('Warning: Negative / Inf values found in hdr image. Clamping to nearest valid value') nearest_nonzero_value = hdr_image[hdr_image > 0].min() nearest_non_inf_value = hdr_image[hdr_image != float('inf')].max() hdr_image[hdr_image <= 0] = nearest_nonzero_value hdr_image[hdr_image == float('inf')] = nearest_non_inf_value tonemapper = cv2.createTonemapDrago(1.0, 0.7) tonemapped = tonemapper.process(hdr_image) fig = plt.figure() plt.axis('off') plt.imshow(tonemapped[:, :])
def tonemapping(hdr, tmo_func='reinhard', gamma=2.2, fstop=0): ## tone mapping hdr if tmo_func == 'reinhard': tmo = cv2.createTonemapReinhard(gamma=gamma) elif tmo_func == 'durand': tmo = cv2.createTonemapDurand(gamma=gamma) elif tmo_func == 'drago': tmo = cv2.createTonemapDrago(gamma=gamma) elif tmo_func == 'mantiuk': tmo = cv2.createTonemapMantiuk(gamma=gamma) elif tmo_func == 'linear': output = hdr - hdr.min() output = output / output.max() # return output return tonemapping(output, tmo_func='gamma') elif tmo_func == 'gamma': inv_gamma = 1.0 / gamma exposure = np.power(2., fstop) output = clamp_img(np.power(exposure * hdr, inv_gamma), 0, 1) return output else: raise NotImplementedError # elif tmo_func =='cut_high': # output = hdr - hdr.min() # output = output/output.max() # return output output = tmo.process(hdr.astype('float32')) return output
def tMO(file,name): #tonemapping the file try: if (name == 'reinhard'): print('Reinhard') intensity=-1.0 light_adapt=0.8 color_adapt=0.0 gamma=2.0 tmo = cv2.createTonemapReinhard(gamma=gamma, intensity=intensity, light_adapt=light_adapt, color_adapt=color_adapt) #([, gamma[, intensity[, light_adapt[, color_adapt]]]]) https://www.kite.com/python/docs/cv2.createTonemapReinhard# if (name == 'mantiuk'): print('Mantiuk') saturation=1.0 scale=0.75 gamma=2.0 tmo = cv2.createTonemapMantiuk(saturation=saturation, scale=scale, gamma=gamma) if (name == 'drago'): print('Drago') saturation=1.0 bias=0.85 gamma=2.0 tmo = cv2.createTonemapDrago(saturation=saturation, bias=bias, gamma=gamma) if (name == 'linear'): print('Linear') gamma=2.0 tmo = cv2.createTonemap(gamma=gamma) except: print('ToneMapping Error') ldr = tmo.process(file) return ldr
def __init__(self, saturation=1.0, bias=0.85, gamma=2.0, randomize=False): if randomize: gamma = uniform(1.8, 2.2) bias = uniform(0.7, 0.9) self.op = cv2.createTonemapDrago( saturation=saturation, bias=bias, gamma=gamma)
def process_photos(folders): psave = folders["psave"] ptmp = folders["ptmp"] pgal = folders["pgal"] foldername = folders["foldername"] save_folder = psave + "/" + foldername makedirs(save_folder) onlyfiles = [f for f in listdir(ptmp) if isfile(join(ptmp, f))] images = [] times = np.array([], dtype=np.float32) logging.info("Loading images for HDR") for filename in onlyfiles: filesrc = ptmp + "/" + filename filedest = save_folder + "/" + filename shutil.move(filesrc, filedest) file_data = open(filedest, 'rb') tags = exifread.process_file(file_data) exposure = float(tags['EXIF ExposureTime'].values[0]) im = cv2.imread(filedest) images.append(im) times = np.append(times, np.float32(exposure)) logging.info("Align input images") align_MTB = cv2.createAlignMTB() align_MTB.process(images, images) logging.info('Obtain Camera Response Function (CRF)') calibrate_debevec = cv2.createCalibrateDebevec() response_debevec = calibrate_debevec.process(images, times) logging.info('Merge images into an HDR linear image') merge_debevec = cv2.createMergeDebevec() hdr_debevec = merge_debevec.process(images, times, response_debevec) logging.info('Save HDR image') save_file = pgal + "/" + foldername cv2.imwrite(save_file + ".hdr", hdr_debevec) logging.info("Tonemaping using Drago's method ... ") tonemap_drago = cv2.createTonemapDrago(1.0, 0.7) ldr_drago = tonemap_drago.process(hdr_debevec) ldr_drago = 3 * ldr_drago cv2.imwrite(save_file + "_drago.jpg", ldr_drago * 255) logging.info("Tonemaping using Reinhard's method ... ") tonemap_reinhard = cv2.createTonemapReinhard(1.5, 0,0,0) ldr_reinhard = tonemap_reinhard.process(hdr_debevec) cv2.imwrite(save_file + "_reinhard.jpg", ldr_reinhard * 255) logging.info("Tonemaping using Mantiuk's method ... ") tonemap_mantiuk = cv2.createTonemapMantiuk(2.2,0.85, 1.2) ldr_mantiuk = tonemap_mantiuk.process(hdr_debevec) ldr_mantiuk = 3 * ldr_mantiuk cv2.imwrite(save_file + "_mantiuk.jpg", ldr_mantiuk * 255)
def test_tonemap(self): global hdrDebevec print("Tonemaping using Gamma and Drago's method ... ") tonemapGamma = cv2.createTonemap(3) tStart = time.time() ldrGamma = tonemapGamma.process(hdrDebevec) tEnd = time.time() tOril = tEnd - tStart tStart = time.time() _tonemap.process(hdrDebevec) tEnd = time.time() tOur = tEnd - tStart print("tOril:", tOril) print("tOur:", tOur) cv_file = cv2.FileStorage("TonemapGamma.ext", cv2.FILE_STORAGE_READ) ldrGamma_our = cv_file.getNode("result").mat() ldrGamma_our[np.isnan(ldrGamma_our)] = 0 self.assertEqual(np.allclose(ldrGamma_our, ldrGamma), True) tonemapDrago = cv2.createTonemapDrago(1.0, 0.7) tStart = time.time() ldrDrago = tonemapDrago.process(hdrDebevec) tEnd = time.time() tOril = tEnd - tStart tStart = time.time() _tonemap.processDrag(hdrDebevec) tEnd = time.time() tOur = tEnd - tStart print("tOril:", tOril) print("tOur:", tOur) cv_file = cv2.FileStorage("TonemapDrag.ext", cv2.FILE_STORAGE_READ) ldrDrag_our = cv_file.getNode("result").mat() ldrDrag_our[np.isnan(ldrDrag_our)] = 0 self.assertEqual(np.allclose(ldrDrag_our, ldrDrago, 1), True) hdrDebevec = 3 * hdrDebevec cv2.imwrite("hdrDebevec.jpg", hdrDebevec * 255) print("saved hdrDebevec.jpg") ldrGamma = 3 * ldrGamma cv2.imwrite("ldrGamma_cv.jpg", ldrGamma * 255) print("saved ldrGamma_cv.jpg") ldrGamma_our = 3 * ldrGamma_our cv2.imwrite("ldrGamma_our.jpg", ldrGamma_our * 255) print("saved ldrGamma_our.jpg") ldrDrag_our = 3 * ldrDrag_our cv2.imwrite("ldrDrag_our.jpg", ldrDrag_our * 255) print("saved ldrDrag_our.jpg") ldrDrago = 3 * ldrDrago cv2.imwrite("ldrDrago.jpg", ldrDrago * 255) print("saved ldrDrago.jpg") # vim: set fenc=utf8 ff=unix et sw=4 ts=4 sts=4:
def part_4(self, image): ''' Tonemapping of the HDR composite image Arguements: Image Return: Image ''' tonemap1 = cv2.createTonemapDrago(gamma=2.2) result = tonemap1.process(image) return (result)
def display_hdr_image(hdr_image: np.ndarray): ''' Given HDR image, display by tonemapping Args: - hdr_image: HxWxC HDR float32 image ''' tonemapper = cv2.createTonemapDrago(1.0, 0.7) tonemapped = tonemapper.process(hdr_image) fig = plt.figure() plt.axis('off') plt.imshow(tonemapped[:, :])
def tone_map(img, tmo_name): if (tmo_name == 'exposure'): tmo = Exposure(gamma=opt.gamma, stops=opt.stops) if (tmo_name == 'reinhard'): tmo = cv2.createTonemapReinhard(intensity=-1.0, light_adapt=0.8, color_adapt=0.0) elif tmo_name == 'mantiuk': tmo = cv2.createTonemapMantiuk(saturation=1.0, scale=0.75) elif tmo_name == 'drago': tmo = cv2.createTonemapDrago(saturation=1.0, bias=0.85) elif tmo_name == 'durand': tmo = cv2.createTonemapDurand(contrast=3, saturation=1.0, sigma_space=8, sigma_color=0.4) return tmo.process(img)
def tMO(file,name): #tonemapping the file try: if (name == 'reinhard'): tom = cv2.createTonemapReinhard() if (name == 'mantiuk'): tom = cv2.createTonemapMantiuk() if (name == 'drago'): tom = cv2.createTonemapDrago() if (name == 'linear'): tom = cv2.createTonemap() except: print('ToneMapping Error') ldr = tom.process(file) return ldr
def doneByOpenCV(): print('doing HDR and TM by openCV') images, shutters, max_shift = readInfo() images = readImg(images) calibrateDebevec = cv2.createCalibrateDebevec() responseDebevec = calibrateDebevec.process(images, shutters) mergeDebevec = cv2.createMergeDebevec() hdrDebevec = mergeDebevec.process(images, shutters, responseDebevec) print('HDR max:%.2f, min:%.2f' % (hdrDebevec.max(), hdrDebevec.min())) cv2.imwrite('%s/hdr_%d_OpenCV.hdr' % (RESULT_DIR, IMG_NUM), hdrDebevec) tonemapDrago = cv2.createTonemapDrago(1, 0.8) # hand set params ldrDrago = tonemapDrago.process(hdrDebevec) ldrDrago = 255 * ldrDrago * 1.5 # hand set params print('LDR max:%.2f, min:%.2f' % (ldrDrago.max(), ldrDrago.min())) cv2.imwrite('%s/ldr_%d_OpenCV.jpg' % (RESULT_DIR, IMG_NUM), ldrDrago)
def __init__(self, ip_pi): QThread.__init__(self) self.threadID = 1 self.name = "ImgThread" self.window = None self.saveOn = False self.mergeMertens = cv2.createMergeMertens( 0, 1, 1) #contrast saturation exposure # self.mergeMertens = cv2.createMergeMertens() # print("Contrast:",self.mergeMertens.getContrastWeight()) # print("Saturation:",self.mergeMertens.getSaturationWeight()) # print("Exposure:",self.mergeMertens.getExposureWeight()) self.mergeDebevec = cv2.createMergeDebevec() self.calibrateDebevec = cv2.createCalibrateDebevec() # self.toneMap = cv2.createTonemapReinhard(gamma=1.) self.toneMap = cv2.createTonemapDrago() # self.linearTonemap = cv2.createTonemap(1.) #Normalize with Gamma 1.2 # self.toneMap = cv2.createTonemapMantiuk() # self.claheProc = cv2.createCLAHE(clipLimit=1, tileGridSize=(8,8)) # self.simpleWB = cv2.xphoto.createSimpleWB() # self.simpleWB = cv2.xphoto.createGrayworldWB() # self.wb= False # self.equalize = False # self.clahe = False # self.clipLimit = 1. # self.alignMTB = cv2.createAlignMTB() self.invgamma = np.empty((1, 256), np.uint8) for i in range(256): self.invgamma[0, i] = np.clip(pow(i / 255.0, 0.45) * 255.0, 0, 255) self.gamma = np.empty((1, 256), np.uint8) for i in range(256): self.gamma[0, i] = np.clip(pow(i / 255.0, 2.2) * 255.0, 0, 255) self.reduceFactor = 1 self.ip_pi = ip_pi self.hflip = False self.vflip = False self.table = None self.doCalibrate = False try: npz = np.load("calibrate.npz") self.table = npz['table'] except Exception as e: pass
def drago_hdr(image_names, algo='debevec', exposures=None, gamma=1.0, saturation=1.0, bias=0.85, output=None): """ Create an HDR image from the supplied images. :param images: List of images to process. :return: Returns name of new HDR image. """ hdr_img = process_image(image_names, exposures, algo) tonemap_drago = cv2.createTonemapDrago(gamma, saturation, bias) ldr_drago = tonemap_drago.process(hdr_img) img_out = get_image_output(image_names[1], output) cv2.imwrite(img_out, ldr_drago * 255)
def main(): #读取多张曝光的图像 images,times = readImagesAndTimes() #对齐图像 alignMTB = cv2.createAlignMTB() alignMTB.process(images, images) #恢复相机响应函数 calibrateDebevec = cv2.createCalibrateDebevec() responseDebevec = calibrateDebevec.process(images, times) # 将多张图像融合成hdr mergeDebevec = cv2.createMergeDebevec() hdrDebevec = mergeDebevec.process(images, times, responseDebevec) # 保存融合结果,可用ps打开 cv2.imwrite("hdrDebevec.hdr", hdrDebevec) # Tonemap using Drago's method to obtain 24-bit color image tonemapDrago = cv2.createTonemapDrago(1.0, 0.7) ldrDrago = tonemapDrago.process(hdrDebevec) ldrDrago = 3 * ldrDrago cv2.imwrite("ldr-Drago.jpg", ldrDrago * 255) # Tonemap using Durand's method obtain 24-bit color image tonemapDurand = cv2.createTonemapDurand(1.5,4,1.0,1,1) ldrDurand = tonemapDurand.process(hdrDebevec) ldrDurand = 3 * ldrDurand cv2.imwrite("ldr-Durand.jpg", ldrDurand * 255) # Tonemap using Reinhard's method to obtain 24-bit color image tonemapReinhard = cv2.createTonemapReinhard(1.5, 0,0,0) ldrReinhard = tonemapReinhard.process(hdrDebevec) cv2.imwrite("ldr-Reinhard.jpg", ldrReinhard * 255) # Tonemap using Mantiuk's method to obtain 24-bit color image tonemapMantiuk = cv2.createTonemapMantiuk(2.2,0.85, 1.2) ldrMantiuk = tonemapMantiuk.process(hdrDebevec) ldrMantiuk = 3 * ldrMantiuk cv2.imwrite("ldr-Mantiuk.jpg", ldrMantiuk * 255)
def main(): images, times = readImagesAndTimes() # Align input images alignMTB = cv2.createAlignMTB() alignMTB.process(images, images) # Obtain Camera Response Function (CRF) calibrateDebevec = cv2.createCalibrateDebevec() responseDebevec = calibrateDebevec.process(images, times) # Merge images into an HDR linear image mergeDebevec = cv2.createMergeDebevec() hdrDebevec = mergeDebevec.process(images, times, responseDebevec) # Save HDR image. cv2.imwrite("hdrDebevec.hdr", hdrDebevec) # Tonemap using Drago's method to obtain 24-bit color image tonemapDrago = cv2.createTonemapDrago(1.0, 0.7) ldrDrago = tonemapDrago.process(hdrDebevec) ldrDrago = 3 * ldrDrago cv2.imwrite("ldr-Drago.jpg", ldrDrago * 255)
from DataPreprocess.ProcessEXR import * from DataPreprocess.WarpUtils import * import cv2 import imageio as im tonemap_drago = cv2.createTonemapDrago(2.2, 0.5) def new_exr2jpg(hdr_file_name, full_jpg_name): exr_data = exr2array(hdr_dataset_dir + hdr_file_name) im.imwrite(hdr_dataset_hdrformat_dir + hdr_file_name.replace(".exr", ".hdr"), exr_data, format='hdr') hdr_data = cv2.imread( hdr_dataset_hdrformat_dir + hdr_file_name.replace(".exr", ".hdr"), cv2.IMREAD_ANYDEPTH) ldrDurand = tonemap_drago.process(hdr_data) ldr_8bit = np.clip(ldrDurand * 255, 0, 255).astype('uint8') cv2.imwrite(full_jpg_name, ldr_8bit) if __name__ == '__main__': pfm_files = [ f for f in listdir(depth_files_dir) if isfile(join(depth_files_dir, f)) and f.endswith(".pfm") ] for file in pfm_files: print(file) new_exr2jpg(file.replace("-depth.pfm", ".exr"), fusion_hdr_jpgs_dir + file.replace("-depth.pfm", ".jpg"))
for i in range(1, 17): name = 'Processed Images/processed_exposure' + str(i) + '.tiff' img = np.float32(cv2.imread(name, -1)) / 65535 ## Normalizing the image img[img > 0.8] = 0 ## Valid Pixels img[img < 0.05] = 0 ## Valid Pixels w = (np.exp(-4 * ((img - 0.5)**2) / (0.5**2))) ## Weights w[w == np.exp(-4)] = 0 ## Weights for just valid pixels else = 0 n1 = (w * img / (2**(i - 12))) ## Img*weights n = n + n1 ## Weighted Sum d = d + w ## Sum of weights i_hdr = (n / d) ## HDR image with out tone map ## Photo tone map with different fraction of additions in denominator for i in range(1, 11): i_tm = np.uint16(i_hdr * 65535 / ((i * 0.1) + i_hdr)) cv2.imwrite('Output Images/HDR_phototonemap' + str(i) + '.png', i_tm) ############################################################################### ############# Inbuilt OpenCV tone mapping ##################################### ## Drago Tonemap ## tonemapDrago = cv2.createTonemapDrago(1.0, 0.7) ## Setting gamma and saturation ldrDrago = tonemapDrago.process(np.float32(i_hdr)) ldrDrago = 3 * ldrDrago ## factor of 3 is by trail and error cv2.imwrite('Output Images/HDR_Drago.png', ldrDrago * 255) ## Reinhard Tonemap ## ## parameters - Gamma,intensity,light adapt,color adapt tonemapReinhard = cv2.createTonemapReinhard(1.2, 0.5, 0, 0) ldrReinhard = tonemapReinhard.process(np.float32(i_hdr)) cv2.imwrite("Output Images/HDR_Reinhard1.png", ldrReinhard * 255)
def RAW2RGB( Iref, color_matrix=[], camera_wb=[], flag_list=[0, 1, 1, 1, 0, 3, 0, 1, 0, 1], gamma=1.5, wb_parameter=5, contrast_strength=20.0, tonemap_strength=1.5, ): # 1. black level # 2. white balance # 3. Demosaick # 4. sRGB color correction (sensor RGB to linear sRGB) # 5. Dynamic range commpression (local tone mapping) # 6. Global tone mapping (gamma correction : linear sRGB to non-linear sRGB) # 7. Chromatic aberration correction # 8. Global contrast increase # 9. Sharping # 10. Auto white balance if flag_list[1]: if camera_wb == []: camera_wb = [1.687527, 1.0, 2.082254] wb_mask = WB_Mask(Iref, 'RGGB', camera_wb[0], camera_wb[2]) Iref = Iref * wb_mask print("2 : white balance - wb:{}".format(camera_wb)) if flag_list[2]: Iref = Iref * (2**16) Iref = np.clip(Iref, 0.0, 2**16 - 1) Iref = Iref.astype(np.uint16) out = cv2.cvtColor(Iref, cv2.COLOR_BAYER_BG2RGB) out = out.astype(np.float32) out = out / (2**16 - 1) # out_min = out.min() # out_max = out.max() # out = ((out - out_min) / (out_max - out_min)) # out = out.astype(np.uint8) print("3 : demosaick") else: out = Iref if flag_list[3]: # color_matrix = dng.rgb_xyz_matrix[:, 0:3] if color_matrix == []: color_matrix = np.array([[0.24448879, 0.5810741, 0.17443706], [-0.00469436, 0.96864164, 0.03605274], [-0.00345951, -0.06517734, 1.0686369]], dtype=np.float32) out = hdrp_srgb(out, color_matrix) print("4 : sRGB color correction colormatrix:{}".format(color_matrix)) else: out = out.astype(np.float32) out = np.clip(out, 0.0, 1.0) # change the value to [0, 255] if flag_list[5] == 1: tonemap = cv2.createTonemapDrago(gamma=gamma) out = tonemap.process(out.astype(np.float32)) * 255.0 print("5 : tone mapping") elif flag_list[5] == 2: out = adjust_gamma(out, gamma) print("5 : tone mapping_2") elif flag_list[5] == 3: out = adjust_gamma(out, gamma) out = nonlinear_masking(out, strength_multiplier=tonemap_strength) print("5 : tone mapping_3") else: out *= 255.0 if flag_list[6] == 1: out = purple_fringe_removal(out) print("6 : Chromatic aberration correction") if flag_list[7] == 1: out = hdrp_contrast(out, strength=contrast_strength) print("7 : global contrast increase") if flag_list[8]: out = hdrp_sharpen(out, strength=2.5) print("8 : sharpen") if flag_list[9] == 1: wb = cv2.xphoto.createGrayworldWB() wb.setSaturationThreshold(wb_parameter) out = wb.balanceWhite(out.astype(np.uint8)).astype(np.float32) print("10 : auto white balance_grayworld") elif flag_list[9] == 2: wb = cv2.xphoto.createSimpleWB() wb.setP(wb_parameter) p = wb.getP() print('p:{}'.format(p)) out = wb.balanceWhite(out.astype(np.uint8)).astype(np.float32) # out = RGB_WB(out, camera_wb[0], camera_wb[2], camera_wb[1]) print("10 : auto white balance_SimpleWB") elif flag_list[9] == 2: wb = cv2.xphoto.LearningBasedWB() out = wb.balanceWhite(out.astype(np.uint8)).astype(np.float32) # out = RGB_WB(out, camera_wb[0], camera_wb[2], camera_wb[1]) print("10 : auto white balance_learning_based") # out = cv2.cvtColor(out, cv2.COLOR_RGB2BGR) # cv2.imwrite(outputname, out.astype(np.uint8)) return out
def hdr2ldr(hdr, filename): tonemap = cv2.createTonemapDrago(5) ldr = tonemap.process(hdr) cv2.imwrite('{}.png'.format(filename), ldr * 255)
i= i +1 with rawpy.imread(path) as raw: rgb = raw.postprocess(gamma=(1,1), no_auto_bright=True, output_bps=16) xnew,ynew=rgb.shape[1]/10,rgb.shape[0]/10 xnew = int(xnew) ynew = int(ynew) #print (xnew) rgb=cv2.resize(rgb,(xnew,ynew)) imageio.imsave('processed_exposure%d.tiff' % i,rgb) for k in range(1,17): ps.append('processed_exposure%d.tiff' % k ) img_list = [cv2.imread(fn) for fn in ps] ks = np.array(tk) ks = np.array(tk) print(type(ks)) alignMTB = cv2.createAlignMTB() alignMTB.process(img_list, img_list) merge_robertson = cv2.createMergeRobertson() hdr_robertson = merge_robertson.process(img_list, times=ks) #Tonemap HDR image tonemap2 = cv2.createTonemapDrago(1.2,0.7) res_robertson = tonemap2.process(hdr_robertson.copy()) # Convert datatype to 8-bit and save res_robertson_8bit = np.clip(res_robertson*255, 0, 255).astype('uint8') cv2.imwrite("ldr_robertson_BUILTIN.jpg", res_robertson_8bit) #cv2.imwrite("ldr_robertsontry.jpg", res_robertson) ###############################################
def tonemap(hdr, alg=1): tonemapDrago = cv2.createTonemapDrago(1.0, 0.7) ldrDrago = tonemapDrago.process(hdr) ldrDrago = 3 * ldrDrago cv2.imwrite("part4_plot/hdr%d.jpg" % alg, ldrDrago * 255)
# times = np.log(np.array([1, 1/1.6, 1/2.5, 1/4, 1/6, 1/10, 1/15, 1/30, 1/50, 1/80, 1/125, 1/200, 1/320, 1/500, 1/800, 1/1600,1/3200,1/4000], dtype=np.float32)) # times = np.log(np.array([1/1.6, 1/3, 1/5, 1/10, 1/15, 1/25, 1/50, 1/100, 1/200, 1/400, 1/800], dtype=np.float32)) # filenames = [("./road/DSC_"+str(i)+".JPG") for i in range(3767, 3777)] filenames = [("./sc/DSC_" + str(i) + ".JPG") for i in range(3750, 3763)] images = [] for filename in filenames: im = cv2.imread(filename) images.append(im) images = np.array(images) return images, times img_list, exp_times = readImagesAndTimes() response_list = [] radiance_list = [] result_img = HDR(img_list, exp_times, float(150)) plt.imsave('./hdr_sc.png', result_img) # tonemapping: # Tonemap using Drago's method to obtain 24-bit color image tonemapDrago = cv2.createTonemapDrago(0.787, 0.95) ldrDrago = tonemapDrago.process(result_img) ldrDrago = 3 * ldrDrago cv2.imwrite("./Drago_sc.png", ldrDrago * 255) # Tonemap using Reinhard's method to obtain 24-bit color image ''' tonemapReinhard = cv2.createTonemapReinhard(1.5, 0,0,0) ldrReinhard = tonemapReinhard.process(result_img) cv2.imwrite("./Reinhard_sc.jpg", ldrReinhard * 255) '''
# Obtain Camera Response Function (CRF) print("Calculating Camera Response Function (CRF) ... ") calibrateDebevec = cv2.createCalibrateDebevec() responseDebevec = calibrateDebevec.process(images, times) # Merge images into an HDR linear image print("Merging images into one HDR image ... ") mergeDebevec = cv2.createMergeDebevec() hdrDebevec = mergeDebevec.process(images, times, responseDebevec) # Save HDR image. cv2.imwrite("hdrDebevec.hdr", hdrDebevec) print("saved hdrDebevec.hdr ") # Tonemap using Drago's method to obtain 24-bit color image print("Tonemaping using Drago's method ... ") tonemapDrago = cv2.createTonemapDrago(1.0, 0.7) ldrDrago = tonemapDrago.process(hdrDebevec) ldrDrago = 3 * ldrDrago cv2.imwrite("ldr-Drago.jpg", ldrDrago * 255) print("saved ldr-Drago.jpg") # Tonemap using Durand's method obtain 24-bit color image print("Tonemaping using Durand's method ... ") tonemapDurand = cv2.createTonemapDurand(1.5,4,1.0,1,1) ldrDurand = tonemapDurand.process(hdrDebevec) ldrDurand = 3 * ldrDurand cv2.imwrite("ldr-Durand.jpg", ldrDurand * 255) print("saved ldr-Durand.jpg") # Tonemap using Reinhard's method to obtain 24-bit color image print("Tonemaping using Reinhard's method ... ")
def tone_mapping_Drago(): tonemapDrago = cv2.createTonemapDrago(1.0, 0.7) ldrDrago = tonemapDrago.process(hdrDebevec) ldrDrago = 3 * ldrDrago return ldrDrago * 255
def TonemapDrago(file_path): im = cv2.imread(file_path, cv2.IMREAD_ANYDEPTH) tonemapDrago = cv2.createTonemapDrago(1.0, 0.7) ldrDrago = tonemapDrago.process(im) return ldrDrago
def drago(x, saturation=1.0, gamma=2.2, bias=0.85): tmo = cv2.createTonemapDrago(gamma=gamma, saturation=saturation, bias=bias) return tmo.process(x)
# Obtain Camera Response Function (CRF) print("Calculating Camera Response Function (CRF) ... ") calibrateDebevec = cv2.createCalibrateDebevec() responseDebevec = calibrateDebevec.process(images, times) # Merge images into an HDR linear image print("Merging images into one HDR image ... ") mergeDebevec = cv2.createMergeDebevec() hdrDebevec = mergeDebevec.process(images, times, responseDebevec) # Save HDR image. cv2.imwrite("./images/HDR/hdrDebevec-example.hdr", hdrDebevec) print("saved hdrDebevec.hdr ") # Tonemap using Drago's method to obtain 24-bit color image print("Tonemaping using Drago's method ... ") tonemapDrago = cv2.createTonemapDrago(1.0, 0.7) ldrDrago = tonemapDrago.process(hdrDebevec) # The final output is multiplied by 3 just because it gave the most pleasing results. ldrDrago = 3 * ldrDrago cv2.imwrite("./images/HDR/ldr-Drago-example.jpg", ldrDrago * 255) cv2.imshow("ldr-Drago", ldrDrago) print("saved ldr-Drago.jpg") # Tonemap using Durand's method obtain 24-bit color image print("Tonemaping using Durand's method ... ") tonemapDurand = cv2.createTonemapDurand(1.5, 4, 1.0, 1, 1) ldrDurand = tonemapDurand.process(hdrDebevec) ldrDurand = 3 * ldrDurand cv2.imwrite("./images/HDR/ldr-Durand-example.jpg", ldrDurand * 255) cv2.imshow("ldrDurand", ldrDurand) print("saved ldr-Durand.jpg")
print("Calculating Camera Response Function (CRF) ... ") # calibrateDebevec = cv2.createCalibrateDebevec() calibrateDebevec = cv2.createCalibrateDebevec() responseDebevec = calibrateDebevec.process(images, times) # Merge images into an HDR linear image print("Merging images into one HDR image ... ") mergeDebevec = cv2.createMergeDebevec() hdrDebevec = mergeDebevec.process(images, times, responseDebevec) # Save HDR image. cv2.imwrite("hdrDebevec.hdr", hdrDebevec) print("saved hdrDebevec.hdr ") # Tonemap using Drago's method to obtain 24-bit color image print("Tonemaping using Drago's method ... ") tonemapDrago = cv2.createTonemapDrago(1.0, 3.3) ldrDrago = tonemapDrago.process(hdrDebevec) ldrDrago = 65 * ldrDrago cv2.imwrite("ldr-Drago.jpg", ldrDrago * 255) print("saved ldr-Drago.jpg") # Tonemap using Durand's method obtain 24-bit color image print("Tonemaping using Durand's method ... ") #tonemapDurand = cv2.createTonemapDurand(1.5, 4, 1.0, 1, 1) tonemapDurand = cv2.createTonemapDurand(4, 10, 1.0, 1, 1) ldrDurand = tonemapDurand.process(hdrDebevec) ldrDurand = 3 * ldrDurand cv2.imwrite("ldr-Durand.jpg", ldrDurand * 255) print("saved ldr-Durand.jpg") # Tonemap using Reinhard's method to obtain 24-bit color image