# Define Mask

All of the Cognex Deep Learning Tools support the ability to exclude parts of the image from training by creating and applying masks.

During training and processing, the Deep Learning tools work by sampling the image, with the Feature Size being the nominal span of the Sampling Region, in pixels. During sampling, a significant amount of context information, also known as the Context Region, from around the Sample Region is also considered.

1 | Feature Size
2 | Sampling Region
3 | Context Region

When a mask is applied to an image, the pixels within the Sampling Region that are masked are always discarded. The Masking Mode parameter determines how the mask is applied to the Context Region.

Consider the following masked image, with the hypothetical Sampling and Context Regions:

When the Masking Mode parameter is set to the default setting of Transparent, the tool only collects samples within the unmasked parts of the image. However, the tool still considers context information from masked areas, signified by the bright green areas within the Context Region.

When the Masking Mode parameter is set to Mask, the tool discards any masked pixels within the Context Region, signified by the bright red areas within the Context Region. This setting also focuses the tool to emphasize the center of the ROI during sampling.

## Mask Toolbar

You can launch the Mask toolbar with the following methods:

- Select View > Edit Mask in the main menu bar.

- Right-click in an image and select Edit Mask from the context menu.

- Use the Ctrl+M keyboard shortcut.

After you have defined the mask on the image, click the Apply button. Click Apply All to use the mask on all the images in your training image set. Click the Close button to return to the process of constructing your Deep Learning application.

Setting | Description
--- | ---
Drawing Tools | The three drawing tools can be used in conjunction. The Width value defines the size of the area drawn. Line: Click and drag to create a line in your image. Tip: Hold down the Shift key while drawing to draw a straight line between two points. If you want to fill in a rectangular area, draw the outline using the line drawing tool by holding down the Shift key to create straight lines, and then fill it in using the magic wand tool. Circle: Place the cursor at the center of your circular object, and drag outward. Tip: Hold down the Shift key to have the circle expand from the corner of a bounding box. Magic Wand: Click and drag to fill in areas of the image. You can also click on individual features to apply fill to just that area. Note: The Eraser tool can be used to trim or remove unnecessary drawings on the image.
Width | Defines the pixel width of the mask drawing tools, border mask and the grow area. Make sure to set the Width before applying the mask. If you adjust the Width value while editing your mask, either with a drawing tool, border mask or area growth, the new value will only be applied to the next mask you create. Changing the Width value does not change previously created masks, even in the same editing session.
Border Mask | Creates a mask around the border of the image, based on the Width value. Applying a mask to the border of the image is helpful when you have images that arbitrarily cut off an object, such as a piece of fabric or a textile, because the field of view of the camera acquiring the image crops the object.
Grow Area | Expands the area of the mask by the Width value.
Invert | Inverts the mask. Using this option unmasks the area that was already masked, and masks the area that did not contain a mask.
Clear | Removes the mask from the image.
Import/Export | The import and export buttons launch an Import Mask-Image and Export Mask-Image dialog for importing or exporting a mask image, as a .png file. A mask image is a two-dimensional array of pixels that has the same size as the view in which the mask resides. The pixel values in the mask image determine whether or not the corresponding pixels in the view are used for training or runtime processing. The import/export options allow you to either import a previously created mask image, or export one that you just created.
Undo/Apply/Close/Apply All | Options to control the application of the mask. Undo: Undoes the last action. Apply: Applies the mask to the image. If you have multiple tools connected in a chain, this action also applies the mask to downstream tools. Close: Ends the mask editing session. You have to close the mask editing session to perform any other actions in the application. Apply All: Applies the same mask on all the images.

## Import Mask to Other View

You can import a previously created mask and apply it to an image or images in the View Browser. To import a mask to another view:

1. Launch the Mask toolbar.

2. Choose the views where you want to import the mask with one of the following methods:

   - Select one or more images in the View Browser, then right-click on the selection and chose Import Mask from the context menu.

   - Click on the Actions for N Views button of the Display Filter, and choose Import Mask from the context menu.

3. Select a mask file in the Import Mask-Image dialog.

4. Click OK to confirm your choice in the Apply Mask pop-up window.