# Apply Mask

This function block filters an image using a mask image (black/white or grayscale) to keep or remove specific parts of the input image. It is intended for simple segmentation and masking workflows in AugeLab Studio.

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

* `Image` — Main image to be filtered (input socket).
* `Mask` — Mask image used to define preserved areas (input socket). Accepts a binary, grayscale or color image. The block will treat white/bright mask pixels as kept areas and dark pixels as removed areas.

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

* `Filtered` — Resulting image after applying the mask (output socket).

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

This block has no extra controls or widget settings. It performs masking based on the two provided inputs.

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

* Works with color (RGB) and grayscale images.
* Accepts masks in grayscale or color; the mask is interpreted as a keep/remove map.
* Keeps image pixels where mask is bright/white and hides pixels where mask is dark/black.
* Designed for fast, visual filtering in processing chains.

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

When evaluated, the block checks both inputs, ensures the mask can be used as a binary selector, and then applies the mask to the main image. The output is the original image with non-masked areas removed (or set to black) according to the mask.

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

1. Provide the image you want to filter to the `Image` input.
2. Provide a mask image to the `Mask` input. Bright/white regions in the mask will be preserved in the output; dark/black regions will be removed.
3. Connect the `Filtered` output to downstream blocks (for visualization, saving, or further processing).

Tips:

* If the mask and the image differ in size, resize or crop them so they match before connecting.
* If your mask is a rough segmentation, combine this block with morphological or smoothing steps to refine results.

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

* Create masks from intensity values using the `Image Threshold` or `Image Adaptive Threshold` blocks before feeding them into this block.
* Reduce noise in the mask source using `Blur` or `Denoising` to avoid small unwanted holes in the result.
* Improve foreground/background separation with `HSV Filter` when color is a strong cue, then convert that result into a mask for this block.
* For higher-quality subject extraction, try `Background Removal (RMBG-1.4)` or `Background Removal (BiRefNet)` and feed the resulting mask or cutout into this block.
* Use `Morphological Transformations` to clean up small mask artifacts (erode/dilate) before applying the mask.
* Crop or focus processing with `Image ROI Select` or `Image ROI` so the mask and image align on the area of interest.
* Preview results with `Show Image` and save successful outputs using `Image Logger` or `Image Write`.

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

* Mask appears to do nothing: Verify the `Mask` is aligned with the `Image` and that bright areas correspond to the regions you expect to keep. If sizes differ, use `Image Resize` or crop with `Image ROI Select`.
* Output contains speckles or holes: Apply `Blur` or `Morphological Transformations` to the mask to remove small defects.
* No visible change after masking: Check that the mask actually contains non-black regions. Use `Image Threshold` to explicitly binarize uncertain masks.
* Need to inspect intermediate results: Insert `Show Image` after mask creation steps to verify mask correctness before applying it.

## 📊 Evaluation <a href="#evaluation" id="evaluation"></a>

On execution this block outputs the `Filtered` image. Use the output to visualize, save, or feed into later analysis blocks such as detection or measurement.
