Related Articles

# Tensorflow.js tf.image.transform() Function

• Last Updated : 04 Aug, 2021

Tensorflow.js is an open-source library developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The .image.transform() function is used to apply the specified transform(s) to the image(s).

Syntax:

```tf.image.transform(image, transforms, interpolation?, fillMode?,
fillValue?, outputShape?)```

Parameters: This method accepts the following parameters:

• images: The stated 4d tensor, which is of configuration [batch, imageHeight, imageWidth, depth]. It can be of type tf.Tensor4D, TypedArray, or Array.
• transforms: The stated projective transform matrix or matrices. It is a 1d tensor of range 8 or else tensor of dimension N x 8. In case, a single row of transforms is [a0, a1, a2, b0 b1, b2, c0, c1], subsequently it plots the output point i.e. (x, y) into a modified input point i.e. (x’, y’) = ((a0 x + a1 y + a2) / k, (b0 x + b1 y + b2) / k), where k = c0 x + c1 y + 1. Moreover, the transforms are reversed in comparison to the transform plotting input points to the output points.
• interpolation: It is the stated interpolation mode. It can be of type ‘nearest’, or ‘bilinear’. It is optional and the by default value is ‘nearest’.
• fillMode: Here, the stated points beyond the margins of the input are filled in accordance with the stated mode. The mode is optional and can be of type ‘constant’, ‘reflect’, ‘wrap’, or ‘nearest’. The by default value is ‘constant’. In the ‘reflect‘ mode i.e. (d c b a | a b c d | d c b a ), the input is elongated by meditating around the verge of the utmost pixel. In the ‘constant‘ mode i.e. (k k k k | a b c d | k k k k), the input is elongated by stuffing every values beyond the verge along with the identical constant value k. In ‘wrap‘ mode i.e. (a b c d | a b c d | a b c d), the input is elongated by enclosing about the opposite edge. In ‘nearest‘ mode i.e. (a a a a | a b c d | d d d d), the input is elongated by the closest pixel.
• fillValue: It is a float that depicts the value which is to be filled beyond the edges, if the stated fillMode is ‘constant‘. It is optional and is of type number.
• outputShape: It is optional and is of type [number, number].

Return Value: It returns tf.Tensor4D object.

Example 1: Using a 4d tensor and a 2d tensor i.e. transforms.

## Javascript

 `// Importing the tensorflow.js library``const tf = require(``"@tensorflow/tfjs"``)`` ` `// Calling image.transform() method and``// Printing output``tf.image.transform(tf.tensor4d([[`` ` `  ``[[4, 7], [21, 9]],`` ` `  ``[[8, 9], [1, 33]]`` ` `]]), tf.tensor2d([1, 2, 3, 4, 5, 6, 7, 8], [1, 8])).print();`

Output:

```Tensor
[[[[0, 0 ],
[1, 33]],

[[1, 33],
[8, 9 ]]]]```

Example 2: Using an array of floats, interpolation, fillMode, fillValue, as well as outputShape.

## Javascript

 `// Importing the tensorflow.js library``const tf = require(``"@tensorflow/tfjs"``)`` ` `// Defining an array of floats``const arr = [[``  ``[[1.1, 1.7, 1.5, 1.1], ``  ``[1.7, 1.9, 8.1, 6.3]],``  ``[[3.3, 3.4, 3.7, 4.0], ``  ``[5.1, 5.2, 5.3, 5.9]]``]];`` ` `// Calling image.transform() method and``// Printing output``tf.image.transform(arr, tf.tensor2d(``    ``[1.3, 2.4, 3.5, 4.5, 5.1, 6.0, 7.2, 8.5], ``    ``[1, 8]),``'bilinear'``, ``'reflect'``, 2, [2, 2])``    ``.print();`

Output:

```Tensor
[[[[3.3      , 3.4000001, 3.7      , 4        ],
[4.3536587, 4.4536586, 4.6365857, 5.112195 ]],

[[4.4178948, 4.5178947, 4.6936841, 5.1800003],
[3.8970597, 4.0186343, 4.3869019, 4.721858 ]]]]```

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!

My Personal Notes arrow_drop_up