vkit.mechanism.distortion.geometric.grid_rendering.grid_creator

  1# Copyright 2022 vkit-x Administrator. All Rights Reserved.
  2#
  3# This project (vkit-x/vkit) is dual-licensed under commercial and SSPL licenses.
  4#
  5# The commercial license gives you the full rights to create and distribute software
  6# on your own terms without any SSPL license obligations. For more information,
  7# please see the "LICENSE_COMMERCIAL.txt" file.
  8#
  9# This project is also available under Server Side Public License (SSPL).
 10# The SSPL licensing is ideal for use cases such as open source projects with
 11# SSPL distribution, student/academic purposes, hobby projects, internal research
 12# projects without external distribution, or other projects where all SSPL
 13# obligations can be met. For more information, please see the "LICENSE_SSPL.txt" file.
 14from typing import List
 15from itertools import chain
 16
 17from vkit.element import Point, PointList
 18from .type import ImageGrid
 19from .point_projector import PointProjector
 20
 21
 22def create_src_image_grid(height: int, width: int, grid_size: int):
 23    ys = list(range(0, height, grid_size))
 24    if ys[-1] != height - 1:
 25        ys.append(height - 1)
 26
 27    xs = list(range(0, width, grid_size))
 28    if xs[-1] != width - 1:
 29        xs.append(width - 1)
 30
 31    points_2d = []
 32    for y in ys:
 33        points = []
 34        for x in xs:
 35            points.append(Point.create(y=y, x=x))
 36        points_2d.append(points)
 37
 38    return ImageGrid(
 39        points_2d=points_2d,
 40        grid_size=grid_size,
 41    )
 42
 43
 44def create_dst_image_grid_and_shift_amounts_and_resize_ratios(
 45    src_image_grid: ImageGrid,
 46    point_projector: PointProjector,
 47    resize_as_src: bool = True,
 48):
 49    dst_points_2d = []
 50
 51    src_flatten_points = src_image_grid.flatten_points
 52    num_src_flatten_points = len(src_flatten_points)
 53
 54    dst_flatten_points = point_projector.project_points(src_flatten_points)
 55
 56    assert len(dst_flatten_points) == num_src_flatten_points
 57    dst_points_2d: List[PointList] = []
 58    for begin in range(0, num_src_flatten_points, src_image_grid.num_cols):
 59        dst_points_2d.append(PointList(dst_flatten_points[begin:begin + src_image_grid.num_cols]))
 60
 61    y_min = dst_points_2d[0][0].y
 62    y_max = y_min
 63    x_min = dst_points_2d[0][0].x
 64    x_max = x_min
 65
 66    for point in chain.from_iterable(dst_points_2d):
 67        y_min = min(y_min, point.y)
 68        y_max = max(y_max, point.y)
 69        x_min = min(x_min, point.x)
 70        x_max = max(x_max, point.x)
 71
 72    shift_amount_y = y_min
 73    shift_amount_x = x_min
 74
 75    for row_idx in range(src_image_grid.num_rows):
 76        for col_idx in range(src_image_grid.num_cols):
 77            point = dst_points_2d[row_idx][col_idx]
 78            dst_points_2d[row_idx][col_idx] = point.to_shifted_point(
 79                offset_y=-shift_amount_y,
 80                offset_x=-shift_amount_x,
 81            )
 82
 83    src_image_height = src_image_grid.image_height
 84    src_image_width = src_image_grid.image_width
 85
 86    resize_ratio_y = 1.0
 87    resize_ratio_x = 1.0
 88
 89    if resize_as_src:
 90        raw_dst_image_grid = ImageGrid(points_2d=dst_points_2d)
 91
 92        resize_ratio_y = src_image_height / raw_dst_image_grid.image_height
 93        resize_ratio_x = src_image_width / raw_dst_image_grid.image_width
 94
 95        raw_dst_image_grid_image_shape = raw_dst_image_grid.image_shape
 96        del raw_dst_image_grid
 97
 98        for row_idx in range(src_image_grid.num_rows):
 99            for col_idx in range(src_image_grid.num_cols):
