# 3.1 Workflow
iPeriGrain implements the following two steps to achieve delineation of grain perimeters in 2D image
### Step 1: Initial Segmentation
In the initial segmentation, iPeriGrain generates grain markers automatically. The function of initial segmentation contains 4 procedures:
1. iPeriGrain uses [Canny edge detection](https://docs.opencv.org/trunk/da/d22/tutorial_py_canny.html) to detect edges in the 2D image with the hysteresis thresholds (both minimal and maximum), which are adjustable by users.
2. A distance map is then created by computing the distance of each pixel to its nearest edge.
3. Grain markers are generated if the distance value of the pixels is greater than a pre-defined distance threshold (fixed in iPeriGrain).
4. Marker controlled watershed segmentation is then conducted using the grain markers.
Two initial segmentation results with different thresholds are shown below:
#### Optical image
-- Original

-- min threshold = 50; max threshold = 100

-- min threshold = 100; max threshold =200

#### DEM
-- Original

-- min threshold = 50; max threshold =100

-- min threshold = 25; max threshold =50

-- Details of the Canny edge detection with hysteresis threshold can be found in [OpenCV's document](https://docs.opencv.org/trunk/da/d22/tutorial_py_canny.html)
-- The background knowledge about watershed segmentation can be found in the [documentation of OpenCV](https://docs.opencv.org/master/d3/db4/tutorial_py_watershed.html), more details can be found in [Morphological segmentation (S Beucher and F Meyer,1990)](https://www.researchgate.net/profile/Serge_Beucher/publication/238808165_Morphological_segmentation/links/5ac5ef12a6fdcc051dafc46d/Morphological-segmentation.pdf)
### Step 2. Marker controlled watershed segmentation with users interactively placing markers
The results from the initial segmentation varies with the choice of hysteresis thresholds. Oversegmentation and undersegmentation may co-exist in the image (see above examples). This is where iPeriGrain can be useful. Users can place grain and/or background markers in the 2D image to guide the segmentation process in order to delineate the perimeter of each grain.
- Grain markers are placed on the pixels that are likely to be grain.
- Background markers are placed on the pixels that are less likely to be grain.
- Avoid placing the markers on the pixels that are likely to be grain edge.
After placing the grain and background markers, users can update the segmentation results by clicking "ReSegment".
- Both grain and background markers are used to guide watershed segmentation in the same manner.
- Segments with grain markers are regarded as grain segments.
- Segments with background markers are regarded as background, which are discarded.
#### Optical image
-- After initial segmentation

-- Placing grain and background markers

-- ReSegment

#### DEM
-- After initial segmentation

-- Placing grain and background markers

-- ReSegment

Users can repeat interactively placing markers and ReSegment to achieve delineating all grains in the 2D image.
