blob: 687f8f041504190be475fceecb44c2399647a9ba [file]
#-------------------------------------------------------------
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
#-------------------------------------------------------------
# The Image Translate function translates the image.
# Optionally resizes the image (without scaling).
# Uses nearest neighbor sampling.
#
# .. code-block:: python
#
# >>> import numpy as np
# >>> from systemds.context import SystemDSContext
# >>> from systemds.operator.algorithm import img_translate
# >>>
# >>> with SystemDSContext() as sds:
# ... img = sds.from_numpy(
# ... np.array([[ 10., 20., 30.],
# ... [ 40., 50., 60.],
# ... [ 70., 80., 90.]], dtype=np.float32)
# ... )
# ... result_img = img_translate(img, 1., 1., 3, 3, 255.).compute()
# ... print(result_img)
# [[255. 255. 255.]
# [255. 10. 20.]
# [255. 40. 50.]]
#
#
# INPUT:
# ----------------------------------------------------------------------------------------------
# img_in Input image as 2D matrix with top left corner at [1, 1]
# offset_x The distance to move the image in x direction
# offset_y The distance to move the image in y direction
# out_w Width of the output image
# out_h Height of the output image
# fill_value The background of the image
# ----------------------------------------------------------------------------------------------
#
# OUTPUT:
# --------------------------------------------------------------------------------------------
# img_out Output image as 2D matrix with top left corner at [1, 1]
# --------------------------------------------------------------------------------------------
m_img_translate = function(Matrix[Double] img_in, Double offset_x, Double offset_y, Integer out_w, Integer out_h, Double fill_value)
return (Matrix[Double] img_out) {
w = ncol(img_in)
h = nrow(img_in)
# round to nearest integer as fraction is irrelevant for nearest neighbor sampling
offset_x = round(offset_x)
offset_y = round(offset_y)
# index range in input image
start_x = 1 - offset_x
start_y = 1 - offset_y
end_x = max(w , out_w) - offset_x
end_y = max(h, out_h) - offset_y
# clip copy region to left and top of input image
if (start_x < 1)
start_x = 1
if (start_y < 1)
start_y = 1
# clip copy region to right and bottom of input image
if (w < end_x)
end_x = w
if (h < end_y)
end_y = h
# clip copy region to right and bottom of output image
if (out_w < end_x + offset_x)
end_x = out_w - offset_x
if (out_h < end_y + offset_y)
end_y = out_h - offset_y
img_out = matrix(fill_value, rows=out_h, cols=out_w)
if (start_x < end_x & start_y < end_y) {
img_out[(start_y+offset_y):(end_y+offset_y), (start_x+offset_x):(end_x+offset_x)] = img_in[start_y:end_y, start_x:end_x]
}
}