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 theImage Region Analyzerapp from the toolstrip. On theAppstab, in theImage Processing and Computer Visionsection, click Image Region Analyzer.

On the app toolstrip, clickLoad Image, and then selectLoad Image from Workspaceto 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 clickOK.

You can also open the app from the command line using theimageRegionAnalyzerfunction, specifying the image you want to analyze:

imageRegionAnalyzer(BW)

Image Region Analyzerdisplays 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, clickFilter.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 theArea属性,选择大于或等于symbol (>=), and then specify the minimum value.

To filter on another property, clickAdd. 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. ClickExport>Export Image. In the Export to Workspace dialog box, accept the default name for the mask image, or specify another name. Then, clickOK.

You can save the list of properties as a structure or table. ClickExport>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. ClickExport>导出功能, 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