API¶
Image¶
-
class
pylapsy.image.Image(input=None, **meta)[source]¶ pylapsy Image class
-
meta¶ dict-like object containing image meta information
Type: ImageMetaData
-
edit_log¶ dict-like object containing editing information
Type: CallableDict
Parameters: - input – input image data (e.g. file path or numpy array)
- **meta – meta information
-
img¶ Image data array
Image data needs to be numpy array, either with 3 or 2 dimensions.
Raises: AttributeError – if no image data is assigned to this object
-
pyrlevel¶ Current pyramid level of image
-
shape¶ shape of image array
-
dtype¶ dtype of image array
-
is_gray¶ Boolean specifying wheter image is gray image.
-
is_color¶ Boolean specifying wheter image is color image.
-
file_path¶ Image file path
-
Image list¶
Created on Sun Sep 1 12:14:02 2019
@author: Jonas
Meta data¶
Deshaker¶
-
class
pylapsy.deshaker.Deshaker(imglist=None, outdir=None)[source]¶ Interface for deshaking a series of images
-
imglist¶ List of images (instance of
pylapsy.ImageList)
-
find_shifts(ref_index=None, parallel=True)[source]¶ Find shifts for all images in
imglistParameters: ref_index (int) – index of reference image in image list (shifts are computed wrt that image) Returns: dictionary containing shifts (dx, dy) and transformation matrices (matrices) for each image in imglist.Return type: dict
-
deshake(outdir=None, ref_index=None, sequence_id=None, save_preview_video=False, parallel=True)[source]¶ Method that deshakes images sequence and saves result
Parameters: - outdir (str, optional) – output directory. If None, a subdirectory will be created in the current directory
- ref_index (int, optional) – Index of reference image in sequence (all images are adjusted wrt to this image, defaults to 0).
- sequence_id (str, optional) – name of the sequence (for output directory)
- save_preview_video (bool) – if True, a preview video is saved (currently not working)
-
deshake_v0(outdir=None, ref_index=None, sequence_id=None, save_images=True, save_preview_video=True, preview_fps=24)[source]¶ Method that deshakes images sequence and saves result
Parameters: - outdir (str, optional) – output directory. If None, a subdirectory will be created in the current directory
- ref_index (int, optional) – Index of reference image in sequence (all images are adjusted wrt to this image, defaults to 0).
- sequence_id (str, optional) – name of the sequence (for output directory)
- save_images (bool) – if True, corrected images are saved
- save_preview_video (bool) – if True, a preview video is saved
- preview_fps (int) – fps of preview video (if applicable)
-
I/O¶
-
pylapsy.io.get_testimg_files_deshake()[source]¶ Get list of images for example deshake sequence
Returns: list with filepaths for example deshake sequence Return type: list
-
pylapsy.io.get_test_img(which=1)[source]¶ Get file path of test image shipped with installation
Test images are stored in pylapsy/data dir and are named test_img<NUM>.jpg <NUM> can be specified via input parameter which.
Parameters: which (int) – number of test image Returns: image file location Return type: str Raises: FileNotFoundError – if installation data directory
Low-level utility methods¶
-
pylapsy.utils.imread(file_path)[source]¶ Read image file using
cv2.imread()Note
opencv reads in BGR mode, not RGB
Parameters: file_path (str) – image file path Returns: image data Return type: ndarray
-
pylapsy.utils.imsave(img_arr, path)[source]¶ Save image files using
cv2.imwrite()Parameters: - img_arr (ndarray) – image data
- path (str) – destination of image
Returns: success or not
Return type:
-
pylapsy.utils.imshow(img_arr, add_cbar=False, cbar_label=None, cmap=None, ax=None, **kwargs)[source]¶ Show image
Works both for grayscale and color images. For color images, it is assumed that the index is ordered in BGR, i.e. that the image was read using
imread()(which usescv2.imread()).Parameters: - img_arr (ndarray) – image data
- add_cbar (bool) – if True, a color bar is added to the figure
- cbar_label (str, optional) – label of colorbar (only relevant if add_cbar is True)
- cmap (str, optional) – colormap that is supposed to be used
- ax (axes) – matplotlib axes instance that is supposed to be used for display
- **kwargs – additional keyword args passed to
imshow()
Returns: Return type: ax
-
pylapsy.utils.to_gray(img_arr)[source]¶ Convert image array to gray
Parameters: img_arr (ndarray) – color image data with color indices in BGR mode (cf. imread()). Shape: (N, M, 3)Returns: img_arr – gray image data (cf. imread()). Shape: (N, M, 1)Return type: ndarray
-
pylapsy.utils.apply_sobel_hor(img_arr, **kwargs)[source]¶ Horizontal sobel filter (wrapper for
cv2.Sobel())Parameters: - img_arr (ndarray) – input grayscale image
- **kwargs – additional keyword args passed to
cv2.Sobel()
Returns: filtered input image array
Return type: ndarray
-
pylapsy.utils.apply_sobel_vert(img_arr, **kwargs)[source]¶ Vertical sobel filter (wrapper for
cv2.Sobel())Parameters: - img_arr (ndarray) – input grayscale image
- **kwargs – additional keyword args passed to
cv2.Sobel()
Returns: filtered input image array
Return type: ndarray
-
pylapsy.utils.apply_sobel_2d(img_arr, **kwargs)[source]¶ Apply 2D sobel filter to in input gray-image
Combines output of
apply_sobel_hor()andapply_sobel_vert()usingcv2.bitwise_or()to retrieve edges in all directions.Parameters: - img_arr (ndarray) – input grayscale image
- **kwargs – additional keyword args passed to
cv2.Sobel()
Returns: filtered input image array
Return type: ndarray
-
pylapsy.utils.find_good_features_to_track(img_arr, plot=False, **params)[source]¶ Wrapper for
cv2.goodFeaturesToTrack()See here for more information.
Parameters: - img_arr (ndarray) – image data from suitable tracking coordinates are supposed to be identified
- plot (bool) – option that plots the detected points onto the input image
- **params – additional input parameters that are passed to
cv2.goodFeaturesToTrack()
Returns: list of coordinates
Return type: ndarray
-
pylapsy.utils.plot_feature_points(points, ax, marker='+', markersize=20, color='r', mew=3)[source]¶ Plot feature points into image
Parameters: - points (ndarray) – feature points either retrieved using
find_good_features_to_track()orcompute_flow_lk() - ax (axes) – matplotlib axes instance in which the points are supposed to be
plotted (e.g. output of
imshow()) - marker (str) – marker that is supposed to be used to plot the points
- markersize (int) – size of markers
- color (str) – color of points
- mew (int) – marker edge width
Returns: Return type: ax
- points (ndarray) – feature points either retrieved using
-
pylapsy.utils.compute_flow_lk(img1, img2, points_to_track=None, **params)[source]¶ Method that computes optical flow using Lucas-Kanade algorithm
Parameters: - img1 (ndarray) – first image
- img2 (ndarray) – next image
- points_to_track (ndarray, optional) – feature points that are used for tracking (e.g. output of
find_good_features_to_track()). Usesfind_good_features_to_track(), if unspecified. - **params – additional keyword args passed to
cv2.calcOpticalFlowPyrLK()
Returns: - ndarray – feature points in img1 that could be used for successful tracking (corresponds to points_to_track)
- ndarray – same points as found in level 2
-
pylapsy.utils.find_affine_partial2d(p0=None, p1=None, **kwargs)[source]¶ Find 2D affine transformation matrix
Find affine transformation matrix for translation and rotation based on input coordinates. Wrapper for method
cv2.estimateAffinePartial2D().Note
Input feature points p0 and p1 can be retrieved from 2 images using method
compute_flow_lk().Parameters: - p0 (ndarray) – coordinates of feature points in first image
- p1 (ndarrax) – coordinates of feature points in next image
- **kwargs – additional keyword args passed to
cv2.estimateAffinePartial2D()
Returns: transformation matrix
Return type: ndarray
-
pylapsy.utils.find_homography(p0=None, p1=None)[source]¶ Find homography matrix
Find homography matrix based on input coordinates. Wrapper for method
cv2.estimateAffinePartial2D().Note
Input feature points p0 and p1 can be retrieved from 2 images using method
compute_flow_lk().Parameters: - p0 (ndarray) – coordinates of feature points in first image
- p1 (ndarrax) – coordinates of feature points in next image
- **kwargs – additional keyword args passed to
cv2.estimateAffinePartial2D()
Returns: transformation matrix
Return type: ndarray
-
pylapsy.utils.find_shift(first_gray, second_gray, **feature_lk_params)[source]¶ Find shift between two input images using lukas kanade optical flow
Detects shift at suitable points to track in both images (e.g. corners) and based on detected shifts, finds the affine transformation that can be used to shift and rotate the second image such that it matches best the first image
Parameters: - first_gray (ndarray) – first image (gray scale)
- second_gray (ndarray) – second image
- **feature_lk_params – additional, optional input keyword args passed to
compute_flow_lk(). Default settings for lukas kanade can be found indefaults
Returns: - tuple – (dx, dy) shift
- float – rotation angle
- ndarray – affine transformation matrix
-
pylapsy.utils.to_pylapsy_image(input)[source]¶ Convert input to instance of pylapsy.Image class
Accepts valid image file path or numpy array
-
pylapsy.utils.get_crop(dx, dy, w0, h0)[source]¶ Get crop ROI based on shift (dx, dy) and original image size
- dx : float or ndarray
- x shift or list of x shifts (for batch processing)
- dy : float or ndarray
- y shift or list of y shifts
- w0 : int
- original image width
- h0 : int
- original image height
Returns: 4-element tuple containing ROI: (x0, x1, y0, y1) Return type: tuple
High level scripts¶
-
pylapsy.highlevel_methods.deshake(dir_name=None, file_pattern=None, outdir=None, **deshake_args)[source]¶ Deshake image sequence
Applies deshaking to all images (which should be part of a timelapse sequence) and saves the corrected images in provided output directory. By default, also a preview video is created. For details see
Deshaker.Parameters: - dir_name (str, optional) – Image directory. If None, the current directory is used. The default is None.
- file_pattern (str, optional) – Pattern used to identify images in the input directory (e.g. *.jpg). The default is None.
- outdir (str, optional) – Output. The default is None.
- **deshake_args – Additional keyword args passed to
Deshaker.deshake()
CLI¶
Command line interface of timelapsy
usage: ply [-h] [-d DIR] [-o OUTDIR] [--file_pattern FILE_PATTERN] task
Positional Arguments¶
| task | Processing task that is supposed to be performed. Choose from: [‘deshake’, ‘info’] |
Named Arguments¶
| -d, --dir | Input directory containing timelapse sequence. Uses “.” if unspecified Default: “.” |
| -o, --outdir | Output directory for processed data. If unspecified a subdirectory “pylapsy_out” is created in current directory. |
| --file_pattern | Filename pattern used to identify image files (e.g. *.jpg) Default: “*” |