# Distance Transformation

This function block converts a binary or grayscale image into a continuous distance map where brighter pixels represent larger distances from foreground (white) regions. It is useful for segmentation refinement, shape analysis and preparing images for contour or watershed-based processing.

## 📥 Inputs <a href="#inputs" id="inputs"></a>

`ImageGray` This block expects a binary or single-channel grayscale image as input.

## 📤 Outputs <a href="#outputs" id="outputs"></a>

`ImageGray` Outputs a single-channel grayscale image where pixel values represent computed distance values (normalized for visualization and subsequent processing).

## 🕹️ Controls <a href="#controls" id="controls"></a>

`Transormation Type` A dropdown that lets you choose the distance metric used for the transformation. Options correspond to commonly used distance measures (for example, Euclidean, Manhattan, and Chessboard styles). Select the one that best fits your measurement/segmentation needs.

## 🎨 Features <a href="#features" id="features"></a>

* Produces a continuous distance map from a binary/grayscale input to reveal how far each background pixel is from the nearest foreground pixel.
* Normalized output suitable for visualization and for feeding into later processing blocks.
* Simple dropdown control to switch between distance metric types to adapt behavior for different shapes and layouts.

## ⚙️ Running mechanism <a href="#running-mechanism" id="running-mechanism"></a>

When the block runs it reads the provided grayscale/binary image, computes a distance value for each pixel using the selected distance metric, normalizes the result for easy viewing, and outputs the resulting grayscale distance map. Use a valid binary foreground/background input for best results.

## 📝 Usage instructions <a href="#usage" id="usage"></a>

1. Provide a clean binary or well-thresholded grayscale image to the `ImageGray` input.
2. Choose the desired `Transormation Type` from the dropdown.
3. Use the resulting `ImageGray` output as visualization or feed it into other analysis blocks.

## 💡 Tips and Tricks <a href="#tips-and-tricks" id="tips-and-tricks"></a>

* Preprocess with `Image Threshold` or `Image Adaptive Threshold` to obtain a clean binary foreground before using this block.
* Use `Blur` to reduce small noise before thresholding — this often produces smoother distance maps.
* After distance transformation, combine with `Watershed Algorithm` or `Image Skeletonize` to perform object separation or extract medial axes.
* Use `Find Contour`, `Approximate Contour`, `Minimum Rectangle` or `Minimum Circle` on the cleaned/segmented result to obtain geometric measurements or bounding shapes.
* Limit analysis to a region of interest by using `Image ROI Select` or `Get ROI` before feeding the image into this block.
* Visualize outputs with `Show Image` and store important results with `Image Logger` or `Image Write`.

## 🛠️ Troubleshooting <a href="#troubleshooting" id="troubleshooting"></a>

* If the output looks noisy or scattered, ensure the input is a well-defined binary mask; consider using `Morphological Transformations` to remove small artifacts.
* If very large values or unexpected scaling appear, re-check upstream preprocessing steps (thresholding and noise removal) and use the visualization via `Show Image` to inspect intermediate results.
* For poor separation of touching objects, try changing the `Transormation Type` or follow the distance transform with `Watershed Algorithm` to improve segmentation.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.augelab.com/function-blocks/blocks-reference/image-transformations/transformation-filters/distance-transformation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
