Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
The data array read from the file.
"""
file_root, file_ext = os.path.splitext(filename)
data = None
if file_ext.lower() in COLOR_IMAGE_EXTS:
data = cv2.cvtColor(cv2.imread(filename), cv2.COLOR_BGR2RGB)
elif file_ext == '.npy':
data = np.load(filename)
elif file_ext == '.npz':
data = np.load(filename)['arr_0']
else:
raise ValueError('Extension %s not supported' % (file_ext))
return data
class ColorImage(Image):
"""An RGB color image.
"""
def __init__(self, data, frame='unspecified', encoding='rgb8'):
"""Create a color image from an array of data.
Parameters
----------
data : :obj:`numpy.ndarray`
An array of data with which to make the image. The first dimension
of the data should index rows, the second columns, and the third
individual pixel elements (i.e. R,G,B values). Alternatively, the
image may have a single channel, in which case it is interpreted as
greyscale.
frame : :obj:`str`
A string representing the frame of reference in which the new image
lies.
Returns
-------
:obj:`DepthImage`
The new depth image.
"""
file_root, file_ext = os.path.splitext(filename)
data = Image.load_data(filename)
if file_ext.lower() in COLOR_IMAGE_EXTS:
data = (data * (MAX_DEPTH / BINARY_IM_MAX_VAL)).astype(np.float32)
return DepthImage(data, frame)
class IrImage(Image):
"""An IR image in which individual pixels have a single uint16 channel.
"""
def __init__(self, data, frame='unspecified'):
"""Create an IR image from an array of data.
Parameters
----------
data : :obj:`numpy.ndarray`
An array of data with which to make the image. The first dimension
of the data should index rows, the second columns, and the third
individual pixel elements (IR values as uint16's).
frame : :obj:`str`
A string representing the frame of reference in which this image
lies.
.npy, or .npz.
frame : :obj:`str`
A string representing the frame of reference in which the new image
lies.
Returns
-------
:obj:`GrayscaleImage`
The new grayscale image.
"""
data = Image.load_data(filename)
return GrayscaleImage(data, frame)
class BinaryImage(Image):
"""A binary image in which individual pixels are either black or white (0 or BINARY_IM_MAX_VAL).
"""
def __init__(self, data, frame='unspecified',
threshold=BINARY_IM_DEFAULT_THRESH):
"""Create a BinaryImage image from an array of data.
Parameters
----------
data : :obj:`numpy.ndarray`
An array of data with which to make the image. The first dimension
of the data should index rows, the second columns, and the third
individual pixel elements (only one channel, all uint8).
The data array will be thresholded
and will end up only containing elements that are BINARY_IM_MAX_VAL or 0.
.npy, or .npz.
frame : :obj:`str`
A string representing the frame of reference in which the new image
lies.
Returns
-------
:obj:`ColorImage`
The new color image.
"""
data = Image.load_data(filename).astype(np.uint8)
return ColorImage(data, frame)
class DepthImage(Image):
"""A depth image in which individual pixels have a single floating-point
depth channel.
"""
def __init__(self, data, frame='unspecified'):
"""Create a depth image from an array of data.
Parameters
----------
data : :obj:`numpy.ndarray`
An array of data with which to make the image. The first dimension
of the data should index rows, the second columns, and the third
individual pixel elements (depths as floating point numbers).
frame : :obj:`str`
A string representing the frame of reference in which this image
def to_color(self):
if self.num_segments > 10:
color = (np.iinfo(np.uint8).max * plt.cm.tab20(self.data)).astype(np.uint8)
else:
color = (np.iinfo(np.uint8).max * plt.cm.tab10(self.data)).astype(np.uint8)
return ColorImage(color[:, :, :3])
@staticmethod
def open(filename, frame='unspecified'):
""" Opens a segmentation image """
data = Image.load_data(filename)
return SegmentationImage(data, frame)
class PointCloudImage(Image):
"""A point cloud image in which individual pixels have three float channels.
"""
def __init__(self, data, frame='unspecified'):
"""Create a PointCloudImage image from an array of data.
Parameters
----------
data : :obj:`numpy.ndarray`
An array of data with which to make the image. The first dimension
of the data should index rows, the second columns, and the third
individual pixel elements (three floats).
frame : :obj:`str`
A string representing the frame of reference in which this image
lies.
center_j : int
The center width point at which to crop. If not specified, the center
of the image is used.
Returns
-------
:obj:`Image`
A cropped Image of the same type.
"""
gray_im_crop = self.gray.crop(height, width, center_i, center_j)
depth_im_crop = self.depth.crop(height, width, center_i, center_j)
return GdImage.from_grayscale_and_depth(gray_im_crop, depth_im_crop)
class SegmentationImage(Image):
"""An image containing integer-valued segment labels.
"""
def __init__(self, data, frame='unspecified'):
"""Create a Segmentation image from an array of data.
Parameters
----------
data : :obj:`numpy.ndarray`
An array of data with which to make the image. The first dimension
of the data should index rows, the second columns, and the third
individual pixel elements (only one channel, all uint8).
The integer-valued data should correspond to segment labels.
frame : :obj:`str`
A string representing the frame of reference in which this image
def from_array(x, frame='unspecified'):
""" Converts an array of data to an Image based on the values in the array and the data format. """
if not Image.can_convert(x):
raise ValueError('Cannot convert array to an Image!')
dtype = x.dtype
height = x.shape[0]
width = x.shape[1]
channels = 1
if len(x.shape) == 3:
channels = x.shape[2]
if dtype == np.uint8:
if channels == 1:
if np.any((x % BINARY_IM_MAX_VAL) > 0):
return GrayscaleImage(x, frame)
return BinaryImage(x, frame)
elif channels == 3:
return ColorImage(x, frame)
else:
frame : :obj:`str`
A string representing the frame of reference in which the new image
lies.
Returns
-------
:obj:`BinaryImage`
The new binary image.
"""
data = Image.load_data(filename)
if len(data.shape) > 2 and data.shape[2] > 1:
data = data[:, :, 0]
return BinaryImage(data, frame)
class RgbdImage(Image):
""" An image containing a red, green, blue, and depth channel. """
def __init__(self, data, frame='unspecified'):
""" Create an RGB-D image from an array of data.
Parameters
----------
data : :obj:`numpy.ndarray`
An array of data with which to make the image. The first dimension
of the data should index rows, the second columns, and the third
individual pixel elements (four channels, all float).
The first three channels should be the red, greed, and blue channels
which must be in the range (0, BINARY_IM_MAX_VAL).
The fourth channel should be the depth channel.
frame : :obj:`str`
center_j : int
The center width point at which to crop. If not specified, the center
of the image is used.
Returns
-------
:obj:`Image`
A cropped Image of the same type.
"""
color_im_crop = self.color.crop(height, width, center_i, center_j)
depth_im_crop = self.depth.crop(height, width, center_i, center_j)
return RgbdImage.from_color_and_depth(color_im_crop, depth_im_crop)
class GdImage(Image):
""" An image containing a grayscale and depth channel. """
def __init__(self, data, frame='unspecified'):
"""Create a G-D image from an array of data.
Parameters
----------
data : :obj:`numpy.ndarray`
An array of data with which to make the image. The first dimension
of the data should index rows, the second columns, and the third
individual pixel elements (two channels, both float).
The first channel should be the grayscale channel
which must be in the range (0, BINARY_IM_MAX_VAL).
The second channel should be the depth channel.
frame : :obj:`str`
frame : :obj:`str`
A string representing the frame of reference in which the new image
lies.
Returns
-------
:obj:`IrImage`
The new IR image.
"""
data = Image.load_data(filename)
data = (data * (MAX_IR / BINARY_IM_MAX_VAL)).astype(np.uint16)
return IrImage(data, frame)
class GrayscaleImage(Image):
"""A grayscale image in which individual pixels have a single uint8 channel.
"""
def __init__(self, data, frame='unspecified'):
"""Create a grayscale image from an array of data.
Parameters
----------
data : :obj:`numpy.ndarray`
An array of data with which to make the image. The first dimension
of the data should index rows, the second columns, and the third
individual pixel elements (greyscale values as uint8's).
frame : :obj:`str`
A string representing the frame of reference in which this image
lies.