def create_hdr(directory): print("creating HDR image for " + directory + '...') imgs, speeds = read_images(directory) # align images aligner = cv2.createAlignMTB(max_bits=2, cut=True) aligner.process(imgs, imgs) result = np.zeros_like(imgs[0], dtype=np.float32) for c in range(3): # sample points samples = sample_points(imgs[:, :, c], num_locations=500) # solve for log exposure curve g g, _ = solve_log_exposure(samples, speeds, lmbda=50) result[:, :, c] = combine_images(imgs[:, :, :, c], speeds, g) plt.imsave( os.path.join('./output', os.path.split(directory)[1] + '_radiance.png'), rgb2gray(result)) # tone mapping method = 'bilateral' result = tone_mapping(result, method=method) plt.imsave( os.path.join('./output', os.path.split(directory)[1] + '_' + method + '.png'), result)
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 main(folder): """ Reads and process the images given by the read_and_store_data function and creates a new hdr file """ images, times = read_and_store_data(folder) """ Align the pictures using the brightest spots, it doesn't matter the exposures are different """ logging.debug("Aligning pictures") alignMTB = cv2.createAlignMTB() alignMTB.process(images, images) """ Finds the camera response function """ logging.debug("Calculating Camera response function") calibrate = cv2.createCalibrateDebevec() response = calibrate.process(images, times) """ Putting everything together into a single hdr image """ logging.debug("Merging images...") merge = cv2.createMergeDebevec() hdr = merge.process(images, times, response) """ Saving the hdr file into the specified folder """ hdr_folder = os.path.join(cwd, "HDR") logging.debug("Saving HDR at {}....".format(hdr_folder)) cv2.imwrite(hdr_folder + "/file2.hdr", hdr) logging.debug("Saved file")
def align_images(): """ reason of doing this :return: """ alignMTB = cv2.createAlignMTB() alignMTB.process(images, images)
def exposure_fusion(exposures): ##Mertens## # Align input images alignMTB = cv2.createAlignMTB() alignMTB.process(exposures, exposures) merge_mertens = cv2.createMergeMertens() res_mertens = merge_mertens.process(exposures) res_mertens_8bit = np.clip(res_mertens * 255, 0, 255).astype('uint8') return res_mertens_8bit
def align_images(images): """ Converts all the images to median threshold bitmaps (MTB), the MTBs can be aligned without requiring us to specify the exposure time. :return: """ alignMTB = cv2.createAlignMTB() alignMTB.process(images, images) return images
def merge2HDR(self, input_path, output_path, exp_times, verbosity): """ *************************************************************** Function to merge Debayered images into HDR images :param input_path: the debayered images path :param output_path: the output HDR path :param exp_times: the input exposure times in seconds :param verbosity: show individual file progress :return: <none> NOTE: Function uses Debevec's merging algorithm to merge exposures to HDR file. *************************************************************** """ # global starting number of the HDR frames hdrnum = 0 # list all files in the debayer folder filelist = [ filename for dirpath, dirnames, filenames in os.walk(input_path) for filename in filenames if filename.endswith('.jpg') ] # check whether directory exists else create a directory if not os.path.exists(output_path): os.makedirs(output_path) # process debayer images to HDR images for i in range(0, len(filelist), len(exp_times)): exposures = [] # read the exposures and append a list of exposures to be merged to HDR for j in range(i, i + 4): filename = os.path.join(input_path, filelist[j]) ldr_image = cv2.imread(filename, cv2.IMREAD_COLOR) ldr_image = cv2.cvtColor(ldr_image, cv2.COLOR_BGR2RGB) exposures.append(ldr_image) # align the exposure list alignMTB = cv2.createAlignMTB() alignMTB.process(exposures, exposures) # obtain camera response function calibrateDebevec = cv2.createCalibrateDebevec() responseDebevec = calibrateDebevec.process(exposures, exp_times) # create HDR from camera response mergeDebevec = cv2.createMergeDebevec() hdr = mergeDebevec.process(exposures, exp_times, responseDebevec) # set output file name and write exr (we use a separate exr because OpenCV EXR is not compressed) outfilename = os.path.join(output_path, '{0:05d}.exr'.format(hdrnum)) self.writeEXR(hdr, outfilename) if verbosity == 1: print('HDR file: {0} merged..'.format(outfilename)) hdrnum += 1 return
def merge_image(images): #images=[cv2.imread(image) for image in images] alignmtb = cv2.createAlignMTB() alignmtb.process(images, images) mm = cv2.createMergeMertens() result_mertens = mm.process(images) res_mertens_8bit = np.clip(result_mertens * 255, 0, 255).astype('uint8') img_out = res_mertens_8bit return img_out
def combine(img_stack): alignMTB = cv2.createAlignMTB() alignMTB.process(img_stack, img_stack) #exposure_times = np.array([1/100, 1/160, 1/320, 1/500,1/800,1/1600], dtype=np.float32) #do HDR calculation # Merge exposures to HDR image merge_mertens = cv2.createMergeMertens() res_mertens = merge_mertens.process(img_stack) res_mertens_8bit = np.clip(res_mertens * 255, 0, 255).astype('uint8') ################################################################ #imgBGR = cv2.cvtColor(res_mertens_8bit,cv2.COLOR_RGB2BGR) return res_mertens_8bit
def fusion_Image(dir): images = read_image(dir) alignMTB = cv2.createAlignMTB() alignMTB.process(images, images) mergeMertens = cv2.createMergeMertens() exposureFusion = mergeMertens.process(images) print(type(exposureFusion)) cv2.imshow('Fusion', exposureFusion) while True: k = cv2.waitKey(33) if k == 27: break cv2.destroyAllWindows()
def expFusion(imList): """ Computes the exposure fusion of a list of images with identical sizes. Cf. Exposure Fusion: A Simple and Practical Alternative to High Dynamic Range Photography. Tom Mertens, Jan Kautz and Frank Van Reeth In Computer Graphics Forum, 28 (1) 161 - 171, 2009 @param imList: @type imList: list of ndarray @return: @rtype: ndarray """ alignMTB = cv2.createAlignMTB() alignMTB.process(imList, imList) mergeMertens = cv2.createMergeMertens() fusion = mergeMertens.process(imList) np.clip(fusion, 0.0, 1.0, out=fusion) return fusion * 255
def load_images(path, gray=True, align=False): imgs = [] # Load images from the path with different focus valid_images = [".jpg", ".png", ".gif"] for f in os.listdir(path): ext = os.path.splitext(f)[1] if ext.lower() not in valid_images: continue if ext.lower() == ".gif": gif = Image.open((os.path.join(path, f))) imgs.append(np.array(gif.convert('RGB'))) else: imgs.append(cv2.imread(os.path.join(path, f))) if align: alignMTB = cv2.createAlignMTB() alignMTB.process(imgs, imgs) images = tuple(imgs) if gray: images = [cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) for img in images] return images
def createHDR(images, timeDifference): times = np.array([1.0, timeDifference], dtype=np.float32) #try: alignMTB = cv2.createAlignMTB() alignMTB.process(images, images) mergeMertens = cv2.createMergeMertens(10, 4, 8) hdrs = mergeMertens.process(images) #calibrateDebevec = cv2.createCalibrateDebevec() #responseDebevec = calibrateDebevec.process(images, times) #mergeDebevec = cv2.createMergeDebevec() #hdrDebevec = mergeDebevec.process(images, times, responseDebevec) #tonemapReinhard = cv2.createTonemapReinhard(1.5, 2.0,0,0) #ldrReinhard = tonemapReinhard.process(hdrDebevec) #return ldrReinhard return hdrs
def test_align(self): global images images_cv = images.copy() images_our = images.copy() print("Aligning images ... ") alignMTB = cv2.createAlignMTB() tStart = time.time() alignMTB.process(images, images_cv) tEnd = time.time() tOril = tEnd - tStart tStart = time.time() images_our = _align.process(images) tEnd = time.time() tOur = tEnd - tStart print("tOril:", tOril) print("tOur:", tOur) for i in range(len(images)): self.assertEqual(np.array_equal(images_cv[i], images_our[i]), True) self.assertEqual(images_cv[i].shape, images_our[i].shape) images = images_our
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)
def readImages(directory): images_name = [] images = [] images_r, images_g, images_b = [], [], [] images_rgb = [images_r, images_g, images_b] for filename in os.listdir(directory): images_name.append(directory + '/' + filename) images_name = sorted(images_name) print(images_name) for i in images_name: images.append(cv2.imread(i)) alignMTB = cv2.createAlignMTB() alignMTB.process(images, images) for img in images: img = cv2.resize(img, (0, 0), fx=1.0, fy=1.0) b, g, r = cv2.split(img) images_r.append(np.array(r)) images_g.append(np.array(b)) images_b.append(np.array(g)) # cv2.imwrite("img.png", cv2.merge((b,g,r))) # images_rgb.append(cv2.cvtColor(images[-1], cv2.COLOR_BGR2RGB)) images_r, images_g, images_b = np.array(images_r), np.array( images_g), np.array(images_b) return images, images_rgb
def process(image_list, exposure_times, mode): if(mode=='MTB'): # MTB alignMTB = cv.createAlignMTB() alignMTB.process(image_list, image_list) return image_list, exposure_times elif(mode=='ORB'): print("Work mode: ORB") refImgIndex = chooseRef.getRefImage_br(image_list) cv.imwrite("ref.jpg", image_list[refImgIndex]) return ORBAlignment(image_list,refImgIndex,exposure_times) elif(mode=='grad'): # todo return image_list, exposure_times elif(mode=='ECC'): print("Work mode: ECC") refImgIndex= chooseRef.getRefImage_br(image_list) cv.imwrite("ref.jpg", image_list[refImgIndex]) return ECCAlignment(image_list,refImgIndex), exposure_times elif(mode=='AKAZE'): print("Work mode: AKAZE") # refImgIndex = chooseRef.getRefImage(image_list) refImgIndex = chooseRef.getRefImage_br(image_list) cv.imwrite("ref.jpg", image_list[refImgIndex]) res_img_list = [] res_exp_list = [] idx_list, res_img_list = AKAZEAlignment(image_list,refImgIndex) for idx in idx_list: # res_img_list.append(image_list[idx]) res_exp_list.append(exposure_times[idx]) return res_img_list, res_exp_list elif(mode=='SIFT'): print("Work mode: SIFT") return SIFTProcess(image_list, exposure_times) else: return image_list, exposure_times
def createHDR(self): """ Numpy array of exposure times from exif data """ for image in self.image_paths: p = Photo(image) self.exposure_times.append(p.exifData()) times_np = np.asarray(self.exposure_times, dtype=np.float32) # Align Images alignMTB = cv.createAlignMTB() alignMTB.process(self.im_list, self.im_list) # Find Camera Response Curve calibrateDebevec = cv.createCalibrateDebevec() responseDebevec = calibrateDebevec.process(self.im_list, times_np) # Merge Images mergeDebevec = cv.createMergeDebevec() hdrDebevec = mergeDebevec.process(self.im_list, times_np, responseDebevec) # Generate HDR image and LDR tone mapped preview cv.imwrite("hdr.hdr", hdrDebevec) toneMapReinhard = cv.createTonemapReinhard(1.5, 0.0) ldrReinhard = toneMapReinhard.process(hdrDebevec) cv.imwrite("hdr_preview.jpg", ldrReinhard * 255)
def align_images(images): align_mtb = cv2.createAlignMTB() align_mtb.process(images, images)
def alignImages(images): alignMTB = cv.createAlignMTB() alignMTB.process(images, images) return alignMTB
if len(sys.argv) > 1: # Read images from the command line images = [] for filename in sys.argv[1:]: im = cv2.imread(filename) images.append(im) needsAlignment = False else : # Read example images images = readImagesAndTimes() needsAlignment = False # Align input images if needsAlignment: print("Aligning images ... ") alignMTB = cv2.createAlignMTB() alignMTB.process(images, images) else : print("Skipping alignment ... ") # Merge using Exposure Fusion print("Merging using Exposure Fusion ... "); mergeMertens = cv2.createMergeMertens() exposureFusion = mergeMertens.process(images) # Save output image print("Saving output ... exposure-fusion.jpg") cv2.imwrite("exposure-fusion.jpg", exposureFusion * 255)
def main(image_files, output_folder, exposure_times, resize=False, align=False, rad_plot=False, histo_plot=False): """ Generate a basic HDR image and rad_map plot from the images in the source folder, - and - Generate histograms from basic and histogram-equalized (histEQ) HDR images. """ # Print the information associated with each image -- use this # to verify that the correct exposure time is associated with each # image, or else you will get very poor results print("{:^30} {:>15}".format("Filename", "Exposure Time")) print("\n".join([ "{:>30} {:^15.4f}".format(*v) for v in zip(image_files, exposure_times) ])) img_stack = [ cv2.imread(name) for name in image_files if path.splitext(name)[-1][1:].lower() in EXTENSIONS ] if any([im is None for im in img_stack]): raise RuntimeError("One or more input files failed to load.") # RESIZE: Subsampling the images can reduce runtime for large files, # set resize to True in main call at end of this file. if resize: img_stack = [img[::4, ::4] for img in img_stack] print('images resized, new shape =', img_stack[0].shape) else: print('images full size, shape =', img_stack[0].shape) # ALIGN: Corrects small misalignments in images. If your images need more # intensive alignment, do additional processing on your own. # Set align to True in main call at end of this file. if align: alignMTB = cv2.createAlignMTB() alignMTB.process(img_stack, img_stack) print('images aligned') else: print('image alignment disabled') # Compute HDR image and make rad_plot log_exposure_times = np.log(exposure_times) basicHDR = computeHDR(img_stack, log_exposure_times, output_folder, rad_plot=rad_plot) cv2.imwrite(path.join(output_folder, "basicHDR.png"), basicHDR) print("Basic HDR image complete") # compute histogram equalized HDR image and make histogram plots histEQ = computeHistogramEQ(basicHDR.astype(np.uint8), output_folder, histo_plot=histo_plot) cv2.imwrite(path.join(output_folder, "histEQ.png"), histEQ) print("histogram EQ image complete") # compute best HDR bestHDR_image = hdr.bestHDR(basicHDR) cv2.imwrite(path.join(output_folder, "bestHDR.png"), bestHDR_image) histogram = hdr.computeHistogram(bestHDR_image.astype(np.uint8)) cdf = hdr.computeCumulativeDensity(histogram) plotHistogramCDF(histogram, cdf, output_folder, plot_filename="histCDF_bestHDR") print("best HDR image complete") return
def align(images): print("Aligning Source Images...") align = cv2.createAlignMTB() align.process(images, images)
plt.show() def contrast(L): Lmean = np.mean(L) first = 1 * np.sum((L - Lmean)) # насыщенность def saturation(img): satIm = cv.cvtColor(img, cv.COLOR_BGR2HSV) saturation = np.mean(satIm[:, :, 1]) return saturation makeHDR = 1 if makeHDR == 1: args = input("Name of dir with SDR images: ") images, times = loadExposureSeq(args) alignMTB = cv.createAlignMTB(6, 4, True) alignMTB.process(images, images[1]) calibrate = cv.createCalibrateDebevec() response = calibrate.process(images, times) # crf(response) merge_debevec = cv.createMergeDebevec() hdr = merge_debevec.process(images, times, response) cv.imwrite(input("Save HDR file as: "), hdr) else: filename = input("Name of HDR file: ") hdr = cv.imread(filename, cv.IMREAD_UNCHANGED)
import sys import numpy as np import cv2 if __name__ == '__main__': if len(sys.argv) < 2: print ("[Usage] python script <num of img> <dir of img>") sys.exit(0) num = int(sys.argv[1]) dir = sys.argv[2] img = [] for k in range(num): filename = dir + "/%02d.png" % k img.append(cv2.imread(filename, cv2.IMREAD_UNCHANGED)) lst = [32, 16, 8, 4, 2, 1, 0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625, 0.007125, 0.00390625, 0.001953125, 0.00097656525] exp = np.array(lst[0:num], dtype=np.float32) Align = cv2.createAlignMTB() Align.process(img, img) ResponseCurve = cv2.createCalibrateDebevec() cur = ResponseCurve.process(img, exp) RadianceMap = cv2.createMergeDebevec() hdr = RadianceMap.process(img, exp, cur) cv2.imwrite("result.hdr", hdr)
for filename in filenames: im = cv2.imread(filename) images.append(im) return images, times if __name__ == '__main__': # Read images and exposure times print("Reading images ... ") images, times = readImagesAndTimes() # Align input images print("Aligning images ... ") alignMTB = cv2.createAlignMTB() alignMTB.process(images, images) # 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 ")
def enhance(image): images, times = readImagesAndTimes(image) alignMTB = cv2.createAlignMTB() mergeDebevec = cv2.createMergeDebevec() hdrDebevec = mergeDebevec.process(images, times, responseDebevec) return hdrDebevec