# Align

Align 节点提供了一种通用方法，用于将 [`ImgFrame`](/software-v3/depthai/depthai-components/messages/img_frame.md) 和
[`Transformable`](/software-v3/depthai/api/cpp.md) 消息相互对齐。这两种类型都可以作为源或对齐目标，包括将 `ImgFrame` 对齐到任何可转换消息类型。

Align 默认在 [RVC4](/hardware/platform/rvc/rvc4.md) 设备上运行，也可以在主机上运行。自定义消息对齐需要主机执行并覆盖 `transformTo()`。

## 如何放置

#### Python

```python
with dai.Pipeline() as pipeline:
    align = pipeline.create(dai.node.Align)
```

#### C++

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

## 输入和输出

 * `input`: 要转换的消息。
 * `inputAlignTo`: 其 `ImgTransformation` 定义目标坐标系的消息。
 * `inputConfig`: 接受 `AlignConfig` 消息以在运行时更新对齐设置。使用 `initialConfig` 在启动管道之前配置节点。
 * `outputAligned`: 对齐后的消息，类型与 `input` 上的消息相同。
 * `passthroughInput`: 用于对齐的原始输入消息。

## 工作原理

Align 使用源消息和目标消息携带的 `ImgTransformation` 元数据将源映射到目标坐标系。源和目标不需要具有相同的消息类型。例如，您可以将检测对齐到图像，将图像对齐到检测，或将两个可转换消息相互对齐。

对于
[`ImgDetections`](/software-v3/depthai/depthai-components/messages/img_detections.md)，这允许您将神经网络输入图像中的检测映射到调整大小、裁剪、旋转或去畸变的图像以进行显示。

## 用法

要将检测对齐到图像，请将现有 [DetectionNetwork](/software-v3/depthai/depthai-components/nodes/detection_network.md)
的输出连接到 `input`，并将目标图像流连接到 `inputAlignTo`。在此代码片段中，`manip` 是一个现有的
[ImageManip](/software-v3/depthai/depthai-components/nodes/image_manip.md) 节点，它生成目标图像。

#### Python

```python
align = pipeline.create(dai.node.Align)
detectionNetwork.out.link(align.input)
manip.out.link(align.inputAlignTo)

alignedDetectionsQueue = align.outputAligned.createOutputQueue()
```

#### C++

```cpp
auto align = pipeline.create<dai::node::Align>();
detectionNetwork->out.link(align->input);
manip->out.link(align->inputAlignTo);

auto alignedDetectionsQueue = align->outputAligned.createOutputQueue();
```

输出队列包含目标图像坐标系中的 `ImgDetections` 消息。要将图像对齐到检测，请交换连接到 `input` 和 `inputAlignTo` 的流；对齐后的输出将包含 `ImgFrame` 消息。

## 自定义消息

自定义消息可以派生自 `TransformableBuffer` 并覆盖 `transformTo(target)` 以返回转换后的副本。将源 `ImgTransformation` 附加到每条消息，在 `transformTo()`
中转换其自定义字段，并设置返回消息的目标转换。

在启动管道之前将 Align 设置为在主机上运行：

#### Python

```python
align.setRunOnHost(True)
```

#### C++

```cpp
align->setRunOnHost(true);
```

下面的自定义消息示例定义了一条具有两个端点的线，并覆盖 `transformTo()` 以将这些端点重新映射到目标图像的坐标系中。

## 功能示例

| 示例 | Python | C++ |
| --- | --- | --- |
| 将 `ImgDetections` 对齐到转换后的图像 | [Python
示例](https://github.com/luxonis/depthai-core/blob/main/examples/python/Align/img_detections_align.py) | [C++
示例](https://github.com/luxonis/depthai-core/blob/main/examples/cpp/Align/img_detections_align.cpp) |
| 在主机上对齐自定义消息 | [Python 示例](https://github.com/luxonis/depthai-core/blob/main/examples/python/Align/custom_message_align.py) |
[C++ 示例](https://github.com/luxonis/depthai-core/blob/main/examples/cpp/Align/custom_message_align.cpp) |

## 参考

### dai::node::Align

Kind: class

Align node. Aligns ImgFrame and Transformable messages using ImgTransformation metadata.

#### std::shared_ptr< AlignConfig > initialConfig

Kind: variable

Initial config to use when aligning messages.

#### 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 to be aligned. Can be either ImgFrame or any message that implements Transformable interface. 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 the input message aligned to the inputAlignTo message. Output message will be of the same type as input message.

#### Output passthroughInput

Kind: variable

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

#### Align & setNumFramesPool(int numFramesPool)

Kind: function

Specify number of frames in the pool

#### 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

#### void buildStage1()

Kind: function

Build stages;.

#### DeviceNodeCRTP()

Kind: function

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

Kind: function

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

Kind: function

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

Kind: function

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

Kind: function

### 需要帮助？

请访问[讨论论坛](https://discuss.luxonis.com/)以获取技术支持或您可能有的任何其他问题。
