def convert_image(self, image: carla.Image, color_converter=None): color_converter = color_converter or self.color_converter or carla.ColorConverter.Raw image.convert(color_converter) array = np.frombuffer(image.raw_data, dtype=np.uint8) array = np.reshape(array, (image.height, image.width, 4)) array = array[:, :, :3] array = array[:, :, ::-1] return array
def convert_rgb_from_source_to_agent( self, source: carla.Image) -> Union[RGBData, None]: """Convert CARLA raw Image to a Union with RGB numpy array""" try: source.convert(cc.Raw) return RGBData(data=self._to_rgb_array(source)) except: return None
def carla_cityscapes_image_to_ndarray(image: carla.Image) -> np.ndarray: # pylint: disable=no-member """Returns a `NumPy` array from a `CARLA` semantic segmentation image. Args: image: The `CARLA` semantic segmented image. Returns: A `NumPy` array representation of the image. """ image.convert(carla.ColorConverter.CityScapesPalette) # pylint: disable=no-member array = np.frombuffer(image.raw_data, dtype=np.dtype("uint8")) array = array.astype(np.float32) / 255 array = np.reshape(array, (image.height, image.width, 4)) array = array[:, :, :3] array = array[:, :, ::-1] return array
def convert(image: carla.Image, log=False): """Converts the given carla.Image into grayscale in which each pixel contains the depth distance.""" image.convert(carla.ColorConverter.Raw) # to rgb image array = np.frombuffer(image.raw_data, dtype=np.uint8) array = np.reshape(array, (image.height, image.width, 4)) array = array[:, :, :3] array = array[:, :, ::-1] # to depth b = array[:, :, 0] g = array[:, :, 1] r = array[:, :, 2] normalized = (r + g * 256 + b * 256 * 256) / (256 * 256 * 256 - 1) in_meters = 1000 * normalized if log: return np.log(in_meters) return in_meters