# Cropping

### tf.image.resize_image_with_crop_or_pad(image, target_height, target_width)

Crops and/or pads an image to a target width and height.

Resizes an image to a target width and height by either centrally cropping the image or padding it evenly with zeros.

If width or height is greater than the specified target_width or target_height respectively, this op centrally crops along that dimension. If width or height is smaller than the specified target_width or target_height respectively, this op centrally pads with 0 along that dimension.

##### Args:
• image: 3-D tensor of shape [height, width, channels]
• target_height: Target height.
• target_width: Target width.
##### Raises:
• ValueError: if target_height or target_width are zero or negative.
##### Returns:

Cropped and/or padded image of shape [target_height, target_width, channels]

### tf.image.central_crop(image, central_fraction)

Crop the central region of the image.

Remove the outer parts of an image but retain the central region of the image along each dimension. If we specify central_fraction = 0.5, this function returns the region marked with "X" in the below diagram.

 --------
|        |
|  XXXX  |
|  XXXX  |
|        |   where "X" is the central 50% of the image.
--------

##### Args:
• image: 3-D float Tensor of shape [height, width, depth]
• central_fraction: float (0, 1], fraction of size to crop
##### Raises:
• ValueError: if central_crop_fraction is not within (0, 1].

3-D float Tensor

### tf.image.pad_to_bounding_box(image, offset_height, offset_width, target_height, target_width)

Pad image with zeros to the specified height and width.

Adds offset_height rows of zeros on top, offset_width columns of zeros on the left, and then pads the image on the bottom and right with zeros until it has dimensions target_height, target_width.

This op does nothing if offset_* is zero and the image already has size target_height by target_width.

##### Args:
• image: 3-D tensor with shape [height, width, channels]
• offset_height: Number of rows of zeros to add on top.
• offset_width: Number of columns of zeros to add on the left.
• target_height: Height of output image.
• target_width: Width of output image.
##### Returns:

3-D tensor of shape [target_height, target_width, channels]

##### Raises:
• ValueError: If the shape of image is incompatible with the offset_* or target_* arguments, or either offset_height or offset_width is negative.

### tf.image.crop_to_bounding_box(image, offset_height, offset_width, target_height, target_width)

Crops an image to a specified bounding box.

This op cuts a rectangular part out of image. The top-left corner of the returned image is at offset_height, offset_width in image, and its lower-right corner is at offset_height + target_height, offset_width + target_width.

##### Args:
• image: 3-D tensor with shape [height, width, channels]
• offset_height: Vertical coordinate of the top-left corner of the result in the input.
• offset_width: Horizontal coordinate of the top-left corner of the result in the input.
• target_height: Height of the result.
• target_width: Width of the result.
##### Returns:

3-D tensor of image with shape [target_height, target_width, channels]

##### Raises:
• ValueError: If the shape of image is incompatible with the offset_* or target_* arguments, or either offset_height or offset_width is negative, or either target_height or target_width is not positive.

### tf.image.extract_glimpse(input, size, offsets, centered=None, normalized=None, uniform_noise=None, name=None)

Extracts a glimpse from the input tensor.

Returns a set of windows called glimpses extracted at location offsets from the input tensor. If the windows only partially overlaps the inputs, the non overlapping areas will be filled with random noise.

The result is a 4-D tensor of shape [batch_size, glimpse_height, glimpse_width, channels]. The channels and batch dimensions are the same as that of the input tensor. The height and width of the output windows are specified in the size parameter.

The argument normalized and centered controls how the windows are built:

• If the coordinates are normalized but not centered, 0.0 and 1.0 correspond to the minimum and maximum of each height and width dimension.
• If the coordinates are both normalized and centered, they range from -1.0 to 1.0. The coordinates (-1.0, -1.0) correspond to the upper left corner, the lower right corner is located at (1.0, 1.0) and the center is at (0, 0).
• If the coordinates are not normalized they are interpreted as numbers of pixels.
##### Args:
• input: A Tensor of type float32. A 4-D float tensor of shape [batch_size, height, width, channels].
• size: A Tensor of type int32. A 1-D tensor of 2 elements containing the size of the glimpses to extract. The glimpse height must be specified first, following by the glimpse width.
• offsets: A Tensor of type float32. A 2-D integer tensor of shape [batch_size, 2] containing the x, y locations of the center of each window.
• centered: An optional bool. Defaults to True. indicates if the offset coordinates are centered relative to the image, in which case the (0, 0) offset is relative to the center of the input images. If false, the (0,0) offset corresponds to the upper left corner of the input images.
• normalized: An optional bool. Defaults to True. indicates if the offset coordinates are normalized.
• uniform_noise: An optional bool. Defaults to True. indicates if the noise should be generated using a uniform distribution or a gaussian distribution.
• name: A name for the operation (optional).
##### Returns:

A Tensor of type float32. A tensor representing the glimpses [batch_size, glimpse_height, glimpse_width, channels].

### tf.image.crop_and_resize(image, boxes, box_ind, crop_size, method=None, extrapolation_value=None, name=None)

Extracts crops from the input image tensor and bilinearly resizes them (possibly

with aspect ratio change) to a common output size specified by crop_size. This is more general than the crop_to_bounding_box op which extracts a fixed size slice from the input image and does not allow resizing or aspect ratio change.

Returns a tensor with crops from the input image at positions defined at the bounding box locations in boxes. The cropped boxes are all resized (with bilinear interpolation) to a fixed size = [crop_height, crop_width]. The result is a 4-D tensor [num_boxes, crop_height, crop_width, depth].

##### Args:
• image: A Tensor. Must be one of the following types: uint8, int8, int16, int32, int64, half, float32, float64. A 4-D tensor of shape [batch, image_height, image_width, depth]. Both image_height and image_width need to be positive.
• boxes: A Tensor of type float32. A 2-D tensor of shape [num_boxes, 4]. The i-th row of the tensor specifies the coordinates of a box in the box_ind[i] image and is specified in normalized coordinates [y1, x1, y2, x2]. A normalized coordinate value of y is mapped to the image coordinate at y * (image_height - 1), so as the [0, 1] interval of normalized image height is mapped to [0, image_height - 1] in image height coordinates. We do allow y1 > y2, in which case the sampled crop is an up-down flipped version of the original image. The width dimension is treated similarly. Normalized coordinates outside the[0, 1]range are allowed, in which case we useextrapolation_value to extrapolate the input image values.
• box_ind: A Tensor of type int32. A 1-D tensor of shape [num_boxes] with int32 values in [0, batch). The value of box_ind[i] specifies the image that the i-th box refers to.
• crop_size: A Tensor of type int32. A 1-D tensor of 2 elements, size = [crop_height, crop_width]. All cropped image patches are resized to this size. The aspect ratio of the image content is not preserved. Both crop_height and crop_width need to be positive.
• method: An optional string from: "bilinear". Defaults to "bilinear". A string specifying the interpolation method. Only 'bilinear' is supported for now.
• extrapolation_value: An optional float. Defaults to 0. Value used for extrapolation, when applicable.
• name: A name for the operation (optional).
##### Returns:

A Tensor of type float32. A 4-D tensor of shape [num_boxes, crop_height, crop_width, depth]`.