Python Imath.PixelType() Examples

The following are 12 code examples of Imath.PixelType(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module Imath , or try the search function .
Example #1
Source File: utils.py    From noise2noise-pytorch with MIT License 7 votes vote down vote up
def load_hdr_as_tensor(img_path):
    """Converts OpenEXR image to torch float tensor."""

    # Read OpenEXR file
    if not OpenEXR.isOpenExrFile(img_path):
        raise ValueError(f'Image {img_path} is not a valid OpenEXR file')
    src = OpenEXR.InputFile(img_path)
    pixel_type = Imath.PixelType(Imath.PixelType.FLOAT)
    dw = src.header()['dataWindow']
    size = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1)
    
    # Read into tensor
    tensor = torch.zeros((3, size[1], size[0]))
    for i, c in enumerate('RGB'):
        rgb32f = np.fromstring(src.channel(c, pixel_type), dtype=np.float32)
        tensor[i, :, :] = torch.from_numpy(rgb32f.reshape(size[1], size[0]))
        
    return tensor 
Example #2
Source File: render_utils.py    From pvnet-rendering with Apache License 2.0 7 votes vote down vote up
def exr_to_png(exr_path):
        depth_path = exr_path.replace('.png0001.exr', '.png')
        exr_image = OpenEXR.InputFile(exr_path)
        dw = exr_image.header()['dataWindow']
        (width, height) = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1)

        def read_exr(s, width, height):
            mat = np.fromstring(s, dtype=np.float32)
            mat = mat.reshape(height, width)
            return mat

        dmap, _, _ = [read_exr(s, width, height) for s in exr_image.channels('BGR', Imath.PixelType(Imath.PixelType.FLOAT))]
        dmap = Image.fromarray((dmap != 1).astype(np.int32))
        dmap.save(depth_path)
        exr_image.close()
        os.system('rm {}'.format(exr_path)) 
Example #3
Source File: dataset_utils.py    From rpg_davis_simulator with GNU General Public License v3.0 6 votes vote down vote up
def extract_grayscale(img, srgb=False):
  dw = img.header()['dataWindow']

  size = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1)
  precision = Imath.PixelType(Imath.PixelType.FLOAT)
  R = img.channel('R', precision)
  G = img.channel('G', precision)
  B = img.channel('B', precision)
  
  r = np.fromstring(R, dtype = np.float32)
  g = np.fromstring(G, dtype = np.float32)
  b = np.fromstring(B, dtype = np.float32)
  
  r.shape = (size[1], size[0])
  g.shape = (size[1], size[0])
  b.shape = (size[1], size[0])
  
  rgb = cv2.merge([b, g, r])
  grayscale = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)
  
  if srgb:
      grayscale = lin2srgb(grayscale)

  return grayscale 
Example #4
Source File: dataset_utils.py    From EVDodgeNet with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def extract_grayscale(img, srgb=False):
  dw = img.header()['dataWindow']

  size = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1)
  precision = Imath.PixelType(Imath.PixelType.FLOAT)
  R = img.channel('R', precision)
  G = img.channel('G', precision)
  B = img.channel('B', precision)
  
  r = np.fromstring(R, dtype = np.float32)
  g = np.fromstring(G, dtype = np.float32)
  b = np.fromstring(B, dtype = np.float32)
  
  r.shape = (size[1], size[0])
  g.shape = (size[1], size[0])
  b.shape = (size[1], size[0])
  
  rgb = cv2.merge([b, g, r])
  grayscale = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)
  
  if srgb:
      grayscale = lin2srgb(grayscale)

  return grayscale 
Example #5
Source File: dataset_utils.py    From EVDodgeNet with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def extract_rgb(img, srgb=False):
  dw = img.header()['dataWindow']

  size = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1)
  precision = Imath.PixelType(Imath.PixelType.FLOAT)
  R = img.channel('R', precision)
  G = img.channel('G', precision)
  B = img.channel('B', precision)
  
  r = np.fromstring(R, dtype = np.float32)
  g = np.fromstring(G, dtype = np.float32)
  b = np.fromstring(B, dtype = np.float32)
  
  r.shape = (size[1], size[0])
  g.shape = (size[1], size[0])
  b.shape = (size[1], size[0])
  
  rgb = cv2.merge([b, g, r])
#  grayscale = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)
  
#  if srgb:
#      grayscale = lin2srgb(grayscale)

  return rgb 
Example #6
Source File: dataset_utils.py    From rpg_davis_simulator with GNU General Public License v3.0 5 votes vote down vote up
def extract_depth(img):
  dw = img.header()['dataWindow']
  size = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1)
  precision = Imath.PixelType(Imath.PixelType.FLOAT)
  Z = img.channel('Z', precision)
  z = np.fromstring(Z, dtype = np.float32)
  z.shape = (size[1], size[0])
  return z 
Example #7
Source File: exr.py    From graphics with Apache License 2.0 5 votes vote down vote up
def write_exr(filename, values, channel_names):
  """Writes the values in a multi-channel ndarray into an EXR file.

  Args:
    filename: The filename of the output file
    values: A numpy ndarray with shape [height, width, channels]
    channel_names: A list of strings with length = channels

  Raises:
    TypeError: If the numpy array has an unsupported type.
    ValueError: If the length of the array and the length of the channel names
      list do not match.
  """
  if values.shape[-1] != len(channel_names):
    raise ValueError(
        'Number of channels in values does not match channel names (%d, %d)' %
        (values.shape[-1], len(channel_names)))
  header = OpenEXR.Header(values.shape[1], values.shape[0])
  try:
    exr_channel_type = Imath.PixelType(_np_to_exr[values.dtype.type])
  except KeyError:
    raise TypeError('Unsupported numpy type: %s' % str(values.dtype))
  header['channels'] = {
      n: Imath.Channel(exr_channel_type) for n in channel_names
  }
  channel_data = [values[..., i] for i in range(values.shape[-1])]
  exr = OpenEXR.OutputFile(filename, header)
  exr.writePixels(
      dict((n, d.tobytes()) for n, d in zip(channel_names, channel_data)))
  exr.close() 