100                point = dst_points_2d[row_idx][col_idx]
101                dst_points_2d[row_idx][col_idx] = point.to_conducted_resized_point(
102                    raw_dst_image_grid_image_shape,
103                    resized_height=src_image_height,
104                    resized_width=src_image_width,
105                )
106
107    dst_image_grid = ImageGrid(points_2d=dst_points_2d)
108
109    if resize_as_src:
110        assert dst_image_grid.image_height == src_image_height
111        assert dst_image_grid.image_width == src_image_width
112
113    shift_amounts = (shift_amount_y, shift_amount_x)
114    resize_ratios = (resize_ratio_y, resize_ratio_x)
115    return dst_image_grid, shift_amounts, resize_ratios
116
117
118def create_dst_image_grid(
119    src_image_grid: ImageGrid,
120    point_projector: PointProjector,
121    resize_as_src: bool = True,
122):
123    dst_image_grid, _, _ = create_dst_image_grid_and_shift_amounts_and_resize_ratios(
124        src_image_grid=src_image_grid,
125        point_projector=point_projector,
126        resize_as_src=resize_as_src,
127    )
128    return dst_image_grid
def create_src_image_grid(height: int, width: int, grid_size: int):
23def create_src_image_grid(height: int, width: int, grid_size: int):
24    ys = list(range(0, height, grid_size))
25    if ys[-1] != height - 1:
26        ys.append(height - 1)
27
28    xs = list(range(0, width, grid_size))
29    if xs[-1] != width - 1:
30        xs.append(width - 1)
31
32    points_2d = []
33    for y in ys:
34        points = []
35        for x in xs:
36            points.append(Point.create(y=y, x=x))
37        points_2d.append(points)
38
39    return ImageGrid(
40        points_2d=points_2d,
41        grid_size=grid_size,
42    )
def create_dst_image_grid_and_shift_amounts_and_resize_ratios( src_image_grid: vkit.mechanism.distortion.geometric.grid_rendering.type.ImageGrid, point_projector: vkit.mechanism.distortion.geometric.grid_rendering.point_projector.PointProjector, resize_as_src: bool = True):
 45def create_dst_image_grid_and_shift_amounts_and_resize_ratios(
 46    src_image_grid: ImageGrid,
 47    point_projector: PointProjector,
 48    resize_as_src: bool = True,
 49):
 50    dst_points_2d = []
 51
 52    src_flatten_points = src_image_grid.flatten_points
 53    num_src_flatten_points = len(src_flatten_points)
 54
 55    dst_flatten_points = point_projector.project_points(src_flatten_points)
 56
 57    assert len(dst_flatten_points) == num_src_flatten_points
 58    dst_points_2d: List[PointList] = []
 59    for begin in range(0, num_src_flatten_points, src_image_grid.num_cols):
 60        dst_points_2d.append(PointList(dst_flatten_points[begin:begin + src_image_grid.num_cols]))
 61
 62    y_min = dst_points_2d[0][0].y
 63    y_max = y_min
 64    x_min = dst_points_2d[0][0].x
 65    x_max = x_min
 66
 67    for point in chain.from_iterable(dst_points_2d):
 68        y_min = min(y_min, point.y)
 69        y_max = max(y_max, point.y)
 70        x_min = min(x_min, point.x)
 71        x_max = max(x_max, point.x)
 72
 73    shift_amount_y = y_min
 74    shift_amount_x = x_min
 75
 76    for row_idx in range(src_image_grid.num_rows):
 77        for col_idx in range(src_image_grid.num_cols):
 78            point = dst_points_2d[row_idx][col_idx]
 79            dst_points_2d[row_idx][col_idx] = point.to_shifted_point(
 80                offset_y=-shift_amount_y,
 81                offset_x=-shift_amount_x,
 82            )
 83
 84    src_image_height = src_image_grid.image_height
 85    src_image_width = src_image_grid.image_width
 86
 87    resize_ratio_y = 1.0
 88    resize_ratio_x = 1.0
 89
 90    if resize_as_src:
 91        raw_dst_image_grid = ImageGrid(points_2d=dst_points_2d)
 92
 93        resize_ratio_y = src_image_height / raw_dst_image_grid.image_height
 94        resize_ratio_x = src_image_width / raw_dst_image_grid.image_width
 95
 96        raw_dst_image_grid_image_shape = raw_dst_image_grid.image_shape
 97        del raw_dst_image_grid
 98
 99        for row_idx in range(src_image_grid.num_rows):
100            for col_idx in range(src_image_grid.num_cols):
101                point = dst_points_2d[row_idx][col_idx]
102                dst_points_2d[row_idx][col_idx] = point.to_conducted_resized_point(
103                    raw_dst_image_grid_image_shape,
104                    resized_height=src_image_height,
105                    resized_width=src_image_width,
106                )
107
108    dst_image_grid = ImageGrid(points_2d=dst_points_2d)
109
110    if resize_as_src:
111        assert dst_image_grid.image_height == src_image_height
112        assert dst_image_grid.image_width == src_image_width
113
114    shift_amounts = (shift_amount_y, shift_amount_x)
115    resize_ratios = (resize_ratio_y, resize_ratio_x)
116    return dst_image_grid, shift_amounts, resize_ratios
def create_dst_image_grid( src_image_grid: vkit.mechanism.distortion.geometric.grid_rendering.type.ImageGrid, point_projector: vkit.mechanism.distortion.geometric.grid_rendering.point_projector.PointProjector, resize_as_src: bool = True):
119def create_dst_image_grid(
120    src_image_grid: ImageGrid,
121    point_projector: PointProjector,
122    resize_as_src: bool = True,
123):
124    dst_image_grid, _, _ = create_dst_image_grid_and_shift_amounts_and_resize_ratios(
125        src_image_grid=src_image_grid,
126        point_projector=point_projector,
127        resize_as_src=resize_as_src,
128    )
129    return dst_image_grid