mt.opencv.imgres
A module to represent an image resolution to avoid misuse of cv2.Size and numpy.ndarray’s shape.
An image resolution (imgres) is defined as a list [width, height], so that it can be smoothly serialized using yaml. There are some functions in this module to convert between imgres and common image resolutions types like equivalents of OpenCV’s size format or a part of a row-major numpy.ndarray’s shape.
A few common resolutions are used here. The reference for resolutions is: https://en.wikipedia.org/wiki/List_of_common_resolutions
For CIF-based resolutions, see https://en.wikipedia.org/wiki/Low-definition_television
Functions
equal()
: Checks if two imgresses are equal or not.imgres2size()
: Converts an imgres into an equivalent OpenCV’s size.imgres2shape()
: Converts an imgres into an int tuple that can be used to define a row-major numpy.ndarray.size2imgres()
: Converts an equivalent of OpenCV’s size into imgres.shape2imgres()
: Converts a part of a row-major numpy.ndarray’s shape into imgres.aspect_ratio()
: Gets the aspect ratio of the resolution.get_center_window()
: Returns a center window of the source image that preserves the width-over-height aspect ratio.get_center_window_tfm()
: Returns a 2D affine transformation that maps pixels in a source image to pixels in a destination image that reflects a center window of the source image.get_center_window_tfm_tf()
: Tensorflow version ofget_center_window_tfm()
.make_thumbnail()
: Makes a thumbnail out of an image.get_thumbnail_imgres()
: Gets the thumbnail resolution from the raw imgres.
- mt.opencv.imgres.equal(imgres1, imgres2)
Checks if two imgresses are equal or not.
- mt.opencv.imgres.imgres2size(imgres)
Converts an imgres into an equivalent OpenCV’s size.
- mt.opencv.imgres.imgres2shape(imgres)
Converts an imgres into an int tuple that can be used to define a row-major numpy.ndarray.
- mt.opencv.imgres.size2imgres(size)
Converts an equivalent of OpenCV’s size into imgres.
- mt.opencv.imgres.shape2imgres(shape)
Converts a part of a row-major numpy.ndarray’s shape into imgres.
- mt.opencv.imgres.aspect_ratio(imgres)
Gets the aspect ratio of the resolution.
- mt.opencv.imgres.get_center_window(aspect_ratio, src_imgres, alpha=1.0)
Returns a center window of the source image that preserves the width-over-height aspect ratio.
The center window is defined in the following:
Its center is the center of the source image.
Its width is not more than the source width times alpha.
Its height is not more than the source height times alpha.
Its width-over-height aspect ratio is the same as the provided aspect ratio.
It is as large as possible.
- Parameters:
aspect_ratio (float) – the input width-over-height aspect ratio
src_imgres (list) – pair of [width, height] of the source image resolution
alpha (float) – a positive scalar telling how large the window can be
- Returns:
rect – the output center window
- Return type:
- mt.opencv.imgres.get_center_window_tfm(dst_imgres, src_imgres, alpha=1.0)
Returns a 2D affine transformation that maps pixels in a source image to pixels in a destination image that reflects a center window of the source image.
The center window is defined in the following:
Its center is the center of the source image.
Its width is not more than the source width times alpha.
Its height is not more than the source height times alpha.
Its aspect ratio is the same as that of the destination image resolution.
It is as large as possible.
The function returns a transformation. One can use a warping function to warp the image and then crop using the destination imgres.
- Parameters:
dst_imgres (list) – pair of [width, height] of the destination image resolution
src_imgres (list) – pair of [width, height] of the source image resolution
alpha (float) – a positive scalar telling how large the window can be
- Returns:
tfm – output 2D transformation
- Return type:
Deprecated since version 1.9: It will be removed in version 2.0. Use
mt.cv.ImgCrop()
instead.
- mt.opencv.imgres.get_center_window_tfm_tf(dst_shape, src_shape, alpha=1.0)
Tensorflow version of
get_center_window_tfm()
.Unlike the original function, the function inputs and outputs tensors. Instead of imgreses, the function inputs shapes.
- Parameters:
dst_shape (tensorflow.Tensor or list) – pair of [height, width] of the destination image resolution
src_shape (tensorflow.Tensor or list) – pair of [height, width] of the source image resolution
alpha (tensorflow.Tensor or scalar) – a positive scalar telling how large the window can be
- Returns:
tfm – output 3x3 matrix representing the 2D affine transformation mapping from source center window to destination image. The 3x3 matrix can be used in
tensorflow_graphics.image.transformer.perspective_transform()
.- Return type:
tensorflow.Tensor
Deprecated since version 1.9: It will be removed in version 2.0. Use
mt.cv.ImgCrop()
instead.
- mt.opencv.imgres.make_thumbnail(image: ndarray, large: bool = False, pixel_format: str = 'rgb', extra_meta: dict = {}) Image
Makes a thumbnail out of an image.
Only images of aspect ratio 4:3 or 16:9 are accepted. The thumbnail of a 4:3 image will be of resolution ‘cif’ for normal thumbnails and ‘pal43’ for large thumbnails. The thumbnail of a 16:9 image will be of resolution ‘ws_cif’ for normal thumbnails and ‘pal169’ for large thumbnails. See attribute name2imgres of the module for more details.
- Parameters:
image (np.ndarray) – an image of shape (H, W, D) where 1 <= D <= 4
large (bool) – whether or not to make a large thumbnail
pixel_format (str) – pixel format. To be passed as-is to
mt.opencv.image.Image
extra_meta (dict, optional) – extra metadata for the image. To be passed as-is to
mt.opencv.image.Image
- Returns:
another image of shape (288//A, 288, D) with metadata, where A is the aspect ratio. The metadata of theimage contains key ‘src_imgres’ telling the resolution of the original image, plus any metadata provided by the extra_meta dictionary.
- Return type:
- mt.opencv.imgres.get_thumbnail_imgres(raw_imgres: list, large: bool = False) list
Gets the thumbnail resolution from the raw imgres.
- Parameters:
raw_imgres (list) – pair [width, height] representing the raw image resolution
large (bool) – whether or not to make a large thumbnail
- Returns:
thumb_imgres – pair [width, height] representing the resolution of the thumbnail
- Return type:
list
Variables
- mt.opencv.imgres.name2imgres
dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s
(key, value) pairs
- dict(iterable) -> new dictionary initialized as if via:
d = {} for k, v in iterable:
d[k] = v
- dict(**kwargs) -> new dictionary initialized with the name=value pairs
in the keyword argument list. For example: dict(one=1, two=2)
{'cga': [320, 200], 'cif': [384, 288], 'fhd': [1920, 1280], 'hqvga': [240, 160], 'nintendo_ds': [256, 192], 'pal169': [1024, 576], 'pal43': [768, 576], 'qqvga': [160, 120], 'qvga': [320, 240], 'sony_smartwatch': [128, 128], 'uxga': [1600, 1200], 'ws_cif': [512, 288]}