DataProcessing

class DataProcessing(samples, dim=2, **kwargs)[source]

Bases: object

Process multiple SingleImages

See also

SingleImage2D

static _get_files(directory, extensions)[source]

return files with extensions

Parameters:
  • directory (str) – directory containing the files
  • extensions (list) – list of strings specifying valid extensions
Returns:

valid files

Return type:

list

classmethod from_dir(data_dir, verbose=True, n_jobs=None, n_dim=2)[source]

create class instance from directory

Parameters:
  • data_dir (str) – directory where shapedata is stored
  • verbose (bool) – whether or not to print current progress
  • n_jobs (int) – number of jobs for loading data (default: None -> all available CPUs are used)
  • n_dim (int) – Integer indicating the dimensionality of the image (default: 2)
Returns:

class instance

Return type:

DataProcessing

images

get list of samples’ pixels

Returns:pixels
Return type:list
landmarks

get list of samples’ landmarks

Returns:landmarks
Return type:list
lmk_pca(scale: bool, center: bool, pt_indices=[], *args, **kwargs)[source]

perform PCA on samples’ landmarks

Parameters:
  • scale (bool) – whether or not to scale the principal components with the corresponding eigen value
  • center (bool) – whether or not to substract mean before pca
  • pt_indices (int) – indices to include into PCA (if empty: include all points)
  • args (list) – additional positional arguments (passed to pca)
  • **kwargs – additional keyword arguments (passed to pca)
Returns:

eigen_shapes

Return type:

np.array

resize(img_size)[source]

resize all samples

Parameters:img_size (tuple) – new image size

SingleImage2D

class SingleImage2D(img, lmk=None, **kwargs)[source]

Bases: shapedata.base_data_processing.BaseSingleImage

Holds Single Image

_crop(min_y, min_x, max_y, max_x)[source]

Implements actual cropping inplace

Parameters:
  • min_y (int) – minimum y value
  • min_x (int) – minimum x value
  • max_y (int) – maximum y value
  • max_x (int) – maximum x value
Returns:

cropped image

Return type:

SingleImage2D

static _crop_lmks(lmks, min_y, min_x, max_y, max_x)

Crops landmarks to given values

Parameters:
  • lmks (np.ndarray) – landmarks to crop
  • min_y (int) – minimum y value
  • min_x (int) – minimum x value
  • max_y (int) – maximum y value
  • max_x (int) – maximum x value
Returns:

cropped landmarks

Return type:

np.ndarray

_crop_to_landmarks(proportion=0.0, **kwargs)[source]

Crop to landmarks

Parameters:
  • proportion (float) – Cropping Proportion
  • **kwargs – additional keyword arguments (ignored here)
Returns:

Cropped Image

Return type:

SingleImage2D

_normalize_rotation(lmks, index_left, index_right, **kwargs)

normalizes rotation based on two keypoints

Parameters:
  • lmks (np.ndarray) – landmarks for rotation normalization
  • index_left (int) – index for left point
  • index_right (int) – index for right point
  • **kwargs – additional keyword arguments (passed to skimage.transform.warp())
Returns:

transformed Image

Return type:

BaseSingleImage

_save_landmarks(filepath, lmk_type, **kwargs)[source]

Saves landmarks to file

Parameters:
  • filepath (str) – path to file the landmarks should be saved to
  • lmk_type (str) – specifies the type of landmark file
  • **kwargs – additional keyword arguments passed to save function
Raises:

ValueError – no valid landmarktype is given

_transform_img(transformation: <sphinx.ext.autodoc.importer._MockObject object at 0x7f8f65aab438>, **kwargs)

Apply transformation inplace to image

Parameters:
Returns:

Transformed Image with original Landmarks

Return type:

BaseSingleImage

_transform_lmk(transformation: <sphinx.ext.autodoc.importer._MockObject object at 0x7f8f65af4748>)[source]

Apply transformation inplace to landmarks

Parameters:transformation (skimage.transform.AffineTransform) – transformation to apply
Returns:Image with Transformed Landmarks
Return type:BaseSingleImage
apply_trafo(transformation: <sphinx.ext.autodoc.importer._MockObject object at 0x7f8f65aab438>, **kwargs)

Apply transformation inplace to image and landmarks

Parameters:
Returns:

Transformed Image and Landmarks

Return type:

BaseSingleImage

cartesian_coordinates()[source]

Transforms landmark coordinates inplace to cartesian coordinates

Returns:class – Image with Landmarks in cartesian Coordinates
Return type:SingleImage2D
crop(min_y, min_x, max_y, max_x)

Crops Image by specified values

Parameters:
  • min_y (int) – minimum y value
  • min_x (int) – minimum x value
  • max_y (int) – maximum y value
  • max_x (int) – maximum x value
Returns:

cropped image

Return type:

BaseSingleImage

crop_to_landmarks(proportion=0.0, **kwargs)

Crop image to landmarks

Parameters:
  • proportion (float) – image proportion to add to size of bounding box
  • **kwargs – additional keyword arguments
Returns:

cropped image

Return type:

BaseSingleImage

classmethod from_files(file, extension=None, **kwargs)

Creates a class instance from files

Parameters:
  • *args – positional arguments
  • **kwargs – keyword arguments
Raises:

NotImplementedError

classmethod from_ljson_files(img_file, **kwargs)[source]

Creates class from menpo pts landmarks and image

