def compose(imInput, object_filename, output_filename, global_modifier_factor=1): # read exr imRender = hdrio.imread(object_filename).astype('float32') imInputResized = zoom(imInput, (imRender.shape[0] / imInput.shape[0], imRender.shape[1] / imInput.shape[1], 1.0), order=2) imInputResized = imInputResized**2.2 # undo gamma on crop alphaRender = imRender[..., 3:4] imRender = (imRender[..., :3] * global_modifier_factor)**(1 / 2.2) imCompose = (1 - alphaRender) * imInputResized + alphaRender * imRender imCompose = imCompose**(1 / 2.2) # apply gamma on compose imComposeUint8 = (np.clip(imCompose, 0.0, 1.0) * 255).astype('uint8') imageio.imsave(output_filename, imComposeUint8) print("\tCompositing done...")
def process(im_path): im_filename = "{}-{}.jpg".format(os.path.basename(im_path), 0) subdir = im_filename.replace('pano_a', '')[:2] os.makedirs(os.path.join(output_dir, subdir), exist_ok=True) out_path = os.path.join(output_dir, subdir, im_filename) metadata_path = os.path.join( output_dir, subdir, '{}-meta.json'.format(os.path.basename(im_path))) if os.path.isfile(metadata_path): return im = imread(im_path, 'native') if len(im.shape) == 2: im = np.stack((im, ) * 3, axis=-1) else: im = im[:, :, :3] data = [] for random_id in range(7): datum = makeAndSaveImg(im_path, im, random_id) json_path = os.path.splitext(out_path)[0][:-2] + "-%d.json" % random_id with open(json_path, 'w') as fhdl_datum: json.dump(datum, fhdl_datum) data.append(datum) with open(metadata_path, 'w') as fhdl: json.dump(data, fhdl)
def generateLDRfromHDR(im_path, out_prefix): """Convert an HDR image into a clipped 0-255 value ("simulating" a camera)""" print('Processing: ', im_path) im = imread(im_path) h, w, c = im.shape im = im[:, w/2 - h/2:w/2 + h/2] envmap = EnvironmentMap(im, 'SkyAngular').convertTo('LatLong', TARGET_SIZE[0]) im = envmap.data valid = (im > 0) & (~np.isnan(im)) im_median = np.median(im[valid]) im_low = np.percentile(im[valid], 3) im_high = np.percentile(im[valid], 95) #scales = (TARGET_SIZE[0]/im.shape[0], TARGET_SIZE[1]/im.shape[1]) #im = zoom(im, [scales[0], scales[1], 1]) with open(out_prefix + "_hdr.pkl", 'wb') as fhdl: pickle.dump(im, fhdl, pickle.HIGHEST_PROTOCOL) imsave(out_prefix + '_hdr.exr', im) # 20th percentile -> value 5 # 80th percentile -> value 250 #print("Ratio:", (im_high - im_low)) ratio = im_high - im_low if ratio < 0.1: ratio = 0.1 im_ldr = (im - im_low) * 250. / ratio + 5 im_ldr = np.clip(im_ldr, 0, 255).astype('uint8') imsave(out_prefix + '_ldr.jpg', im_ldr) plt.figure() plt.subplot(1,2,1); plt.hist(im.ravel()[im.ravel()<im_high], 50) plt.subplot(1,2,2); plt.hist(im_ldr.ravel()[im_ldr.ravel()>0], 50) plt.savefig(out_prefix + 'debug.png') plt.close()
def __init__(self, im, format_): """ Creates an EnvironmentMap. :param im: Image to be converted to an EnvironmentMap :param format_: EnvironmentMap format. Can be `Angular`, ... :type im: float, numpy array :type format_: string """ assert format_.lower() in SUPPORTED_FORMATS, ( "Unknown format: {}".format(format_)) self.format_ = format_.lower() self.backgroundColor = np.array([0, 0, 0]) if isinstance(im, str): # We received the filename self.data = imread(im) elif isinstance(im, int): # We received a single scalar if self.format_ == 'latlong': self.data = np.zeros((im, im*2)) elif self.format_ == 'cube': self.data = np.zeros((im, round(3/4*im))) else: self.data = np.zeros((im, im)) elif type(im).__module__ == np.__name__: # We received a numpy array self.data = np.asarray(im, dtype='double') else: raise Exception('Could not understand input. Please prove a ' 'filename, a size or an image.') # Ensure size is valid if self.format_ in ['sphere', 'angular', 'skysphere', 'skyangular']: assert self.data.shape[0] == self.data.shape[1], ( "Sphere/Angular formats must have the same width/height")
import sys import json import numpy as np import scipy.ndimage from hdrio import imread # Usage : gethdr.py source dest image = imread(sys.argv[1]) imresized = scipy.ndimage.interpolation.zoom( image, (580. / image.shape[0], 580 / image.shape[1], 1), order=1) alpha = np.zeros(imresized.shape[0:2]) + 255. imresizeda = np.dstack((imresized, alpha)) out = imresizeda with open(sys.argv[2], 'w') as fhdl: fhdl.write("var targetData = ") json.dump(out.flatten().tolist(), fhdl) fhdl.write(";")
def __init__(self, im, format_=None, copy=True, color=True): """ Creates an EnvironmentMap. :param im: Image path or data to be converted to an EnvironmentMap, or the height of an empty EnvironmentMap. :param format_: EnvironmentMap format. Can be `Angular`, ... :param copy: When a numpy array is given, should it be copied. :param color: When providing an integer, create an empty color or grayscale EnvironmentMap. :type im: float, numpy array :type format_: string :type copy: bool """ if not format_ and isinstance(im, str): filename = os.path.splitext(im)[0] metadata = EnvmapXMLParser("{}.meta.xml".format(filename)) format_ = metadata.getFormat() assert format_ is not None, ( "Please provide format (metadata file not found).") assert format_.lower() in SUPPORTED_FORMATS, ( "Unknown format: {}".format(format_)) self.format_ = format_.lower() self.backgroundColor = np.array([0, 0, 0]) if isinstance(im, str): # We received the filename self.data = imread(im) elif isinstance(im, int): # We received a single scalar if self.format_ == 'latlong': if color: self.data = np.zeros((im, im * 2, 3)) else: self.data = np.zeros((im, im * 2)) elif self.format_ == 'skylatlong': if color: self.data = np.zeros((im, im * 4, 3)) else: self.data = np.zeros((im, im * 4)) elif self.format_ == 'cube': if color: self.data = np.zeros((im, round(3 / 4 * im), 3)) else: self.data = np.zeros((im, round(3 / 4 * im))) else: if color: self.data = np.zeros((im, im, 3)) else: self.data = np.zeros((im, im)) elif type(im).__module__ == np.__name__: # We received a numpy array self.data = np.asarray(im, dtype='double') if copy: self.data = self.data.copy() else: raise Exception('Could not understand input. Please provide a ' 'filename, a single size (height) or an image.') # Ensure size is valid if self.format_ in ['sphere', 'angular', 'skysphere', 'skyangular']: assert self.data.shape[0] == self.data.shape[1], ( "Sphere/Angular formats must have the same width/height") elif self.format_ == 'latlong': assert 2 * self.data.shape[0] == self.data.shape[1], ( "LatLong format width should be twice the height") elif self.format_ == 'skylatlong': assert 4 * self.data.shape[0] == self.data.shape[1], ( "SkyLatLong format width should be four times the height")
xyz = np.array(xyzp) xyzn = normalize(xyz) # get correspondency between 3d and pixel colors uv_S = np.array(world2latlong(xyzn[:, 0], xyzn[:, 1], xyzn[:, 2])) assert not np.isnan(np.sum(uv_S)) # create xml with area light sources gen_mitsuba_xml(xyz, np.array([0, 0, 0]), uv_S, texture, 'area_light_scene.xml') command = 'mitsuba -o non_parametric.exr area_light_scene.xml' print(command) os.system(command) warped_texture = hdrio.imread('non_parametric.exr') # for j in range(3): # warped_texture[:,:,j]/=(warped_texture[:,:,j].max()/ambients[j]) warped_texture[:, :, 0:3] = warped_texture[:, :, 0:3]**2 / 2 # combine them final_prediction = warped_texture[:, :, 0:3] + iblN # final_prediction = iblN hdrio.imwrite(final_prediction, "combined_envmap.exr") # render command = 'mitsuba -o final_render.exr ' + ' -Dobjx=' + str( obj_pos[0]) + ' -Dobjy=' + str(obj_pos[1]) + ' -Dobjz=' + str( obj_pos[2]) + ' -Denvmap=combined_envmap.exr render_scene.xml' print(command) os.system(command)
ctr = 0.5 - 0.5 * np.tan(pitch) / np.tan(vfov / 2) return ctr if __name__ == '__main__': from hdrio import imread, imsave import matplotlib matplotlib.use('qt5agg') from matplotlib import pyplot as plt import dataset fn, fp, data = dataset.getParameters() valid_idx = dataset.getValidIdx(data) fn = fn[valid_idx] fp = fp[valid_idx] data = data[valid_idx, :] pitch = data[:, 0] roll = data[:, 2] vfov = data[:, 3] for i in range(100): print(i) print(fp[i], pitch[i], roll[i], vfov[i]) im = imread(fp[i], 'native')[:, :, :3] imout = showHorizonLine(im, vfov[i], pitch[i], roll[i], debug=True) imsave('{0:03d}.png'.format(i), imout) #plt.clf() #plt.imshow(imout) #plt.show(block=False) #plt.pause(1)
import sys import json import numpy as np import scipy.ndimage from hdrio import imread # Usage : gethdr.py source dest image = imread(sys.argv[1]) imresized = scipy.ndimage.interpolation.zoom(image, (580./image.shape[0], 580/image.shape[1], 1), order=1) alpha = np.zeros(imresized.shape[0:2]) + 255. imresizeda = np.dstack((imresized, alpha)) out = imresizeda with open(sys.argv[2], 'w') as fhdl: fhdl.write("var targetData = ") json.dump(out.flatten().tolist(), fhdl) fhdl.write(";")