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 in the Image Region Analyzer app by using the imageRegionAnalyzer function.

imageRegionAnalyzer(BW)

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.

Binary region displayed in the Analyze Regions tab on the right, and a table with region properties on the left.

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.

Filter Regions dialog box with a single specified filter criteria, of area greater than or equal to 85 pixels.

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.

Only regions that meet the filter criteria appear in the binary image and the table of region properties.

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 90 pixels now appears in the filtered image because the filled area of that region is above the threshold.

Only filled regions that meet the filter criteria appear in the binary image and the table of region properties.

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.

Export to Workspace dialog box has a field to enter the name for the binary mask.

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 10-Oct-2023
%---------------------------------------------------------

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, Inf]);

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

See Also

| | |

Related Topics