Morphological Image Processing
- Preliminary knowledge
- Basic Concepts in Set Theory
- expansion and corrosion
- swell
- Decomposition of Structural Elements
- function strel
- corrosion
- Combination of expansion and corrosion
- Open and closed operations
- Hit or miss transform
- Use a lookup table
- function bwmorph
1. Preliminary knowledge
Basic Concepts in Set Theory
Use "morphology" in the context of mathematical morphology to extract image components, useful in representing and describing the shape of regions such as boundaries, bones, convex hulls.
There are four basic states in a collection:
or (union), and (intersection), not (complement), difference
The symbols are:
| , & , ~ ,&~
perform basic operations
>> f = Fig0903;
>> g = Fig0904;
>> subplot(231), imshow(f);
>> subplot(232), imshow(g);
>> subplot(233), imshow(f|g);
>> subplot(234), imshow(f&g);
>> subplot(235), imshow(f&~g);
>> subplot(236), imshow(~g);
2. Expansion and Corrosion
swell
Dilation is the operation of "lengthening" or "thickening" in a binary image. This particular way and the degree of thickening is controlled by a collection called structuring elements. (Actually, the origin of the structural element is overlapped with 1 in the binary image, and the value of the overlapping part in the binary image that is not 1 is changed to 1 to complete the expansion)
The image of the structuring element based on the origin is to turn the structuring element 180 degrees. Similar to ==spatial convolution==.
- Dilation function
A2 = imdilate(A, B)
A and A2 are both binary images, and B is a matrix of 0s and 1s specifying the structuring elements.
>> A = Fig0906;
>> B = [0 1 0; 1 1 1; 0 1 0];
>> A2 = imdilate(A, B);
>> subplot(121), imshow(A);
>> subplot(122), imshow(A2);
You can clearly see the bold font in the picture.
Decomposition of Structural Elements
A ⊕ B = A ⊕ (B1 ⊕ B2) = (A ⊕ B1) ⊕ B2
B inflating A is equivalent to the result before B1 inflates A and then B2.
A row matrix and a column matrix can be inflated to a full mxn matrix.
function strel
The IPT function strel can construct structuring elements of various shapes and sizes.
se = strel(shape, parameters)
shape represents a string of desired shape, and parameters represents parameters (such as size, etc.) that specify shape information.
Shapes can be constructed according to the table below
>> se = strel('diamond', 5);
>> se
se =
strel is a diamond shaped structuring element with properties:
Neighborhood: [11×11 logical]
Dimensionality: 2
shows that se is a diamond-shaped structuring element that returns +-5 pixels horizontally and vertically (marked by red lines)
- getsequence function
decomp = getsequence(se)
Used to extract and examine individual structural elements in decomposition.
decomp =
4×1 strel array with properties:
Neighborhood
Dimensionality
After decomposition, four element structure vectors are obtained, which can be viewed one by one by index.
>> decomp(1)
ans =
strel is a arbitrary shaped structuring element with properties:
Neighborhood: [3×3 logical]
Dimensionality: 2
corrosion
In contrast to dilation, "shrinking" or "thinning" of objects in a binary image. (In fact, the origin of the structuring element is overlaid on the 1 of each binary image. As long as there are 0s on the binary image that overlap with the 1 of the structuring element, the value that overlaps with the origin is 0)
The same is done by sets and structuring elements.
- Corrosion function
IPT function imerode performs corrosion
A2 = imerode(A, se)
>> f = Fig0908;
>> se = strel('disk', 10);
>> g = imerode(f, se);
>> se = strel('disk', 5);
>> g1 = imerode(f, se);
>> g2 = imerode(f, strel('disk', 20));
>> subplot(221), imshow(f), title('原图');
>> subplot(222), imshow(g), title('disk 10');
>> subplot(223), imshow(g1), title('disk 5');
>> subplot(224), imshow(g2), title('disk 20');
To remove the thin lines in the original image, we need to find a small enough structural element for corrosion processing. It can be seen that when the specified parameter of the structural element is 20, the thin lines are basically processed.
3. Combination of expansion and corrosion
Open and closed operations
- open operation
A ○ B
After A is corroded by B, the result after B is expanded and corroded (corrosion first and then expansion)
C = imopen(A, B)
- closed operation
A • B
After A is expanded by B, and then B is etched and expanded (expanded and then etched)
C = imclose(A, B)
A is a binary image, and B is a specified structuring element composed of a 0, 1 matrix.
>> f = Fig0910;
>> se = strel('square', 40);
>> fo = imopen(f, se);
>> fc = imclose(f, se);
>> foc = imclose(fo, se);
>> subplot(221), imshow(f), title('原图');
>> subplot(222), imshow(fo), title('开');
>> subplot(223), imshow(fc), title('闭');
>> subplot(224), imshow(foc), title('开运算的闭运算');
In practical applications, open and close operations can be used to process fingerprints to achieve a clearer image.
>> f = Fig0911;
>> se = strel('square', 6);
>> fo = imopen(f, se);
>> foc = imclose(fo, se);
>> subplot(131), imshow(f), title('原图');
>> subplot(132), imshow(fo), title('开');
>> subplot(133), imshow(foc), title('开运算的闭运算');
The closing operation of the opening operation can fill the gap for the fingerprint.
Hit or miss transform
Hit-Miss Transform (HMT), the HMT transform can detect both the inside and the outside of an image. Research to solve the fields of target image recognition and pattern recognition can achieve better results in dealing with the relationship between target image and background.
- Identify pixel-specific shape functions
C = bwhitmiss(A, B1, B2)
A represents a set, B1, B2 represent structural elements
>> f = Fig0913;
>> B1 = strel([0 0 0; 0 1 1; 0 1 0]);
>> B2 = strel([1 1 1; 1 0 0; 1 0 0]);
>> C = bwhitmiss(f, B1, B2);
>> subplot(121), imshow(f), title('原图');
>> subplot(122), imshow(C), title('击中击不中变换');
On isolated points or end-point pixels of line segments, the hit or miss transform handles this kind of problem better.
Use a lookup table
When the structural elements in the hit or miss transformation are small, a lookup table can be used to calculate the hit miss faster, and a function endpoints is defined to deal with this type of problem.
- endpoints function
%endpoints函数
function g = endpoints(f)
persistent lut
if isempty(lut)
lut = makelut(@endpoint_fcn, 3);
end
g = applylut(f, lut);
%endpoint_fcn函数
function is_end_point = endpoint_fcn(nhood)
is_end_point = nhood(2, 2) & (sum(nhood(:)) == 2);
The function creates a function handle to an element structure of nhoos of any shape consisting of 0s and 1s, constructs a 3 x 3 lookup table, and writes the function handle.
- Hit or miss using lookup table
>> f = Fig0914;
>> g = endpoints(f);
>> subplot(121), imshow(f);
>> subplot(122), imshow(g);
function bwmorph
This function is implemented based on a combination of dilation, erosion and lookup table operations.
g = bwmorph(f, operation, n)
operation represents a string specifying the desired operation; n represents the number of times to repeat the operation (when n is Inf, it is represented as an infinite number of times).
>> f = Fig0917;
>> g = bwmorph(f, 'thin', 1);
>> g1 = bwmorph(f, 'thin', 3);
>> subplot(131), imshow(f), title('原图');
>> subplot(132), imshow(g), title('细化1次');
>> subplot(133), imshow(g1), title('细化3次');
It can be clearly seen the difference between thinning once and thinning three times. After three thinning, some small lines have signs of disappearing.
- function bwlabel
The IPT function bwlabel is used to calculate all the connected components in the binary image.
[L, num] = bwlabel(f, conn)
L represents the marker matrix, num gives the total number of connected components found, and conn specifies the desired connections (4 or 8).
- Example: Computing and Displaying Centroids of Connected Components
The function find is useful when dealing with marker matrices, returning the row and column indices of all pixels of an object.
>> [L, n] = bwlabel(f);
>> [r, c] = find(L == 3);
>> rbar = mean(r);
>> cbar = mean(c);
>> imshow(f);
>> hold on
>> for k = 1:n
[r, c] = find(L == k);
rbar = mean(r);
cbar = mean(c);
plot(cbar, rbar, 'Marker', 'o', 'MarkerEdgeColor', 'k',...
'MarkerFaceColor', 'k', 'MarkerSize', 10);
plot(cbar, rbar, 'Marker', '*', 'MarkerEdgeColor', 'w');
end