(Digital Image Processing MATLAB+Python) Chapter 9 Image Morphological Operations - Sections 1 and 2: Morphological Basics and Binary Morphological Operations

1: Basics of morphology

Morphology : It is a basic method in image processing, used to analyze and process the shape and structure in images. Morphology is based on the theory of mathematical morphology, using structural elements and morphological operations to change and extract features such as shape, boundaries, and texture in images.

Morphological operation : It uses structural elements to operate on image collections, observes the relationship between various parts of the image, and extracts useful features for analysis and description, in order to achieve the purpose of image analysis and recognition . The following are common morphological operations

  • Dilation
  • Eroision
  • Opening
  • Closing
  • Hit-or-Miss Transform

(1) Structural elements

Structural element : It is an important concept in morphological operations. It is a small fixed-shape template or window used for convolution operations with images in morphological operations. Structural elements can be of various shapes, such as rectangles, circles, crosses, etc., the choice of which depends on the required operation and application scenario. The size and shape of structural elements have an important influence on the results of morphological operations. Smaller structural elements can better capture details and small-scale features in images, while larger structural elements are better suited for operations involving larger targets. Typically, structural elements are defined as binary images , where foreground pixels represent the shape of the structural element and background pixels represent the exterior of the structural element.

Insert image description here

The following principles need to be followed when selecting structural elements:

  • The geometric structure of the structural elements is simpler and bounded than the original image, and the size is significantly smaller than the size of the target image.
  • The shape of the structural elements preferably has some convexity
  • For each structural element, an origin is specified as the "reference point" for the structural element to participate in morphological operations.

All morphological treatments are based on the concept of filling in structural elements . The so-called filling is to use different methods to place structural elements inside the original image, and to induce a series of image characteristics in the filling of structural elements. Different structural elements and different morphological transformations can produce different image processing results.

(2) Procedure

As follows: Generate a diamond-shaped structural element

Insert image description here


matllab implementation :

strelFunctions are functions used to create structural elements used in operations in morphological image processing. Structural elements strelare created by functions according to specified shapes and sizes, and can be used for morphological operations such as dilation, erosion, opening operations, and closing operations. Its syntax format is as follows

se = strel(shape, parameters)

parameter

  • shapeThe parameter specifies the shape of the structural element and can be one of the following forms:

    • 'square': Square structural element
    • 'rectangle': Rectangular structural element
    • 'disk': circular structural element
    • 'line':Linear structural element
  • parametersParameters are optional and specify the size of the structural elements or other specific parameters, depending on the selected shape

strelThe function returns a structural element object sethat can be used to perform morphological operations on the image. Structural element objects have some commonly used methods, such as expansion ( dilate), corrosion ( erode), opening operation ( open), closing operation ( close), etc. These methods can be called to perform corresponding morphological operations.

SE = strel('diamond',3);
GN=getnhood(SE)%获取结构元素的邻域
figure,imshow(GN,[]); 

Python implementation :

import cv2
import numpy as np
import matplotlib.pyplot as plt

# 创建一个菱形结构元素
SE = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
GN = SE  # 获取结构元素的邻域

# 显示邻域图像
plt.imshow(GN, cmap='gray', vmin=0, vmax=1)
plt.axis('off')
plt.show()

2: Basic operations of binary morphology

(1) Basic form transformation

A: Expansion operation

①: Overview

Dilation operation : It is a basic operation in morphological operations, used to expand bright areas in images or increase the size of objects . The dilation operation can be implemented by convolving the structural elements with the image. CollectionXX _X uses structural elementSSS is expanded asX ⊕ S \boldsymbol{X} \oplus \boldsymbol{S}XS

X ⊕ S = { x ∣ [ ( S ^ ) x ∩ X ] ≠ ∅ } \boldsymbol{X} \oplus \boldsymbol{S}=\left\{\boldsymbol{x} \mid\left[(\widehat{\boldsymbol{S}})_{\boldsymbol{x}} \cap \boldsymbol{X}\right] \neq \varnothing\right\} XS={ x[(S )xX]=}

