vkit.mechanism.distortion.geometric.grid_rendering.grid_blender
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. 14import numpy as np 15import cv2 as cv 16 17from vkit.element import ( 18 Image, 19 ImageMode, 20 ScoreMap, 21 Mask, 22) 23from .type import ImageGrid 24 25 26def create_image_from_image_grid(image_grid: ImageGrid, image_mode: ImageMode): 27 ndim = image_mode.to_ndim() 28 if ndim == 2: 29 shape = (image_grid.image_height, image_grid.image_width) 30 elif ndim == 3: 31 num_channels = image_mode.to_num_channels() 32 assert num_channels 33 shape = (image_grid.image_height, image_grid.image_width, num_channels) 34 else: 35 raise NotImplementedError() 36 37 dtype = image_mode.to_dtype() 38 mat = np.zeros(shape, dtype=dtype) 39 return Image(mat=mat, mode=image_mode) 40 41 42def create_score_map_from_image_grid(image_grid: ImageGrid): 43 shape = (image_grid.image_height, image_grid.image_width) 44 mat = np.zeros(shape, dtype=np.float32) 45 return ScoreMap(mat=mat) 46 47 48def create_mask_from_image_grid(image_grid: ImageGrid): 49 shape = (image_grid.image_height, image_grid.image_width) 50 mat = np.zeros(shape, dtype=np.uint8) 51 return Mask(mat=mat) 52 53 54def blend_src_to_dst_image( 55 src_image: Image, 56 src_image_grid: ImageGrid, 57 dst_image_grid: ImageGrid, 58): 59 map_y, map_x = src_image_grid.generate_remap_params(dst_image_grid) 60 dst_image_mat = cv.remap(src_image.mat, map_x, map_y, cv.INTER_LINEAR) 61 return Image(mat=dst_image_mat, mode=src_image.mode) 62 63 64def blend_src_to_dst_score_map( 65 src_score_map: ScoreMap, 66 src_image_grid: ImageGrid, 67 dst_image_grid: ImageGrid, 68): 69 map_y, map_x = src_image_grid.generate_remap_params(dst_image_grid) 70 dst_score_map_mat = cv.remap(src_score_map.mat, map_x, map_y, cv.INTER_LINEAR) 71 return ScoreMap(mat=dst_score_map_mat) 72 73 74def blend_src_to_dst_mask( 75 src_mask: Mask, 76 src_image_grid: ImageGrid, 77 dst_image_grid: ImageGrid, 78): 79 map_y, map_x = src_image_grid.generate_remap_params(dst_image_grid) 80 dst_mask_mat = cv.remap(src_mask.mat, map_x, map_y, cv.INTER_LINEAR) 81 return Mask(mat=dst_mask_mat)
def
create_image_from_image_grid( image_grid: vkit.mechanism.distortion.geometric.grid_rendering.type.ImageGrid, image_mode: vkit.element.image.ImageMode):
27def create_image_from_image_grid(image_grid: ImageGrid, image_mode: ImageMode): 28 ndim = image_mode.to_ndim() 29 if ndim == 2: 30 shape = (image_grid.image_height, image_grid.image_width) 31 elif ndim == 3: 32 num_channels = image_mode.to_num_channels() 33 assert num_channels 34 shape = (image_grid.image_height, image_grid.image_width, num_channels) 35 else: 36 raise NotImplementedError() 37 38 dtype = image_mode.to_dtype() 39 mat = np.zeros(shape, dtype=dtype) 40 return Image(mat=mat, mode=image_mode)
def
create_score_map_from_image_grid( image_grid: vkit.mechanism.distortion.geometric.grid_rendering.type.ImageGrid):
def
create_mask_from_image_grid( image_grid: vkit.mechanism.distortion.geometric.grid_rendering.type.ImageGrid):
def
blend_src_to_dst_image( src_image: vkit.element.image.Image, src_image_grid: vkit.mechanism.distortion.geometric.grid_rendering.type.ImageGrid, dst_image_grid: vkit.mechanism.distortion.geometric.grid_rendering.type.ImageGrid):
55def blend_src_to_dst_image( 56 src_image: Image, 57 src_image_grid: ImageGrid, 58 dst_image_grid: ImageGrid, 59): 60 map_y, map_x = src_image_grid.generate_remap_params(dst_image_grid) 61 dst_image_mat = cv.remap(src_image.mat, map_x, map_y, cv.INTER_LINEAR) 62 return Image(mat=dst_image_mat, mode=src_image.mode)
def
blend_src_to_dst_score_map( src_score_map: vkit.element.score_map.ScoreMap, src_image_grid: vkit.mechanism.distortion.geometric.grid_rendering.type.ImageGrid, dst_image_grid: vkit.mechanism.distortion.geometric.grid_rendering.type.ImageGrid):
65def blend_src_to_dst_score_map( 66 src_score_map: ScoreMap, 67 src_image_grid: ImageGrid, 68 dst_image_grid: ImageGrid, 69): 70 map_y, map_x = src_image_grid.generate_remap_params(dst_image_grid) 71 dst_score_map_mat = cv.remap(src_score_map.mat, map_x, map_y, cv.INTER_LINEAR) 72 return ScoreMap(mat=dst_score_map_mat)
def
blend_src_to_dst_mask( src_mask: vkit.element.mask.Mask, src_image_grid: vkit.mechanism.distortion.geometric.grid_rendering.type.ImageGrid, dst_image_grid: vkit.mechanism.distortion.geometric.grid_rendering.type.ImageGrid):