Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:NVIDIA DALI CropWindow

From Leeroopedia
Revision as of 15:53, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/NVIDIA_DALI_CropWindow.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Knowledge Sources
Domains Utilities, Image_Processing
Last Updated 2026-02-08 16:00 GMT

Overview

Defines the CropWindow struct for representing an n-dimensional cropping region with anchor point and shape, along with validation and generation utilities.

Description

The CropWindow struct in dali/util/crop_window.h represents a rectangular cropping region in an arbitrary number of dimensions. It consists of two TensorShape<> members: anchor (the starting coordinates of the crop) and shape (the extent of the crop along each dimension). By default, both are initialized to 2D zero shapes {0, 0}.

The struct provides a boolean conversion operator that returns true when the crop window has positive extent in all dimensions. It also provides IsInRange to validate that the crop window fits within a given input tensor shape, ensuring that the anchor is non-negative and that the crop does not exceed the input bounds along any dimension. The companion EnforceInRange method raises a DALI error with a descriptive message if the validation fails. Equality and inequality operators are provided for comparing crop windows.

The header also defines CropWindowGenerator, a std::function type alias that takes a TensorShape<> and a TensorLayout and returns a CropWindow. This enables pluggable crop generation strategies (e.g., random cropping, center cropping) to be passed as callbacks to operators that perform cropping.

Usage

Use CropWindow when implementing or configuring operators that perform spatial cropping on tensors. Set the anchor and shape to define the region of interest, then call IsInRange or EnforceInRange to validate against the input dimensions. Use the CropWindowGenerator type to pass custom crop generation logic to crop operators.

Code Reference

Source Location

Signature

struct CropWindow {
  TensorShape<> anchor;
  TensorShape<> shape;

  CropWindow();

  operator bool() const;
  inline bool operator==(const CropWindow& oth) const;
  inline bool operator!=(const CropWindow& oth) const;

  inline bool IsInRange(const TensorShape<>& input_shape) const;
  inline void EnforceInRange(const TensorShape<>& input_shape) const;

  void SetAnchor(TensorShape<> new_anchor);
  void SetShape(TensorShape<> new_shape);
};

inline std::ostream &operator<<(std::ostream &os, const CropWindow &wnd);

using CropWindowGenerator = std::function<CropWindow(const TensorShape<>& shape,
                                                     const TensorLayout& shape_layout)>;

Import

#include "dali/util/crop_window.h"

I/O Contract

Inputs

Name Type Required Description
anchor TensorShape<> Yes Starting coordinates (top-left corner) of the crop in each dimension
shape TensorShape<> Yes Extent (size) of the crop region in each dimension
input_shape TensorShape<> Yes (IsInRange / EnforceInRange) Shape of the input tensor to validate against

Outputs

Name Type Description
operator bool bool True if all dimensions of shape are positive
IsInRange bool True if the crop window fits within the specified input shape

Usage Examples

Creating and Validating a Crop Window

#include "dali/util/crop_window.h"

dali::CropWindow crop;
crop.SetAnchor({10, 20});
crop.SetShape({100, 200});

dali::TensorShape<> input_shape = {480, 640};

if (crop.IsInRange(input_shape)) {
  // Crop is valid, proceed with cropping
}

// Alternatively, throw an error if out of range
crop.EnforceInRange(input_shape);

Using a CropWindowGenerator

#include "dali/util/crop_window.h"

// Define a center crop generator
dali::CropWindowGenerator center_crop = [](const dali::TensorShape<>& shape,
                                           const dali::TensorLayout& layout) {
  dali::CropWindow win;
  // Crop to half the input size, centered
  dali::TensorShape<> crop_shape;
  dali::TensorShape<> anchor;
  for (int i = 0; i < shape.size(); i++) {
    crop_shape.shape.push_back(shape[i] / 2);
    anchor.shape.push_back(shape[i] / 4);
  }
  win.SetShape(std::move(crop_shape));
  win.SetAnchor(std::move(anchor));
  return win;
};

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment