# ImageAlign

ImageAlign node is used for aligning sensor frames, like depth map (from ToF/Stereo) to color frame (RGB-D), or to align
temperature/thermal frame to color frame (RGB-Thermal) when specifying depth plane.

## How to place it

#### Python

```python
with dai.Pipeline() as pipeline:
    img_align = pipeline.create(dai.node.ImageAlign)
```

#### C++

```cpp
dai::Pipeline pipeline;
auto align = pipeline.create<dai::node::ImageAlign>();
```

## Inputs and Outputs

## How it works

ImageAlign takes the `input` frame and aligns it to `inputAlignTo` frame. Only the frame metadata of `inputAlignTo` is used. The
node waits for the first `inputAlignTo` frame and then uses the latest one, so `inputAlignTo` can have a lower frame rate than
`input`. The `input` frame is reprojected and aligned to it based on the extrinsic calibration between the two sensors.

## Usage

#### Python

```python
with dai.Pipeline() as pipeline:
    rgb_cam = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_A)
    video_stream = rgb_cam.requestOutput(size=(1280, 960), enableUndistortion=True)

    # Create left/right stereo cameras & StereoDepth node
    left = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_B)
    right = pipeline.create(dai.node.Camera).build(dai.CameraBoardSocket.CAM_C)
    stereo = pipeline.create(dai.node.StereoDepth)

    left.requestOutput(size=(640, 400)).link(stereo.left)
    right.requestOutput(size=(640, 400)).link(stereo.right)

    # Now create ImageAlign node and align stereo depth to RGB stream
    img_align = pipeline.create(dai.node.ImageAlign)
    stereo.depth.link(img_align.input)
    video_stream.link(img_align.inputAlignTo)

    # If you want to have synchronized aligned depth and color stream, you'd want to use Sync node
    sync = pipeline.create(dai.node.Sync)
    img_align.outputAligned.link(sync.inputs['aligned_depth'])
    video_stream.link(sync.inputs["color"])

    # Create an output queue, so you can get synced frames to the host computer
    queue = sync.out.createOutputQueue()

    # ...
```

#### C++

```cpp
dai::Pipeline pipeline;

// Create RGB Camera
auto rgbCam = pipeline.create<dai::node::Camera>()->build(dai::CameraBoardSocket::CAM_A);
auto videoStream = rgbCam->requestOutput({1280, 960}, std::nullopt, dai::ImgResizeMode::CROP, std::nullopt, true);

// Create left and right stereo cameras
auto left = pipeline.create<dai::node::Camera>()->build(dai::CameraBoardSocket::CAM_B);
auto right = pipeline.create<dai::node::Camera>()->build(dai::CameraBoardSocket::CAM_C);
auto stereo = pipeline.create<dai::node::StereoDepth>();

left->requestOutput({640, 400})->link(stereo->left);
right->requestOutput({640, 400})->link(stereo->right);

// Create ImageAlign node
auto imgAlign = pipeline.create<dai::node::ImageAlign>();
stereo->depth.link(imgAlign->input);
videoStream->link(imgAlign->inputAlignTo);

// Create Sync node
auto sync = pipeline.create<dai::node::Sync>();
imgAlign->outputAligned.link(sync->inputs["aligned_depth"]);
videoStream->link(sync->inputs["color"]);

// Create output queue
auto queue = sync->out.createOutputQueue();
// ...
```

## Examples of functionality

 * [Depth alignment](https://docs.luxonis.com/software-v3/depthai/examples/image_align/depth_align.md) - Align and blend RGB and
   depth frames.

## Reference

### dai::node::ImageAlign

Kind: class

Align an image or depth map to another camera's image geometry.

#### dai::Backend Backend

Kind: enum

Backend preference for ImageManip on RVC4. CPU: Run ImageManip on the CPU.; HW: Prefer the dedicated hardware image manipulation
backend.; GPU: Prefer the GPU backend.; AUTO: Let the runtime select the backend automatically (GPU with CPU fallback).
Hardware-accelerated backends can cause some unexpected behavior when using multiple ImageManip nodes in series. Currently, the
only operation affected is downscaling.

##### CPU

Kind: enum_value

##### HW

Kind: enum_value

##### GPU

Kind: enum_value

##### AUTO

Kind: enum_value

#### constexpr static const char * NAME

Kind: variable

#### std::shared_ptr< ImageAlignConfig > initialConfig

Kind: variable

Initial alignment configuration.

#### Input inputConfig

Kind: variable

Input message with ability to modify parameters in runtime. Default queue is non-blocking with size 4.

#### Input input

Kind: variable

Input message. Default queue is non-blocking with size 4.

#### Input inputAlignTo

Kind: variable

Input align to message. Default queue is non-blocking with size 1.

#### Output outputAligned

Kind: variable

Outputs ImgFrame message that is aligned to inputAlignTo. For RAW16 depth, values represent axial Z in the target camera
coordinate system; zero remains invalid.

#### Output passthroughInput

Kind: variable

Passthrough message on which the calculation was performed. Suitable for when input queue is set to non-blocking behavior.

#### ImageAlign & setOutputSize(int alignWidth, int alignHeight)

Kind: function

Specify the output size of the aligned image

#### ImageAlign & setOutKeepAspectRatio(bool keep)

Kind: function

Specify whether to keep aspect ratio when resizing

#### ImageAlign & setInterpolation(Interpolation interp)

Kind: function

Specify interpolation method to use when resizing

#### ImageAlign & setNumShaves(int numShaves)

Kind: function

Specify number of shaves to use for this node

#### ImageAlign & setNumFramesPool(int numFramesPool)

Kind: function

Specify number of frames in the pool

#### ImageAlign & setBackend(Backend backend)

Kind: function

Select the execution backend before starting the pipeline. CPU uses the host or RVC4 CPU; GPU prefers the RVC4 GPU for supported
RAW16 frames and falls back to CPU otherwise. Host execution always uses CPU. AUTO is the default: on RVC4 it uses the fused CPU
path for depth frames, which is faster than GPU except at high resolution and high frame rate. HW has no depth hardware path on
host or RVC4: it logs a warning and uses CPU. RVC2 device execution always uses hardware remapping and SHAVEs: AUTO and HW select
it silently, CPU and GPU log a warning and use it. The preference applies to RAW16 depth; 8-bit frames use the hardware warp when
the device supports it. This does not change host/device placement; use setRunOnHost() for that. parameters: backend: CPU, GPU, HW
or AUTO return: This node for chaining parameters: std::invalid_argument: for an unknown backend

#### Backend getBackend()

Kind: function

Get the configured backend preference; runtime fallback does not change it.

#### void setRunOnHost(bool runOnHost)

Kind: function

Specify whether to run on host or device By default, the node will run on device.

#### bool runOnHost()

Kind: function

Check if the node is set to run on host

#### void run()

Kind: function

#### inline DeviceNodeCRTP()

Kind: function

#### inline DeviceNodeCRTP(const std::shared_ptr< Device > & device)

Kind: function

#### inline DeviceNodeCRTP(std::unique_ptr< Properties > props)

Kind: function

#### inline DeviceNodeCRTP(std::unique_ptr< Properties > props, bool confMode)

Kind: function

#### inline DeviceNodeCRTP(const std::shared_ptr< Device > & device, std::unique_ptr< Properties > props, bool confMode)

Kind: function

### Need assistance?

Head over to [Discussion Forum](https://discuss.luxonis.com/) for technical support or any other questions you might have.