Example #8
Source File: convertEXR.py    From 3D-point-cloud-generation with MIT License 5 votes vote down vote up
def readEXR(fname,RESOLUTION):
	channel_list = ["B","G","R"]
	file = OpenEXR.InputFile(fname)
	dw = file.header()["dataWindow"]
	height,width = RESOLUTION,RESOLUTION
	FLOAT = Imath.PixelType(Imath.PixelType.FLOAT)
	vectors = [np.array(array.array("f",file.channel(c,FLOAT))) for c in channel_list]
	depth = vectors[0].reshape([height,width])
	return depth 
Example #9
Source File: process_exr.py    From pcn with MIT License 5 votes vote down vote up
def read_exr(exr_path, height, width):
    file = OpenEXR.InputFile(exr_path)
    depth_arr = array.array('f', file.channel('R', Imath.PixelType(Imath.PixelType.FLOAT)))
    depth = np.array(depth_arr).reshape((height, width))
    depth[depth < 0] = 0
    depth[np.isinf(depth)] = 0
    return depth 
Example #10
Source File: dataset_utils.py    From EVDodgeNet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def extract_depth(img):
  dw = img.header()['dataWindow']
  size = (dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1)
  precision = Imath.PixelType(Imath.PixelType.FLOAT)
  Z = img.channel('Z', precision)
  z = np.fromstring(Z, dtype = np.float32)
  z.shape = (size[1], size[0])
  return z 
Example #11
Source File: synthetic.py    From opensurfaces with MIT License 4 votes vote down vote up
def open_multilayer_exr_layers(inputfile, layers):
    """
    Load a list of images, each corresponding to a layer of an OpenEXR file.

    Note that "layer" does not correspond to a single color channel, like "R",
    but rather, a group of 3 color channels.

    :param inputfile: string filename
    :param layers: list of string layer names
    """
    f = OpenEXR.InputFile(inputfile)
    header = f.header()
    dw = header['dataWindow']
    cols, rows = dw.max.x - dw.min.x + 1, dw.max.y - dw.min.y + 1

    # load channels
    FLOAT = Imath.PixelType(Imath.PixelType.FLOAT)
    images = []
    for layer in layers:
        channels = LAYER_CHANNELS[layer]
        image = np.empty((rows, cols, 3), dtype=np.float32)
        for (i, c) in enumerate(channels):
            data = f.channel(c, FLOAT)
            image[:, :, i] = np.fromstring(data, dtype=np.float32) \
                .reshape((rows, cols))
        images.append(image)

    return images


#def denoise_indirect_image(img):
    #denoised = np.empty_like(img)
    #for c in xrange(3):
        #denoised[:, :, c] = median_filter(
            #img[:, :, c],
            #mode='reflect',
            #footprint=[
                #[0, 1, 0],
                #[1, 1, 1],
                #[0, 1, 0],
            #]
        #)
    #return denoised 
Example #12
Source File: flow.py    From EVDodgeNet with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def rigid_flow(self):

    	V, Omega, dt = self.compute_velocity_from_msg(self.pose_data[200,:], self.pose_data[204,:])

    	# print(self.depth_data[1][1])

    	depth_image0 = OpenEXR.InputFile(self.depth_data[200][1])
    	dw0 = depth_image0.header()['dataWindow']
    	size0 = (dw0.max.x - dw0.min.x + 1, dw0.max.y - dw0.min.y + 1)
    	pt0 = Imath.PixelType(Imath.PixelType.FLOAT)
    	depth0 = np.fromstring(depth_image0.channel("Z"), dtype=np.float32)
        depth0.shape = (size0[1], size0[0])  # Numpy arrays are (row, col)

        depth_image1 = OpenEXR.InputFile(self.depth_data[204][1])
        dw1 = depth_image1.header()['dataWindow']
        size1 = (dw1.max.x - dw1.min.x + 1, dw1.max.y - dw1.min.y + 1)
        pt1 = Imath.PixelType(Imath.PixelType.FLOAT)
        depth1 = np.fromstring(depth_image1.channel("Z"), dtype=np.float32)
        depth1.shape = (size1[1], size1[0])  # Numpy arrays are (row, col)

        depth = (depth0+depth1)/2

    	flow_x_dist, flow_y_dist = self.compute_flow_single_frame(V,
                                                                  Omega,
                                                                  depth,
                                                                  dt)
    	print(flow_x_dist, flow_y_dist)

    	flow = np.dstack((flow_x_dist, flow_y_dist))
        flow = np.float32(flow)
    	# verfication 
    	img1 = cv2.imread(self.image_data[200][1],1)
        # img1 = np.float32(img1)
        print(img1.shape)
    	img2 = cv2.imread(self.image_data[204][1], 1)
        # img2 = np.float32(img2)
        print(img1.shape, flow.dtype)

    	warpped_img1 = self.warp_image(img2, flow)
        # warpped_img1 = self.warp_flow(img2, flow)

        cv2.imshow('warpped_img1', cv2.subtract(img1, warpped_img1))


        first_img = self.colorize_image(flow_x_dist, flow_y_dist)
        cv2.imshow('image',first_img)
        cv2.waitKey(0)