def load_depth(filePath): ''' Loads an depth OpenEXR image.''' if oe.isOpenExrFile(filePath) is not True: raise Exception("File ", filePath, " does not exist!") try: ifi = oe.InputFile(filePath) # Compute size header = ifi.header() dw = header["dataWindow"] w, h = dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1 # Read the three channels ifiType = header["channels"]["Z"].type.v if ifiType is not im.PixelType.FLOAT: raise Exception("Only float32 supported! (file is type ", ifiType) Z = ifi.channel("Z", im.PixelType(ifiType)) ifi.close() image = np.zeros((h, w), dtype = np.float32) # order = "C" image[:, :] = np.core.multiarray.fromstring(Z, dtype = np.float32).reshape(h, w) except: raise return image
def matte_pass_channel_cleanup(in_path, out_path): """ @params: in_path: Path to exr file. out_path: Path to write out the new exr file. """ if os.path.exists(in_path) and OpenEXR.isOpenExrFile(in_path): channels_to_merge = defaultdict(list) base_channels = [] # Read the file and header handle = OpenEXR.InputFile(in_path) header = handle.header() # Get the channels channels = header['channels'] header['channels'] = {} new_channels = {} for channel_name, channel_data in channels.iteritems(): match = CHANNEL_MATCH.match(channel_name) if match: layer, channel = match.groups() channels_to_merge[layer].append(channel) elif not ALPHA_MATCH.match(channel_name): base_channels.append(channel_name) for layer in base_channels: all_pixels = array.array( 'f', handle.channel(layer, Imath.PixelType(Imath.PixelType.HALF))).tolist() new_channels[layer] = array.array('f', all_pixels).tostring() for layer, data in channels_to_merge.iteritems(): new_pixels = [] try: new_layer_name = layer.split('_')[1] except: new_layer_name = layer # new pixels new_pixels = array.array('f', handle.channel('{0}.{1}'.format(layer, data[0]))) new_channels['matte.{0}'.format(new_layer_name)] = array.array('f', new_pixels).tostring() # write out new exr header['channels'] = dict([(c, HALF) for c in new_channels.keys()]) out = OpenEXR.OutputFile(out_path, header) out.writePixels(new_channels)
def _load_exr(ctx, filename): f = OpenEXR.InputFile(filename) dw = f.header()['dataWindow'] shape = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1) sz = shape[0] * shape[1] buf = np.empty(4 * sz, np.float32) buf[0::4] = _read_channel(f, 'R') buf[1::4] = _read_channel(f, 'G') buf[2::4] = _read_channel(f, 'B') buf[3::4] = np.ones(sz, np.float32) return cl.Image(ctx, cl.mem_flags.READ_ONLY | cl.mem_flags.COPY_HOST_PTR, cl.ImageFormat(cl.channel_order.RGBA, cl.channel_type.FLOAT), shape=shape, hostbuf=buf)
def get_image(image): latlong = OpenEXR.InputFile(image) dw = latlong.header()['dataWindow'] width = dw.max.x - dw.min.x + 1 height = dw.max.y - dw.min.y + 1 (r_str, g_str, b_str) = latlong.channels("RGB", pt) red = numpy.fromstring(r_str, dtype=numpy.float32) red.shape = (height, width) # (row, col) green = numpy.fromstring(g_str, dtype=numpy.float32) green.shape = (height, width) # (row, col) blue = numpy.fromstring(b_str, dtype=numpy.float32) blue.shape = (height, width) # (row, col) return red, green, blue, width, height
def exr_to_png(exrfile, pngfile): File = OpenEXR.InputFile(exrfile) PixType = Imath.PixelType(Imath.PixelType.FLOAT) DW = File.header()['dataWindow'] Size = (DW.max.x - DW.min.x + 1, DW.max.y - DW.min.y + 1) rgb = [ numpy.fromstring(File.channel(c, PixType), dtype=numpy.float32) for c in 'RGB' ] for i in range(3): rgb[i] = numpy.where(rgb[i] <= 0.0031308, (rgb[i] * 12.92) * 255.0, (1.055 * (rgb[i]**(1.0 / 2.2)) - 0.055) * 255.0) rgb8 = [Image.frombytes("F", Size, c.tostring()).convert("L") for c in rgb] Image.merge("RGB", rgb8).save(pngfile, "png", quality=100)
def test_reading_unknown_exr_type_fails(self): image, channels = _MakeTestImage(3, np.float16) with tempfile.NamedTemporaryFile() as temp: exr.write_exr(temp.name, image, channels) exr_file = OpenEXR.InputFile(temp.name) # Deliberately break the R channel header info. A mock InputFile is # required to override the header() method. header_dict = exr_file.header() header_dict['channels']['R'].type.v = -1 # Any bad value will do. make_mock_exr = collections.namedtuple('MockExr', ['header', 'channel']) mock_broken_exr = make_mock_exr(lambda: header_dict, exr_file.channel) with self.assertRaisesRegexp(RuntimeError, 'Unknown EXR channel type'): _ = exr.channels_to_ndarray(mock_broken_exr, ['R', 'G', 'B'])
def readexr(path): """EXR read helper. Requires OpenEXR.""" import OpenEXR fh = OpenEXR.InputFile(path) dw = fh.header()['dataWindow'] w, h = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1) rgb = [ np.ndarray([h, w], dtype="float32", buffer=fh.channel(c)) for c in ['R', 'G', 'B'] ] ret = np.zeros([h, w, 3], dtype='float32') for i in [0, 1, 2]: ret[:, :, i] = rgb[i] return ret
def __init__(self, f): self.file = OpenEXR.InputFile(f) pixelType = Imath.PixelType(Imath.PixelType.FLOAT) dw = self.file.header()['dataWindow'] self.width = dw.max.x - dw.min.x + 1 self.height = dw.max.y - dw.min.y + 1 self.size = (self.width, self.height) redStr = self.file.channel('R', pixelType) greenStr = self.file.channel('G', pixelType) blueStr = self.file.channel('B', pixelType) self.blue = array.array('f', blueStr) self.red = array.array('f', redStr) self.green = array.array('f', greenStr)
def readEXRImage(filepath, channelrange): """Helper function for reading .exr files from the KAIST dataset. Returns an array with dimension ordering (C, H, W) as required by pytorch. """ file = exr.InputFile(filepath) channels = ["w{}nm".format(wavelength) for wavelength in channelrange] header = file.header() ncols = header["displayWindow"].max.x + 1 nrows = header["displayWindow"].max.y + 1 pt = Imath.PixelType(Imath.PixelType.HALF) imgstrs = file.channels(channels, pt) full = np.zeros((len(channels), nrows, ncols), dtype=np.float16) for i, imgstr in enumerate(imgstrs): red = np.frombuffer(imgstr, dtype=np.float16) full[i, :, :] = np.reshape(red, (nrows, ncols)) return full
def readExr(file_path): exr_file = OpenEXR.InputFile(file_path) exr_header = exr_file.header() H, W = exr_header['dataWindow'].max.y + 1, exr_header[ 'dataWindow'].max.x + 1 assert all( (exr_header['channels'][channel].type == Imath.PixelType(OpenEXR.HALF) for channel in ('R', 'G', 'B'))) R, G, B = exr_file.channels(['R', 'G', 'B']) image_R = np.frombuffer(R, dtype=np.float16).reshape([H, W]) image_G = np.frombuffer(G, dtype=np.float16).reshape([H, W]) image_B = np.frombuffer(B, dtype=np.float16).reshape([H, W]) image = np.stack([image_R, image_G, image_B], axis=2) return image
def main(exrfile, jpgfile): file = OpenEXR.InputFile(exrfile) pt = Imath.PixelType(Imath.PixelType.FLOAT) dw = file.header()['dataWindow'] size = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1) rgbf = [Image.fromstring("F", size, file.channel(c, pt)) for c in "RGB"] extrema = [im.getextrema() for im in rgbf] darkest = min([lo for (lo,hi) in extrema]) lighest = max([hi for (lo,hi) in extrema]) scale = 255 / (lighest - darkest) def normalize_0_255(v): return (v * scale) + darkest rgb8 = [im.point(normalize_0_255).convert("L") for im in rgbf] Image.merge("RGB", rgb8).save(jpgfile)
def load_hdr_multichannel(path): """ Loads an HDR file with RGB, normal (3 channels) and depth (1 channel) :param path: file location :return: HDR image """ pt = Imath.PixelType(Imath.PixelType.FLOAT) rgb_img_openexr = OpenEXR.InputFile(path) rgb_img = rgb_img_openexr.header()['dataWindow'] size_img = (rgb_img.max.x - rgb_img.min.x + 1, rgb_img.max.y - rgb_img.min.y + 1) # color redstr = rgb_img_openexr.channel('color.R', pt) red = np.fromstring(redstr, dtype=np.float32) red.shape = (size_img[1], size_img[0]) greenstr = rgb_img_openexr.channel('color.G', pt) green = np.fromstring(greenstr, dtype=np.float32) green.shape = (size_img[1], size_img[0]) bluestr = rgb_img_openexr.channel('color.B', pt) blue = np.fromstring(bluestr, dtype=np.float32) blue.shape = (size_img[1], size_img[0]) color = np.dstack((red, green, blue)) # normal normal_x_str = rgb_img_openexr.channel('normal.R', pt) normal_x = np.fromstring(normal_x_str, dtype=np.float32) normal_x.shape = (size_img[1], size_img[0]) normal_y_str = rgb_img_openexr.channel('normal.G', pt) normal_y = np.fromstring(normal_y_str, dtype=np.float32) normal_y.shape = (size_img[1], size_img[0]) normal_z_str = rgb_img_openexr.channel('normal.B', pt) normal_z = np.fromstring(normal_z_str, dtype=np.float32) normal_z.shape = (size_img[1], size_img[0]) normal = np.dstack((normal_x, normal_y, normal_z)) # depth depth_str = rgb_img_openexr.channel('distance.Y', pt) depth = np.fromstring(depth_str, dtype=np.float32) depth.shape = (size_img[1], size_img[0]) return color, depth, normal
def ext2hdr(exrpath): File = OpenEXR.InputFile(exrpath) PixType = Imath.PixelType(Imath.PixelType.FLOAT) DW = File.header()['dataWindow'] Size = (DW.max.x - DW.min.x + 1, DW.max.y - DW.min.y + 1) rgb = [ numpy.fromstring(File.channel(c, PixType), dtype=numpy.float32) for c in 'RGB' ] r = numpy.reshape(rgb[0], (Size[1], Size[0])) g = numpy.reshape(rgb[1], (Size[1], Size[0])) b = numpy.reshape(rgb[2], (Size[1], Size[0])) hdr = numpy.zeros((Size[1], Size[0], 3), dtype=numpy.float32) hdr[:, :, 0] = b hdr[:, :, 1] = g hdr[:, :, 2] = r return hdr
def get_color_array(path): pixel_type = Imath.PixelType(Imath.PixelType.FLOAT) file = OpenEXR.InputFile(path) data_window = file.header()['dataWindow'] size = (data_window.max.x - data_window.min.x + 1, data_window.max.y - data_window.min.y + 1) r_str = file.channel('R', pixel_type) g_str = file.channel('G', pixel_type) b_str = file.channel('B', pixel_type) r = np.frombuffer(r_str, dtype=np.float32) r.shape = (size[1], size[0], 1) g = np.frombuffer(g_str, dtype=np.float32) g.shape = (size[1], size[0], 1) b = np.frombuffer(b_str, dtype=np.float32) b.shape = (size[1], size[0], 1) rgb = np.concatenate([r, g, b], axis=2) return rgb
def readEXR(hdrfile): pt = Imath.PixelType(Imath.PixelType.FLOAT) hdr_t = OpenEXR.InputFile(hdrfile) dw = hdr_t.header()['dataWindow'] size = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1) rstr = hdr_t.channel('R', pt) gstr = hdr_t.channel('G', pt) bstr = hdr_t.channel('B', pt) r = np.fromstring(rstr, dtype=np.float32) r.shape = (size[1], size[0]) g = np.fromstring(gstr, dtype=np.float32) g.shape = (size[1], size[0]) b = np.fromstring(bstr, dtype=np.float32) b.shape = (size[1], size[0]) res = np.stack([r, g, b], axis=-1) imhdr = np.asarray(res) return imhdr
def getPixels(path): image = OpenEXR.InputFile(path) header = image.header() pt = Imath.PixelType(Imath.PixelType.FLOAT) dw = header['dataWindow'] x, y = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1) im = numpy.zeros((x, y, 3)) im[:, :, 0] = numpy.frombuffer(image.channel('R', pt), dtype=numpy.float32).reshape((x, y)) im[:, :, 1] = numpy.frombuffer(image.channel('G', pt), dtype=numpy.float32).reshape((x, y)) im[:, :, 2] = numpy.frombuffer(image.channel('B', pt), dtype=numpy.float32).reshape((x, y)) im = numpy.clip(im, 0, 1) return srgb.to(im), x, y
def __init__(self, image_path): exrfile = exr.InputFile(image_path) dw = exrfile.header()['dataWindow'] channels = ['R', 'G', 'B', 'A'] self.header = exrfile.header() self.channels = [] self.size = (dw.max.y - dw.min.y + 1, dw.max.x - dw.min.x + 1) self.channelData = dict() for c in self.header['channels']: self.channels.append(c) C = exrfile.channel(c, Imath.PixelType(Imath.PixelType.FLOAT)) C = np.fromstring(C, dtype=np.float32) C = np.reshape(C, self.size) self.channelData[c] = C
def process_depth(exr_path): depth_exr = OpenEXR.InputFile(exr_path) dw = depth_exr.header()['dataWindow'] sz = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1) depth = depth_exr.channel('Image.V', Imath.PixelType(Imath.PixelType.FLOAT)) depth = np.fromstring(depth, dtype='float32') depth.shape = (sz[1], sz[0]) depth = depth.astype('float32') * 10000 depth = depth.astype(np.uint16) mask = 255 * np.ones(shape=depth.shape, dtype='uint8') mask[depth == 0] = 0 os.remove(exr_path) return depth, mask
def exr2arr(exrfile): file = OpenEXR.InputFile(exrfile) dw = file.header()['dataWindow'] channels = file.header()['channels'].keys() channels_list = list() for c in ('R', 'G', 'B', 'A'): if c in channels: channels_list.append(c) # the shape had incorrect order #size = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1) size = (dw.max.y - dw.min.y + 1, dw.max.x - dw.min.x + 1) color_channels = file.channels(channels_list, Imath.PixelType(Imath.PixelType.FLOAT)) channels_tuple = [np.fromstring(channel, dtype='f') for channel in color_channels] res = np.dstack(channels_tuple) return res.reshape(size + (len(channels_tuple),))
def read_exr_image(exr_filename, channels=['R', 'G', 'B']): exrfile = exr.InputFile(exr_filename) dw = exrfile.header()['dataWindow'] isize = (dw.max.y - dw.min.y + 1, dw.max.x - dw.min.x + 1) channelData_arr = dict() for c in channels: C = exrfile.channel(c, Imath.PixelType(Imath.PixelType.FLOAT)) C = np.fromstring(C, dtype=np.float32) C = np.reshape(C, isize) channelData_arr[c] = C # get the whole array img_arr = np.concatenate( [channelData_arr[c][..., np.newaxis] for c in ['R', 'G', 'B']], axis=2) # (res, res, 3) return img_arr
def loadexr(filename): """Open an exr image and return a numpy array.""" f = OpenEXR.InputFile(filename) dw = f.header()['dataWindow'] sz = (dw.max.y - dw.min.y + 1, dw.max.x - dw.min.x + 1) # Read the three color channels as 32-bit floats FLOAT = Imath.PixelType(Imath.PixelType.FLOAT) image = np.empty((sz[0], sz[1], 3), dtype=np.float32) image[..., 0] = np.fromstring(f.channel("R", FLOAT), dtype=np.float32).reshape(sz) image[..., 1] = np.fromstring(f.channel("G", FLOAT), dtype=np.float32).reshape(sz) image[..., 2] = np.fromstring(f.channel("B", FLOAT), dtype=np.float32).reshape(sz) return image
def readEXR(filename): """Read color + depth data from EXR image file. Parameters ---------- filename : str File path. Returns ------- img : Float matrix Z : Depth buffer in float32 format or None if the EXR file has no Z channel. """ exrfile = exr.InputFile(filename) header = exrfile.header() dw = header["dataWindow"] isize = (dw.max.y - dw.min.y + 1, dw.max.x - dw.min.x + 1) channelData = dict() # convert all channels in the image to numpy arrays for c in header["channels"]: C = exrfile.channel(c, Imath.PixelType(Imath.PixelType.FLOAT)) C = np.frombuffer(C, dtype=np.float32) C = np.reshape(C, isize) channelData[c] = C colorChannels = (["R", "G", "B", "A"] if "A" in header["channels"] else ["R", "G", "B"]) img = np.concatenate( [channelData[c][..., np.newaxis] for c in colorChannels], axis=2) # linear to standard RGB # img[..., :3] = np.where(img[..., :3] <= 0.0031308, # 12.92 * img[..., :3], # 1.055 * np.power(img[..., :3], 1 / 2.4) - 0.055) # sanitize image to be in range [0, 1] # img = np.where(img < 0.0, 0.0, np.where(img > 1.0, 1, img)) Z = None if "Z" not in header["channels"] else channelData["Z"] return img, Z
def readEXR(filenameEXR): pt = Imath.PixelType( Imath.PixelType.FLOAT ) #only works hard-coding this and np.float32, instead of using the value from: exr.header()['channels'][c].type exr = OpenEXR.InputFile(filenameEXR) dw = exr.header()['dataWindow'] size = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1) #this needs to be fixed. 2 of the following 3 lines swap axes. rgb = np.swapaxes( np.array([ np.fromstring(exr.channel(c, pt), dtype=np.float32) for c in "RGB" ]), 0, 1) #I guess if we used HALF we would need to use np.float16 rgb.shape = (size[1], size[0], 3) #numpy array are (row, col) = (y, x) rgb = np.swapaxes(rgb, 0, 1) #(y,x,c) -> (x,y,c) #probably (x,y,c) -> (y,x,c) exr.close() return rgb
def read_openexr(fname): file = OpenEXR.InputFile(fname) header = file.header() dw = header['dataWindow'] pt = Imath.PixelType(Imath.PixelType.HALF) shape = (dw.max.y - dw.min.y + 1, dw.max.x - dw.min.x + 1) r = np.frombuffer(file.channel('Color.R', pt), dtype=np.float16).reshape(shape + (1, )) g = np.frombuffer(file.channel('Color.G', pt), dtype=np.float16).reshape(shape + (1, )) b = np.frombuffer(file.channel('Color.B', pt), dtype=np.float16).reshape(shape + (1, )) a = np.frombuffer(file.channel('Color.A', pt), dtype=np.float16).reshape(shape + (1, )) img = np.concatenate((r, g, b, a), -1).astype(np.float32) file.close() return img
def loadEXR(fname, channelNames): # Open the input file f = OpenEXR.InputFile(fname) # Compute the size dw = f.header()['dataWindow'] sz = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1) # Read the three color channels as 32-bit floats FLOAT = Imath.PixelType(Imath.PixelType.FLOAT) channels = [ array.array('f', f.channel(Chan, FLOAT)).tolist() for Chan in channelNames ] channels = [np.array(channel).reshape(sz) for channel in channels] return np.array(channels)
def load_exr(self, prefix_dir, prefix, file_kind, expected_height, expected_width): exr_file = OpenEXR.InputFile( os.path.join(prefix_dir, prefix + "-" + file_kind + ".exr")) cm_dw = exr_file.header()['dataWindow'] exr_data = np.fromstring(exr_file.channel( 'R', Imath.PixelType(Imath.PixelType.HALF)), dtype=np.float16) exr_data.shape = (cm_dw.max.y - cm_dw.min.y + 1, cm_dw.max.x - cm_dw.min.x + 1) # rows, cols if exr_data.shape[0] != expected_height: print("[ERROR] ", prefix, file_kind, " != expected image height", exr_data.shape[0], expected_height) if exr_data.shape[1] != expected_width: print("[ERROR] ", prefix, file_kind, " width != image width", exr_data.shape[1], expected_width) return exr_data
def convert_to_numpy(img_list, intens=None): imgs = [] for idx, img_name in enumerate(img_list): if idx % 10 == 0: print('Current idx %d' % (idx)) img = exr_to_array(OpenEXR.InputFile(img_name), 'color') if intens is not None: img = np.dot(img, intens[idx]) save_suffix = '_calib.npy' else: save_suffix = '_uncalib.npy' imgs.append(img) img_array = np.concatenate(imgs, 2) save_name = os.path.dirname(img_name) + save_suffix print('Saving numpy array to %s' % (save_name)) np.save(save_name, img_array) return img_array
def ConvertEXRToJPG(exrfile, jpgfile): File = OpenEXR.InputFile(exrfile) PixType = Imath.PixelType(Imath.PixelType.FLOAT) DW = File.header()['dataWindow'] Size = (DW.max.x - DW.min.x + 1, DW.max.y - DW.min.y + 1) rgb = [ np.fromstring(File.channel(c, PixType), dtype=np.float32) for c in 'RGB' ] for i in range(3): rgb[i] = np.where(rgb[i] <= 0.0031308, (rgb[i] * 12.92) * 255.0, (1.055 * (rgb[i]**(1.0 / 2.4)) - 0.055) * 255.0) rgb8 = [Image.frombytes("F", Size, c.tostring()).convert("L") for c in rgb] # rgb8 = [Image.fromarray(c.astype(int)) for c in rgb] Image.merge("RGB", rgb8).save(jpgfile, "JPEG", quality=95)
def get_point_cloud_from_exr(path, pos, ori, intrinsic): exr_file = OpenEXR.InputFile(path) PixType = Imath.PixelType(Imath.PixelType.FLOAT) DW = exr_file.header()['dataWindow'] Size = (DW.max.x - DW.min.x + 1, DW.max.y - DW.min.y + 1) rgb = [ np.frombuffer(exr_file.channel(c, PixType), dtype=np.float32) for c in 'RGB' ] img = np.reshape(rgb[0], (Size[1], Size[0])) depth = cv2.resize( img, (224, 224), interpolation=cv2.INTER_NEAREST) # all float number indicate distance pc = Depth2PointCloud(depth, intrinsic, ori, pos, org_size=img.shape).transpose() inf_idx = (pc != pc) | (np.abs(pc) > 100) pc[inf_idx] = 0.0 pc_index = np.nonzero(pc[:, 2] > 0.002)[0] pc = pc[pc_index] pc_x = pc[:, 0] pc_y = pc[:, 1] pc_z = pc[:, 2] del_idx = (pc_x < -0.22 / 2) | (pc_x > 0.22 / 2) | (pc_y < -0.22 / 2) | ( pc_y > 0.22 / 2) | (pc_z > 0.22) pc = pc[np.logical_not(del_idx)] xyz_max = pc.max(0) xyz_min = pc.min(0) xyz_diff = xyz_max - xyz_min xyz_idx = np.where(xyz_diff < 0.22 / 10)[0] if xyz_idx.shape[0] > 0: xyz_max[xyz_idx] += (0.22 / 10) xyz_min[xyz_idx] -= (0.22 / 10) grids = generate_grid(0.22, 10) grid_choose = (xyz_min.reshape(1, -1) <= grids) * (grids <= xyz_max.reshape(1, -1)) grid_choose = (grid_choose.sum(1) == 3) grids = grids[grid_choose] if grids.shape[0] > 400: idx = np.random.choice(np.arange(grids.shape[0]), 400, replace=False) grids = grids[idx] contact_index = np.arange(pc.shape[0]) pc = pc / np.array([0.22 / 2, 0.22 / 2, 0.22]) return pc, grids, contact_index
def loadEXR(self, filename): import OpenEXR import Imath pt = Imath.PixelType(Imath.PixelType.HALF) image = OpenEXR.InputFile(filename) header = image.header() dw = header['dataWindow'] channels = header['channels'] size = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1) self.source_width = size[0] self.source_height = size[1] if self.parm("width").eval() != 0: self.width = self.parm("width").eval() else: self.width = self.source_width if self.parm("height").eval() != 0: self.height = self.parm("height").eval() else: self.height = self.source_height redstr = image.channel('R', pt) host_buff_r = numpy.fromstring(redstr, dtype = numpy.float16) host_buff_r.shape = (size[1], size[0]) # Numpy arrays are (row, col) self.devInBufferR = cl.Image(self.engine.ctx, self.engine.mf.READ_ONLY | self.engine.mf.COPY_HOST_PTR, cl.ImageFormat(cl.channel_order.INTENSITY, cl.channel_type.HALF_FLOAT), shape=(self.source_width, self.source_height,), pitches=(self.source_width * 2,), hostbuf=host_buff_r) greenstr = image.channel('G', pt) host_buff_g = numpy.fromstring(greenstr, dtype = numpy.float16) host_buff_g.shape = (size[1], size[0]) # Numpy arrays are (row, col) self.devInBufferG = cl.Image(self.engine.ctx, self.engine.mf.READ_ONLY | self.engine.mf.COPY_HOST_PTR, cl.ImageFormat(cl.channel_order.INTENSITY, cl.channel_type.HALF_FLOAT), shape=(self.source_width, self.source_height,), pitches=(self.source_width * 2,), hostbuf=host_buff_g) bluestr = image.channel('B', pt) host_buff_b = numpy.fromstring(bluestr, dtype = numpy.float16) host_buff_b.shape = (size[1], size[0]) # Numpy arrays are (row, col) self.devInBufferB = cl.Image(self.engine.ctx, self.engine.mf.READ_ONLY | self.engine.mf.COPY_HOST_PTR, cl.ImageFormat(cl.channel_order.INTENSITY, cl.channel_type.HALF_FLOAT), shape=(self.source_width, self.source_height,), pitches=(self.source_width * 2,), hostbuf=host_buff_b) if(channels.get('A') is not None): alphastr = image.channel('A', pt) host_buff_a = numpy.fromstring(alphastr, dtype = numpy.float16) host_buff_a.shape = (size[1], size[0]) # Numpy arrays are (row, col) self.devInBufferA = cl.Image(self.engine.ctx, self.engine.mf.READ_ONLY | self.engine.mf.COPY_HOST_PTR, cl.ImageFormat(cl.channel_order.INTENSITY, cl.channel_type.HALF_FLOAT), shape=(self.source_width, self.source_height,), pitches=(self.source_width * 2,), hostbuf=host_buff_a) else: self.devInBufferA = cl.Image(self.engine.ctx, self.engine.mf.READ_ONLY | self.engine.mf.COPY_HOST_PTR, cl.ImageFormat(cl.channel_order.INTENSITY, cl.channel_type.HALF_FLOAT), shape=(self.source_width, self.source_height,), pitches=(self.source_width * 2,), hostbuf=numpy.ones(self.source_width * self.source_height, dtype = numpy.float16))
def colorDeptImage(colorFile, depthFile): pt = Imath.PixelType(Imath.PixelType.FLOAT) depthFile_ = OpenEXR.InputFile(depthFile) dw = depthFile_.header()['dataWindow'] size = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1) depthstr = depthFile_.channel('Y', pt) depth = np.fromstring(depthstr, dtype=np.float32) depth.shape = (size[1], size[0]) img = cv2.imread(colorFile) imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) res, thresh = cv2.threshold(imgray, 120, 255, 0) img0, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) contours = list( filter( lambda x: cv2.contourArea(x) > 1000 and cv2.contourArea(x) < 5000, contours))
def loadImageWithOpenEXR(exrfile): # Taken from http://discourse.techart.online/t/exr-files-into-pyqt-interface/3325/9 file = OpenEXR.InputFile(exrfile) pt = Imath.PixelType(Imath.PixelType.FLOAT) dw = file.header()['dataWindow'] size = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1) rgbf = [Image.frombytes("F", size, file.channel(c, pt)) for c in "RGB"] extrema = [im.getextrema() for im in rgbf] darkest = min([lo for (lo,hi) in extrema]) lighest = max([hi for (lo,hi) in extrema]) scale = 255 / (lighest - darkest) def normalize_0_255(v): return (v * scale) + darkest rgb8 = [im.point(normalize_0_255).convert("L") for im in rgbf] myjpg = Image.merge("RGB", rgb8) return myjpg
def exrHeaderInfo(imagePath): ''' check if the EXR is valid, read the header and return a key, value dict of its values ''' if not 'OpenEXR' in sys.modules.keys(): print "No openEXR module" return #check it is a valid EXR if OpenEXR.isOpenExrFile(imagePath): infile = OpenEXR.InputFile(imagePath) if not infile.isComplete(): print "Invalid EXR: %s" %imagePath #return True here as we still want the directory scan to continue return True EXRheader = infile.header() rDict = {} for k,v in EXRheader.iteritems(): rDict[k]=v #print k,v return rDict
def open(filename): # Check if the file is an EXR file if not OpenEXR.isOpenExrFile(filename): raise Exception("File '%s' is not an EXR file." % filename) # Return an `InputFile` return InputFile(OpenEXR.InputFile(filename), filename)
def load_rgb(filePath): ''' Loads an rgb OpenEXR image.''' if oe.isOpenExrFile(filePath) is not True: raise Exception("File ", filePath, " does not exist!") try: ifi = oe.InputFile(filePath) # Compute size header = ifi.header() dw = header["dataWindow"] w, h = dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1 # Read the three channels ifiType = header["channels"]["R"].type.v if ifiType is not im.PixelType.UINT: raise Exception("Only uint32 supported! (file is type ", ifiType) R = ifi.channel("R", im.PixelType(ifiType)) G = ifi.channel("G", im.PixelType(ifiType)) B = ifi.channel("B", im.PixelType(ifiType)) ifi.close() image = np.zeros((h, w, 3), dtype = np.uint32) # order = "C" image[:, :, 0] = np.core.multiarray.fromstring(R, dtype = np.uint32).reshape(h, w) image[:, :, 1] = np.core.multiarray.fromstring(G, dtype = np.uint32).reshape(h, w) image[:, :, 2] = np.core.multiarray.fromstring(B, dtype = np.uint32).reshape(h, w) except: raise return image