Its meaning is: for structural element SSS is mapped about the origin, and the resulting mapping translation isxxx , forming a new set as( S ^ ) x (\widehat{\boldsymbol{S}})_{\boldsymbol{x}}(S )x, with the set XXStructural element SS when X intersects and is not an empty setThe set of reference points of S is XXX wasSSThe set obtained by expanding S

The process of expansion operation is as follows

  1. Place the structure element SSThe center of S is placed at a pixel position of image X
  2. For structural element SSEach element in S , if the image corresponding to the element XXIf there is a non -zero value (i.e. a white pixel) at the corresponding position of
  3. Repeat steps 1 and 2 until structural element SSS covers the entireimageXXX
  4. The final result image represents image XXAll elements in X with the structural elementSSThe union of intersection pixels in S

The effect of the dilation operation is to expand the boundaries in the image and fill the holes, increasing the size of the target. It can be used for applications such as removing small noise, connecting disconnected boundaries, smoothing the edges of images, etc.

The expansion operation code is implemented as follows

Image=imread('menu.bmp');             %打开图像
BW=im2bw(Image);                   %转换为二值图像
figure,imshow(BW);
[h w]=size(BW);                      %获取图像尺寸
result=zeros(h,w);                     %定义输出图像,初始化为0
for x=2:w-1
    for y=2:h-1                      %扫描图像每一点,即结构元素移动到每一个位置
        for m=-1:1
            for n=-1:1               %当前点周围3×3范围,即结构元素为3×3大小
               if BW(y+n,x+m)       %结构元素所覆盖3×3范围内有像素点为1,即交集不为空
                   result(y,x)=1;      %将参考点记录为前景点
                   break;
               end
            end
        end
    end
end
figure,imshow(result);title('二值图像膨胀');
SE=strel('square',3);                 %创建结构元素
result1=imdilate(BW,SE);             %膨胀运算
figure,imshow(result1);title('二值图像imdilate');


②:Example

Example 1 : Figure (a) is a binary image, and the dark "1" part is the target set XXX. _ The dark “1” part in Figure (b) is the structural elementSSS (the place marked with "+" is the origin, which is the reference point of the structural element). FindX ⊕ S \boldsymbol{X} \oplus \boldsymbol{S}XS

Insert image description here

Solution : The dark “1” part in Figure © is the structural element SSMappingS ^ \widehat{S} of SS . The dark part in (d); among them, the light gray "1" part represents the set XXX ; The dark gray “1” part represents the expansion (expansion) part. Then the entire dark shaded part is the setX ⊕ S \boldsymbol{X} \oplus \boldsymbol{S}XS

Insert image description here


Example 2 : The dark “1” part in Figure (a) is the target set XXX , the dark “1” part in Figure (b) is the structural elementSSS。 求 X ⊕ S \boldsymbol{X} \oplus \boldsymbol{S} XS
Insert image description here

Solution : (c) Change SSS maps toS ^ \widehat{S}S , will S ^ \widehat{S}S in XXMove up X and record the position of the reference point of the structural element when the intersection is not empty. The dark shaded part shown in (d) is the result after expansion. It can be seen that after the expansion of the target set, not only the area is expanded, but two adjacent isolated components are connected
Insert image description here

③:Program

as follows

Insert image description here


matlab implementation :

Image=imread('menu.bmp');
BW=im2bw(Image);
SE=strel('square',3); %创建结构元素
result=imdilate(BW,SE); %膨胀运算
figure,imshow(result);

python implementation :

import cv2
import numpy as np
import matplotlib.pyplot as plt

# 读取图像
Image = cv2.imread('menu.bmp', 0)

# 二值化处理
ret, BW = cv2.threshold(Image, 127, 255, cv2.THRESH_BINARY)

# 创建一个3x3的正方形结构元素
SE = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))

# 膨胀运算
result = cv2.dilate(BW, SE)

# 显示结果图像
plt.imshow(result, cmap='gray')
plt.axis('off')
plt.show()

B: Corrosion operation

①: Overview

Corrosion operation : It is a basic operation in morphological image processing, used to reduce or eliminate the details of boundaries or objects in images . CollectionXX _X uses structural elementSSThe corrosion caused by S is recorded asX ⊖ SX \ominus SXS

