Open In App

Tensorflow.js tf.image.cropAndResize() Function

Last Updated : 04 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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.cropAndResize() function is used to take out the outputs from the stated input image tensor as well as rescales them through bilinear sampling or else nearest neighbor sampling to a normal output dimension as stated by crop length.

Syntax:

tf.image.cropAndResize(image, boxes, boxInd, cropSize, 
method?, extrapolationValue?)

Parameters: This method accepts the following parameters:

  • images: The stated 4d tensor, which is of configuration [batch, imageHeight, imageWidth, depth]. Where, imageHeight as well as imageWidth should be positive, defining the group of images from which the crops are to be taken. It can be of type tf.Tensor4D, TypedArray, or Array.
  • boxes: The stated 2d float32 tensor, which is of configuration [numBoxes, 4]. And every access is [y1, x1, y2, x2], allowing that (y1, x1) and (y2, x2) are the standardized coordinates of the box in the boxInd[i] image in the group. It can be of type tf.Tensor2D, TypedArray, or Array.
  • boxInd: The stated 1d int32 tensor, which is of configuration [numBoxes] along with values in the range [0, batch) which defines the image that the i-th box indicates. It is of type tf.Tensor1D, TypedArray, or Array.
  • cropSize: It is the stated 1d int32 tensor that has two elements and is of configuration [cropHeigh, cropWidth] defining the length to which each and every crops are rescaled to. It is of type [number, number].
  • method: It is an optional parameter that defines the sampling method for rescaling. The by default value is bilinear. It can be of type ‘bilinear’, or ‘nearest’.
  • extrapolationValue: It is the stated threshold that is used to conclude at which time to delete boxes on the basis of the stated score. The Default value is zero. It is optional and is of type number.

Return Value: It returns  tf.Tensor4D object.

Example 1: Using a 4d tensor, boxes, boxInd, and cropSize parameters.

Javascript




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


Output:

Tensor
     [ [ [[0, 0],]]]

Example 2: Using an array of floats, method, as well as extrapolationValue.

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]]
]];
  
// Defining boxes with an array of floats
const boxes = [[11.1, 2.3, 7.3, 6.4], [1, 4]];
  
// Calling image.cropAndResize() method and
// Printing output
tf.image.cropAndResize(arr, boxes, [2, 4], [2, 1], 'nearest', 0.4).print();


Output:

Tensor
    [[ [[0, 0, 0, 0],],

       [[0, 0, 0, 0],]],


     [ [[0, 0, 0, 0],],

       [[0, 0, 0, 0],]]]

Reference: https://js.tensorflow.org/api/latest/#image.cropAndResize



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads