```{index} single: Filters; Replace Element Attributes with Neighbor (Threshold)
```
# Replace Element Attributes with Neighbor (Threshold)

## Group (Subgroup)

Processing (Cleanup)

## Description

This **Filter** first identifies all **Cells** that have a value that meets the selected threshold value set by the
user. Then, for each of those **Cells**, their neighboring **Cells** are checked to determine the neighbor **Cell** with
maximum or minimum value. The attributes of the neighbor with the maximum/minimum value are then reassigned to the
reference **Cell**.

*Note:* By default, the **Filter** will run only one iteration of the cleanup. If the user selects the *Loop Until Gone*
option, then the **Filter** will run iteratively until no **Cells** exist that meet the users criteria. So, if a **Cell**
meets the threshold and so are all of its neighbors, then that **Cell** will not be changed during that iteration and
will remain unchanged until one of its neighbors gets changed by a **Cell** further away.

## Examples

### TSL Data (.ang) File

When attempting to "clean up" EBSD data generated by EDAX/TSL the Attribute Array "Confidence Index" can be used.
Confidence Index is a measure of how confident in the indexing results given by the OIM Analysis software and ranges
from 0.0 (No confidence) to 1.0 (Absolute confidence). More information about the Confidence Index can be found in the
OIM analysis user manual. Typical inputs for TSL (.ang) Data might be the following.

| Filter Parameter | Value                                         |
|------------------|-----------------------------------------------|
| Threshold Value  | 0.1                                           |
| Operator         | <                                             |
| Selected Array   | [DataContainer] / CellData / Confidence Index |
| Loop Until Gone  | User dependent                                |

Using these values says that for every cell that has a confidence index < 0.1 it's neighbor cells will be considered. If
the neighbor cell has a confidence > 0.1 AND is greater than the other neighbors then this cell's data will be copied
into the original cell data location.

### Example EDAX Results

**Reasonable Use of the Filter**

| Original Data                                             |     | After Running Filter                                | True Data                                    |
|-----------------------------------------------------------|-----|-----------------------------------------------------|----------------------------------------------|
| ![Original Data](Images/NeighborReplace_No_Cleanup_2.png) |     | ![Text](Images/NeighborReplace_After_Cleanup_2.png) | ![Text](Images/NeighborReplace_Exemplar.bmp) |

**Example of Too Much Replacement**

| Original Data                                           | Threshold CI < 0.1                                                                                     | After Running Filter                              | True Data                                    |
|---------------------------------------------------------|--------------------------------------------------------------------------------------------------------|---------------------------------------------------|----------------------------------------------|
| ![Original Data](Images/NeighborReplace_No_Cleanup.png) | ![Original Data. Black pixels have Confidence Index < 0.1](Images/NeighborReplace_No_Cleanup_Mask.png) | ![Text](Images/NeighborReplace_After_Cleanup.png) | ![Text](Images/NeighborReplace_Exemplar.bmp) |

Note how in the above use of the filter the grain morphology is substantially changed from the exemplar data. Except for
very large grains being similar most of the fine grains and twins are **NOT** reproduced from the original.

### Oxford/Bruker (.ctf) File

Oxford Instruments and Bruker CTF data files do not include a "Confidence Index" measure. There are several measures
such as *Mean Angular Deviation (MAD)* or *Bands* but these values are not filled in or are defaulted to 0 (Zero) if the
scan point was not indexed. By using the *Error* value from the data file we can get the same effect as the EDAX
*Confidence Index*. The user would use the following input values:

| Filter Parameter | Value                              |
|------------------|------------------------------------|
| Threshold Value  | 0.0                                |
| Operator         | >                                  |
| Selected Array   | [DataContainer] / CellData / Error |
| Loop Until Gone  | User dependent                     |

### Example Oxford/Bruker Results

| Original Data                                           | After Running Filter                      |
|---------------------------------------------------------|-------------------------------------------|
| ![Original Data](Images/NeighborReplace_CTF_Before.png) | ![](Images/NeighborReplace_CTF_After.png) |

Note the large areas of unindexed pixels in the original image (black pixels) and how they are all filled in. The filter
can act much like a generic "flood fill" image processing algorithm if used improperly.


### Input Parameter(s)

| Parameter Name | Parameter Type | Parameter Notes | Description |
|----------------|----------------|-----------------|-------------|
| Threshold Value | Scalar Value |Float32 | The value to of the threshold |
| Comparison Operator | Choices |  | The operator to use for comparisons. 0=Less, 1=Greater Than |
| Loop Until Gone | Bool |  | The algorithm will keep looping until all pixels have been evaluated |

### Input Cell Data

| Parameter Name | Parameter Type | Parameter Notes | Description |
|----------------|----------------|-----------------|-------------|
| Selected Image Geometry | Geometry Selection | Image | The target geometry |
| Input Comparison Array | Array Selection | Allowed Types: int8, uint8, int16, uint16, int32, uint32, int64, uint64, float32, float64, boolean Comp. Shape: 1 | The DataPath to the input array to use for comparison |

## Example Pipelines

The example pipeline (pipelines/Examples/ReplaceElementAttributesWithNeighbor.d3dpipeline) will give output similar to the following images.

|  Before Filter | After Filter |
|--|--|
| ![Original Data](Images/NeighborReplace_Small_IN100_Before.png) | ![Original Data](Images/NeighborReplace_Small_IN100_After.png) |

## License & Copyright

Please see the description file distributed with this **Plugin**

## DREAM3D-NX Help

If you need help, need to file a bug report or want to request a new feature, please head over to the [DREAM3DNX-Issues](https://github.com/BlueQuartzSoftware/DREAM3DNX-Issues/discussions) GitHub site where the community of DREAM3D-NX users can help answer your questions.