X ⊖ S = { x ∣ ( S ) x ⊆ X } \boldsymbol{X} \ominus \boldsymbol{S}=\left\{\boldsymbol{x} \mid(\boldsymbol{S})_{\boldsymbol{x}} \subseteq \boldsymbol{X}\right\} XS={ x(S)xX }
The meaning is: if the structure elementSSS panxxAfter x is fully included in the collection XXIn X , recordSSThe reference point position of S , the resulting set isSSScorrosionXX __The result of X

The corrosion operation code is implemented as follows

Image=imread('menu.bmp');     %打开图像
BW=im2bw(Image);                %转换为二值图像
figure,imshow(BW);
[h w]=size(BW);                  %获取图像尺寸
result=ones(h,w);                %定义输出图像,初始化为1
for x=2:w-1
    for y=2:h-1                    %扫描图像每一点,即结构元素移动到每一个位置
        for m=-1:1
            for n=-1:1             %当前点周围3×3范围,即3×3结构元素所覆盖范围
               if BW(y+n,x+m)==0  %该范围内有像素点为0,即该位置不能完全包含结构元素
                   result(y,x)=0;  %将参考点记录为背景点,即腐蚀掉
                   break;
               end
            end
        end
    end
end
figure,imshow(result); title('二值图像腐蚀');
SE=strel('square',3);             %创建结构元素
result=imerode(BW,SE);         %腐蚀运算
figure,imshow(result); title('二值图像imerode');

②:Example

Example : Figure (a) is a binary image, and the dark "1" part is the target set XXX. _ The dark “1” part in Figure (b) is the structural elementSSS。求 X ⊖ S \boldsymbol{X} \ominus \boldsymbol{S} XS
Insert image description here

Solution : When SSWhen the S reference point is located in the red box "1" in figure (c),( S ) x ⊆ (S)_x\subseteq(S)x X, thenX ⊖ SX \ominus SXS is the dark gray “1” part in Figure (d). The white "0" part is the part where corrosion has disappeared

③:Program

as follows
Insert image description here


matlab implementation :

Image=imread('menu.bmp');
BW=im2bw(Image);
SE=strel('square',3); %创建结构元素
result=imerode(BW,SE); %腐蚀运算
figure,imshow(result);

python implementation :

import cv2
import matplotlib.pyplot as plt

# 读取图像
image = cv2.imread('menu.bmp', cv2.IMREAD_GRAYSCALE)

# 二值化
ret, bw = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)

# 创建结构元素
se = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))

# 腐蚀运算
result = cv2.erode(bw, se)

# 显示结果图像
plt.imshow(result, cmap='gray')
plt.show()

Expansive and corrosive properties

Property 1 : The expansion and erosion operations are dual relations about the set complement map

( X Θ S ) c = X c ⊕ S ^ ( X ⊕ S ) c = X c S ^ \begin{array}{l}(X \Theta \boldsymbol{S})^{\boldsymbol{c}}=X^{\boldsymbol{c}} \oplus \hat{\boldsymbol{S}} \\(X \oplus \boldsymbol{S})^{\boldsymbol{c}}=X^{\boldsymbol{c}} \boldsymbol{\hat { S }}\end{array} ( X Θ S )c=XcS^(XS)c=XcS^

Property 2 : The expansion operation is interchangeable, but the corrosion operation is not.

( X ⊕ S 1 ) ⊕ S 2 = ( X ⊕ S 2 ) ⊕ S 1 \left(X \oplus S_{1}\right) \oplus S_{2}=\left(X \oplus S_{2}\right) \oplus S_{1} (XS1)S2=(XS2)S1

Property 3 : The expansion operation is associative

If S = S 1 ⊕ S 2 , then: X ⊕ S = X ⊕ ( S 1 ⊕ S 2 ) = ( There are:X \oplus S=X \oplus\left(S_{1} \oplus S_{2}\right)=\left(X \oplus S_{1}\right) \oplus S_{2}If S=S1S2,then there is:XS=X(S1S2)=(XS1)S2

Property 4 : The expansion and erosion operations have growth properties

X ⊆ Y ⇒ ( X ⊕ S ) ⊆ ( Y ⊕ S ) X ⊆ Y ⇒ ( X ⊖ S ) ⊆ ( Y ⊖ S ) \begin{array}{l}X \subseteq Y \Rightarrow(X \oplus S) \subseteq(Y \oplus S) \\X \subseteq Y \Rightarrow(X \ominus S) \subseteq(Y \ominus S)\end{array} XY(XS)(YS)XY(XS)(YS)

(2) Composite morphological transformation

In general, expansion and corrosion are not inverse operations of each other . Expansion and corrosion are used in cascade to produce new morphological transformations, namely opening and closing.

A: Definition of opening and closing operations

Opening operation : Use structural elements to first erode and then expand the image

X ∘ S = ( X ⊖ S ) ⊕ S \boldsymbol{X} \circ \boldsymbol{S}=(\boldsymbol{X} \ominus \boldsymbol{S}) \oplus \boldsymbol{S} XS=(XS)S

Closed operation : use structural elements to first expand and then erode the image

X ⋅ S = ( X ⊕ S ) ⊖ S \boldsymbol{X} \cdot \boldsymbol{S}=(\boldsymbol{X} \oplus \boldsymbol{S}) \ominus \boldsymbol{S} XS=(XS)S

B: Opening and closing operation effects

Open operation effect :

  • Remove small objects (filter out spikes)
  • Split objects into small objects (cut off slender overlaps) for separation
  • Smooth the boundaries of large objects without significantly changing their area (smooth image contours)

Closed operation effect :

  • Fill cracks and holes in objects
  • Connecting similar objects (overlapping short discontinuities) plays the role of connecting and patching
  • Smooth the boundaries of large objects without significantly changing their area (smooth image contours)

C: Properties of opening and closing operations

Property 1 : Both opening and closing operations have growth properties

X ⊆ Y ⇒ { ( X ∘ S ) ⊆ Y ( X ⋅ S ) ⊆ Y X \subseteq Y \Rightarrow\left\{\begin{array}{l}(X \circ S) \subseteq Y \\(X \cdot S) \subseteq Y\end{array}\right. XY{ (XS)Y(XS)Y

Property 2 : The opening operation is non-extensional, while the closing operation is extensional

X ∘ S ⊆ X X ⊆ X ⋅ S \begin{array}{l}\boldsymbol{X} \circ \boldsymbol{S} \subseteq \boldsymbol{X} \\\boldsymbol{X} \subseteq \boldsymbol{X} \cdot \boldsymbol{S}\end{array} XSXXXS

Property 3 : Both opening and closing operations have the same precedence

( X ∘ S ) ∘ S = X ∘ S ( X ⋅ S ) ⋅ S = X ⋅ S \begin{array}{l}(X \circ S) \circ S=X \circ S \\(X \cdot S) \cdot S=X \cdot S\end{array} (XS)S=XS(XS)S=XS

Property 4 : Both opening and closing operations have duality

( X ∘ S ) c = X c ⋅ S ^ ( X ⋅ S ) c = X c ∘ S ^ \begin{array}{l}(X \circ S)^{c}=X^{c} \cdot \widehat{S} \\(X \cdot S)^{c}=X^{c} \circ \widehat{S}\end{array} (XS)c=XcS (XS)c=XcS

D: program

as follows:

Insert image description here


matlab implementation :

Image=imread('A.bmp');
BW=im2bw(Image);
SE=strel('square',3);
result1=imopen (BW,SE),SE);
result2=imclose(BW,SE),SE);
figure,imshow(result1) ;title('开运算');
figure,imshow(result2); ;title('闭运算');

python implementation :

import cv2
import matplotlib.pyplot as plt

# 读取图像
image = cv2.imread('A.bmp', cv2.IMREAD_GRAYSCALE)

# 二值化
ret, bw = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)

# 创建结构元素
se = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))

# 开运算
result1 = cv2.morphologyEx(bw, cv2.MORPH_OPEN, se)

# 闭运算
result2 = cv2.morphologyEx(bw, cv2.MORPH_CLOSE, se)

# 显示结果图像
plt.subplot(1, 2, 1)
plt.imshow(result1, cmap='gray')
plt.title('开运算')

plt.subplot(1, 2, 2)
plt.imshow(result2, cmap='gray')
plt.title('闭运算')

plt.show()

Guess you like

Origin blog.csdn.net/qq_39183034/article/details/130779296