Parameters:
  • img_file (str) – image file to load
  • **kwargs – additional keyword arguments
Returns:

class instance

Return type:

SingleImage2D

classmethod from_npy_files(file, **kwargs)[source]

Create class from image file :param file: path to image file :type file: str :param **kwargs: additional keyword arguments

Returns:class instance
Return type:SingleImage2D
classmethod from_pts_files(img_file, **kwargs)[source]

Creates class from menpo ljson landmarks and image

Parameters:
  • img_file (str) – image file to load
  • **kwargs – additional keyword arguments
Returns:

class instance

Return type:

SingleImage2D

static get_landmark_bounds(lmks)

Function to calculate the landmark bounds

Parameters:lmks (np.ndarray) – landmarks
Returns:
  • int (min_y)
  • int (min_x)
  • int (max_y)
  • int (max_x)
homogeneous_coordinates()[source]

Transforms landmark coordinates inplace to homogeneous coordinates

Returns:Image with Landmarks in Homogeneous Coordinates
Return type:SingleImage2D
img

Property to get the actual image pixels

Returns:image pixels
Return type:np.array
is_gray

Property returning whether the image is a grayscale image

Raises:NotImplementedError – if not overwritten by subclass
is_homogeneous

Property returning whether the landmarks are in homogeneous coordinates

Raises:NotImplementedError – if not overwritten by subclass
normalize_rotation(index_left, index_right, **kwargs)[source]

normalizes rotation based on two keypoints

index_left : int
landmark-index of the left point
index_right : int
landmark-index of the right point
**kwargs:
additional keyword arguments (passed to warp())
Returns:normalized image
Return type:SingleImage2D
rescale(scale, **kwargs)

Scale Image and landmarks

Parameters:
  • scale – scale parameter
  • **kwargs – additional keyword arguments (passed to skimage.transform.warp())
Returns:

transformed Image

Return type:

BaseSingleImage

resize(target_shape, **kwargs)

resize image and scale landmarks :param target_shape: target shape for resizing :type target_shape: tuple or list :param **kwargs: additional keyword arguments (passed to

skimage.transform.warp())
Returns:transformed Image
Return type:BaseSingleImage
rotate(angle, degree=True, **kwargs)

Rotates the image and landmarks by given angle

Parameters:
  • angle (float or int) – rotation angle
  • degree (bool) – whether the angle is given in degree or radiant
  • **kwargs – additional keyword arguments (passed to skimage.transform.warp())
Returns:

transformed Image

Return type:

BaseSingleImage

save(directory, filename, lmk_type='LJSON', **kwargs)

Saves Image and optionally landmarks to files

Parameters:
  • directory (str) – string containing the directory to save
  • filename (str) – string containing the filename (without the extension)
  • lmk_type (str or None) – if None: no landmarks will be saved if str: specifies type of landmark file
  • **kwargs – additional keyword arguments passed to save function for landmarks
save_image(filepath)

Saves Image to file

Parameters:filepath (str) – file to save the image to
save_landmarks(filepath, lmk_type='LJSON', **kwargs)

Saves landmarks to file

Parameters:
  • filepath (str) – path to file the landmarks should be saved to
  • lmk_type (str) – specifies the type of landmark file
  • **kwargs – additional keyword arguments passed to save function
to_grayscale()

Convert Image to grayscale

Returns:Grayscale Image
Return type:BaseSingleImage
transform(transform=None, rotation=None, scale=None, translation=None, shear=None, trafo_matrix=None, return_matrix=False, **kwargs)

transform image and landmarks by parameters or transformation matrix See skimage.transform.AffineTransform for a detailed parameter explanation

Parameters:
  • transform (skimage.transform.AffineTransform) – if transform is specified it overwrites all other arguments
  • rotation (float or None) – rotation angle in radiant
  • scale (float or None) – scale value
  • translation – translation params
  • shear – shear params
  • trafo_matrix – transformation matrix
  • return_matrix (bool) – whether to return the transformation matrix along the transformed object
  • **kwargs – additional keyword arguments
Returns:

  • BaseSingleImage – transformed Image
  • [optional] np.ndarray – transformation matrix

transform_about_centre(transform=None, rotation=None, scale=None, translation=None, shear=None, trafo_matrix=None, return_matrix=False, **kwargs)

Perform transformations about the image center. (internally shifting image to origin, perform transformation and shift it back)

Parameters:
  • transform (skimage.transform.AffineTransform) – if transform is specified it overwrites all other arguments
  • rotation (float) – rotation angle in radiant
  • scale (float) – scale value
  • translation – translation params
  • shear – shear params
  • trafo_matrix – transformation matrix
  • return_matrix (bool) – whether to return the transformation matrix along the transformed object
  • **kwargs – additional keyword arguments
Returns:

  • BaseSingleImage – transformed Image
  • [optional] np.ndarray – transformation matrix

translate(translation, relative=False, **kwargs)

translates image and landmarks

translation :
translation parameters
relative : bool
whether translation parameters are relative to image size
**kwargs :
additional keyword arguments (passed to skimage.transform.warp())
BaseSingleImage
transformed Image
view(view_landmarks=False, create_fig=False, **kwargs)[source]

Shows image (and optional the landmarks)

Parameters:
  • view_landmarks (bool) – whether or not to show the landmarks
  • **kwargs – additional keyword arguments (are passed to imshow)
Returns:

figure with plot

Return type:

Figure