This example shows how to create a new binary image, such as a mask image, by filtering an existing binary image based on properties of regions in the image.
Read a binary image into the MATLAB® workspace.
BW = imread('text.png');
Open the Image Region Analyzer app from the MATLAB toolstrip. On the Apps tab, in the Image Processing and Computer Vision section, click Image Region Analyzer .
On the app toolstrip, click Load Image, and then select Load Image from Workspace to load the image from the workspace into the app. In the Import from Workspace dialog box, select the image you read into the workspace, and then click OK.
You can also open the app from the command line using the
imageRegionAnalyzer function, specifying the image you want to analyze:
The Image Region Analyzer app displays the image you selected and a table where each row is a region identified in the image and each column is a property of that region, such as the area, perimeter, and orientation. (The Image Region Analyzer app uses the
regionprops command to identify regions in the image and calculate properties of those regions.)
To filter on the value of a region property, on the app toolstrip, click Filter. Then, select the property on which you want to filter.
Next, specify the filter criteria. For example, to create an image that removes all but the largest regions, choose the greater than or equal to symbol (>=), and then specify the minimum value. To identify the minimum value for the desired property, you can sort the values in the table by that property. The app uses the
bwareafilt functions to filter binary images.
To filter on another property, click Add. The app displays another row in which you can select a property and specify filter criteria. The result is the intersection (logical AND) of the two filtering operations.
If you are creating a mask image, you can optionally perform cleanup operations on the mask, such as clearing all foreground pixels that touch the border and filling holes in objects. Filling holes can change the area of regions in the image, and areas that had been filtered because they were below the threshold can become visible.
When you are done filtering the image, you can save it. Click Export and select Export Image. In the Export to Workspace dialog box, accept the default name for the mask image, or specify another name. Then, click OK.