Main Content

Filter Images on Properties Using Image Region Analyzer App

This example shows how to create a new binary image by filtering an existing binary image based on properties of regions in the image.

Read a binary image into the workspace.

BW = imread("text.png");

Open the Image Region Analyzer app from the 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:


Image Region Analyzer displays the binary image and a table with region properties. In the table, 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.


To filter on the value of a region property, on the app toolstrip, click Filter. Select a property on which you want to filter and specify the filter criteria. For example, to create an image that removes all but the largest regions, select the Area property, choose the greater than or equal to symbol (>=), and then specify the minimum value.


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.

As you apply filters on properties, the app updates the binary image and the table automatically.


If you are creating a mask image, then 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 and therefore the regions that appear in the filtered image. For this example, the area of letters such as "b", "d", and "g" is larger after filling holes. A new region (a letter "o") with an area of 105 pixels now appears in the filtered image because the filled area of that region is above the threshold.


When you are done filtering the image, you can save it. Click Export > Export Image. In the Export to Workspace dialog box, accept the default name for the mask image, or specify another name. Then, click OK.


You can save the list of properties as a structure or table. Click Export > Export Properties.

You can also export a function that filters binary images using the same filters and cleanup operations that you specify. The function returns the filtered binary image and the property measurements in a table. Click Export > Export Function, then save the function as an M file.

function [BW_out,properties] = filterRegions(BW_in)
%filterRegions Filter BW image using auto-generated code from imageRegionAnalyzer app.

% Auto-generated by imageRegionAnalyzer app on 31-Oct-2021

BW_out = BW_in;

% Fill holes in regions.
BW_out = imfill(BW_out, 'holes');

% Filter image based on image properties.
BW_out = bwpropfilt(BW_out,'Area',[85, 124]);

% Get properties.
properties = regionprops(BW_out, {'Area', 'ConvexArea', 'EulerNumber', 'FilledArea', 'MajorAxisLength', 'MinorAxisLength', 'Orientation', 'Perimeter'});

See Also

| | |

Related Topics