Transfer from:Matlab image processing function: regionprops
Here is an image analysis function that is very important in the Matlab image processing toolbox: regionprops. As the name implies: its purpose is to get the properties of region, a function used to measure the properties of an image region.
grammar
STATS = regionprops(L,properties)
description
A series of attributes for each of the labeled areas in the dimension matrix L are measured. Different positive integer elements in L correspond to different regions, for example, an element equal to integer 1 in L corresponds to region 1; an element corresponding to integer 2 in L corresponds to region 2; and so on. The return value STATS is an array of structures of length max(L(:)), and the corresponding fields of the array of structures define the metrics under the corresponding attributes for each region. Properties can be a comma-separated list of strings, a cell array full of strings, a single string 'all' or 'basic'. If properties is equal to the string 'all', then all metric data in the following string list will be evaluated. If properties is not specified or equal to 'basic', the properties: 'Area', 'Centroid', and 'BoundingBox' Will be calculated. The following list is all valid attribute strings that are case sensitive and abbreviated.
Property String List Area EquivDiameter MajorAxisLength
BoundingBox EulerNumber MinorAxisLength
Centroid Extent Orientation
ConvexArea Extrema PixelIdxList
ConvexHull FilledArea PixelList
ConvexImage FilledImage Solidity
Eccentricity Image
Detailed definition of attributes
This section will combine a specific example to illustrate the meaning of the various string related attributes. The matrix is taken from the speckle segmentation image that has been preprocessed in the ant pattern recognition, as shown below:

This is a binary image. It must be labeled before applying the regionprops function. You can call the bwlabel function and pseudo color processing. The labeled image is as follows:

The meaning of the attributes is examined below based on the above materials.
'Area'
Is a scalar that calculates the total number of pixels in each area of the image. Note: This value may be slightly different from the value calculated by the function bwarea. For such a value, we can use it to divide the number of pixels in the entire image area to get the speckle ratio, which can be used as a candidate feature for pattern recognition, and this feature is affine-invariant. The last calculated area vector in this example is
[3.8952,9.7213,17.663,3.5762,1.3432,1.6958,0.41974,0.41974,21.625,12.324,4.8187,1.5111]/10000.
'BoundingBox'
Is a vector of 1 row ndims(L)*2 column, which is the smallest rectangle containing the corresponding region. BoundingBox is of the form [ul_corner width], where ul_corner gives the upper left corner of the bounding box in the form of [x y z ...], and boxwidth in the form of [x_width y_width ...] indicates the bounding box along each dimension length. The minimum rectangle of each part of this example is as shown below! Note: Please familiarize yourself with the use of the function rectangle.

'Centroid'
Is a vector of 1 row ndims (L) column giving the centroid (center of gravity) of each region. Note: The first element of Centroid is the horizontal coordinate of the center of gravity (x coordinate), and the second element is the vertical coordinate of the center of gravity (y coordinate). All other elements of Centroid are arranged in order of dimensions. The following image uses a small circle centered on the center to demonstrate the effect of centroid detection:

The centroid coordinates (standardized) in the figure are:
(x,y)= 0.10478, 0.76739 0.11883, 0.081545 0.19586, 0.61092 0.30701, 0.30807 0.65712, 0.31613 0.73165, 0.30531 0.74548, 0.35378 0.80624, 0.72802 0.84546, 0.61564 0.90554, 0.079574 0.93477, 0.77871 0.97611, 0.15576
'MajorAxisLength'
Is the scalar, long axis length of the ellipse with the same standard second-order center moment as the region (in the sense of pixels). This property only supports 2D dimension matrix.
'MinorAxisLength'
Is the scalar, short axis length of the ellipse with the same standard second-order central moment as the region (in the sense of pixels). This property only supports 2D dimension matrix.
'Eccentricity'
Is the scalar, eccentricity of the ellipse with the same standard second-order central moment as the region (can be characterized). This property only supports 2D dimension matrix.
'Orientation'
Is the scalar, the intersection angle (degrees) of the major axis of the ellipse with the same standard second-order central moment as the region and the x-axis. This property only supports 2D dimension matrix.
The ellipse data for each region of this example are: long axis: 18.767, 45.172, 43.003, 30.687, 16.505, 15.698, 5.8833, 5.8833, 46.954, 38.873, 22.929, 15.429 Short axis: 16.211, 26.079, 32.709, 9.8458, 6.8019, 8.6386, 5.8833,5.8833,35.976,31.022,16.98,7.8038 Eccentricity: 0.50387, 0.81652, 0.6492, 0.94713, 0.91114, 0.83497, 0,0, 0.64262, 0.60262, 0.67205, 0.86266 Directional angle: -29.219, -32.192, -9.3909,- 50.904,-70.333,48.823,0,0,14.035,17.986,3.0319,-34.238
We can examine the trend of eccentricity and get a general impression of the circularity of each region in the whole region. For example, the following figure shows the eccentricity of 12 regions:

It can be seen from the above figure that the overall circularity of the region is not good. In fact, the eccentricity vector can be considered as a feature of pattern recognition! !
'Image'
Binary image, a logical matrix of the same size as a region. You can use this property to extract each sub-region directly and then process it accordingly! For example, the first zebra area of this example is proposed:

'FilledImage'

Same as above, the only difference is that this is a logical matrix that has been filled!
In this case, there is no difference between the above and the above. Only when there is a hole in the area, there is a significant difference.
'FilledArea'
Is a scalar, fills the number of on pixels in the area image.
'ConvexHull'
Is a matrix of p rows and 2 columns, containing the smallest convex polygon of a certain area. Each row of this matrix stores the xy coordinates of a vertex of this polygon. This property only supports 2D dimension matrices. For example: the smallest convex polygon of all sub-areas in this example is as shown below

'ConvexImage'
A binary image used to draw the smallest convex polygon of the above region. At the same time, the pixels in the convex hull are opened, and the image size is the same as the corresponding boundary rectangle of this area. This property only supports 2D dimension matrices. For example: the smallest convex polygon of the second sub-area in this example is

. Note: The function roipoly is useful here!
'ConvexArea'
Is the number of on pixels in the scalar, filled area convex polygon image.
'EulerNumber'
Is a scalar, a topological invariant in the geometric topology - the Euler number, equal to the number of targets in the image minus the number of hollow holes in these targets. This property only supports 2D dimension matrices. The Euler number in this example is 1.
'Extrema'
8 rows and 2 columns matrix, eight-direction area extreme points. Each row of the matrix stores the xy coordinates of these points. The vector format is [top-left top-right right-top right-bottom bottom-right bottom-left left-bottom left-top]. This property only supports 2D dimension matrices.
'EquivDiameter'
Is a scalar, equivalent diameter: the diameter of a circle with the same area as the area. The calculation formula is: sqrt(4*Area/pi). This property only supports 2D dimension matrices. The standardized 12-area diameter vector in this example is:
[2.227,3.5182,4.7423,2.1339,1.3077,1.4694,0.73105,0.73105,5.2473,3.9612,2.477,1.3871]/100.
'Solidity'
Is the scalar, the ratio of pixels in the region and its smallest convex polygon. The calculation formula is: Area/ConvexArea, which is also an affine feature, which actually reflects the degree of firmness of the area. This property only supports 2D dimension matrices. The ratio vector of the convex element in this example 12 is:
[0.97071,0.66171,0.90846,0.86585,0.84211,0.94393,1,1,0.9096,0.75514,0.90823,0.94737].
'Extent'
Is the scalar, the ratio of pixels in the region and its smallest bounding rectangle. The calculation formula is: Area divided by the bounding rectangle area, which is also an affine feature, which actually reflects the extent of the area's extended range. This property only supports 2D dimension matrices. No more calculation results! !
'PixelIdxList'
The p-ary vector, the index subscript of the storage area pixel.
'PixelList'
The p-line ndems(L) column matrix stores the pixel coordinates corresponding to the above index.
Support class
The input label matrix L can have any numerical type.
remind
Use comma to separate list syntax
When you design an algorithm based on the output of the regionprops function, using a comma-separated list syntax highlights its value. For example, for a property that stores scalars, you can use this syntax to create a vector that contains the values of this property in different regions of the image. For example, the following two sentences are equivalent:
stats(1).Area, stats(2).Area, ..., stats(end).Area
stats.Area
Therefore, you can use the following method to create the corresponding vector:
regionprops(L,'Area'); allArea = [stats.Area];
allArea is a vector of the same length as the structure array stats.
Regional selection based on specific principles
It is useful to use the function ismember in conjunction with regionprops when you want to select a region based on certain criteria. For example: create a binary image that only contains areas larger than 80, with the following command
idx = find([stats.Area] > 80); BW2 = ismember(L,idx);
Ismember explains:
a=[1 2 3 4 5];
b=[3 2 5 6 7 8 77 44 3];
[tf index]=ismember(a,b); % Determines if the element in a appears in b
tf =
0 1 1 0 1
index =
0 2 9 0 3
%tf returns a matrix of the same size as a, the element of a appears in b, the corresponding position element value on tf is 1, and if it does not appear, it is 0; index also returns a sum a matrix of the same size, the value of its corresponding position is the last label of the element corresponding to position a in b, note the last label, if an element appears multiple times in b, the last occurrence of the label is recorded Returns 0 if the element of a at that position does not appear in b.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Calculation performance considerations
Most of the property measurement calculation time is very small, except those that depend very much on the number of regions and the number of pixels in the image L. E.g:
'ConvexHull' 'ConvexImage' 'ConvexArea' 'FilledImage'
It is also recommended to calculate all attribute values at once, because the time between the calculation and the calculation together is almost the same!
Working with binary images
The binary image must be converted to an annotation matrix before the regionprops is called. Two functions can do this:
L = bwlabel(BW); L = double(BW);
Note: Although these two functions produce different annotation matrices from the same binary image, they are equivalent! For example: give the following binary matrix BW,
1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 1
Bwlabel creates a continuous area label matrix containing two integers 1 and 2, respectively
mylabel = bwlabel(BW) mylabel = 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 0 0 0 0 2 2
Double Creates a matrix of discontinuous region labels with an integer 1 label.
mylabel2 = double(BW) mylabel2 = 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 1
Regionprops is not responsible for automatically converting binary image data types. Instead, you decide which data conversion method to use to store the data you want.
Extension of the regionprops function
Based on the regionprops function, you can use the basic data provided by it to extend its function, and develop the curvature data and skeleton data of the region as its additional property values, so that it can be used for more detailed feature extraction. .
bwlabel() usefulness is generally used in an image segmentation region, such as facial skin color region segmentation and other open usage L = bwlabel(BW,n) BW and returns a matrix of the same size L,...
sort() usefulness for one-dimensional or two-dimensional matrix to sort usage sort(A): One-dimensional or two-dimensional matrixAscendingSorting, sort and return to the matrix; and when A is a two-dim...
fspecial function Function: used to establish a predefined filter operator, or generate a predefined filter Note: For the form H=fspecial(type), the fspecial function generates a two-dimensional filte...
imread: In the same folder, if you use pictures, you can directly call: imwrite: Output the result data as a picture and store it in the current folder: im2double: The input image data are all integer...
IMDilate- expansion image This MATLAB function can expand grayscale image, binary image, or packaged binary image i, return image J after expansion. The format call is as follows: J = IMDILATE (I, SE)...
Imclearborder with bwareaopen Two functions Python code summary Two functions Imclearborder and bWareAopen are functional functions in matlab, and they turn them into Python versions in the blog. Feat...
Since the digital image can be seen as amatrixTherefore, image processing can be performed with MATLAB. This chapter briefly introduces several commonly usedImage processing function。 1.Read image imr...
MATLAB commonly used image processing function summary 1. Reading of images 2. Writing of images Third, the display of the image Fourth, create a window 5. Format conversion of images Six, gray straig...
1. Basic concept of image noise Noise is often expressed in images as isolated pixels or pixels that cause strong visual effects. Generally, the noise signal is not related to the object to be studied...
In the process of image processing, the image type must be converted, otherwise there will be a lot of operation errors in the middle, so we must study and organize the content of this part, which fun...