Expansion and corrosion

In fact, if you do not know about expansion and corrosion, you need to read this article first to understand image processing, corrosion, expansion and refinement

Expansion and corrosion are the basis of morphological processing and many morphological algorithms are based on these two operations.

(1) inflation

Is based on taking the image of B relative to its own origin and shifting the image by Z. A expanded by B is the set of all the displacements Z, such that there is at least one element of overlap with A. We can rewrite the above equation as:

Structural element B can be regarded as a convolution template. The difference is that inflation is based on set operation and convolution is based on arithmetic operation, but the processing process of the two is similar.

⑴ Scan every pixel of image A with structural element B

(2) Make “and” operation with the structure element and its covered binary image

⑶ If both are 0, the pixel in the resulting image is 0. Otherwise, it is 1

(2) corrosion

For sets A and B in Z,B corrodes A as follows:

⑴ Scan every pixel of image A with structural element B

(2) Make “and” operation with the structure element and its covered binary image

⑶ If both are 1, the pixel in the resulting image is 1. Otherwise, it is 0

The result of etching is to reduce the original binary image by one turn.

⑷ Hit (match) or miss transform

Their applications are extensive:

Eliminate noise

They isolate independent image elements and join adjacent elements.

The Description of the OpenCV functions is erode and dilate

erode

Erode (InputArray SRC,// Original image

OutputArray DST,// Output image // etch operation of the kernel. If not specified, the default is a simple 3×3 matrix. Otherwise, we need to specify its shape explicitly, using getStructuringElement:

           InputArray kernel,      

          Point anchor = Point(-1,-1),

           int iterations =1,

           int borderType = BORDER_CONSTANT,

            const Scalar& borderValue = morphologyDefaultBorderValue()

)

Dilate (use of parameters as above)

dilate(InputArray src,      

           OutputArray dst,      

           InputArray kernel,      

           Point anchor = Point(-1,-1),      

            int iterations =1,      

            int borderType = BORDER_CONSTANT,      

             const Scalar& borderValue = morphologyDefaultBorderValue() 

              )

getStructuringElemen

// where shap is the kernel shape, ksize is the kernel size, and anchor is the anchor position

  Mat getStructuringElement(

                                                  int shape,                         

                                                  Size ksize,                          

                                                   Point anchor= Point(-1,-1)

                                                    )

Shap comes in three shapes

Rectangle: MORPH_RECT

X-type: MORPH_CROSS

The oval: MORPH_ELLIPSE

Code implementation

corrosion

– (void)eroding

{

int erosion_type;

/// dilation_elem global variable int, used to control the selected kernel shape

if(dilation_elem == 0) {

erosion_type = MORPH_RECT; / / rectangle

} else if(dilation_elem == 1) {

erosion_type = MORPH_CROSS; / / cross

} else {

erosion_type = MORPH_ELLIPSE; / / round

}

/// specify the kernel erosion_size global variable. I’m using slider. value to control erosion_size

Mat element = getStructuringElement(erosion_type, CV: : Size (2 * * erosion_size erosion_size + 1, 2 + 1), CV: : Point (erosion_size erosion_size));

// Corrosion operation

erode(src, dst, element);

self.secondImageView.image = MatToUIImage(dst);

}

Effect drawing (one of them)




inflation

– (void)diating

{

int erosion_type;

/// dilation_elem global variable int, used to control the selected kernel shape

if(dilation_elem == 0) {

erosion_type = MORPH_RECT; / / rectangle

} else if(dilation_elem == 1) {

erosion_type = MORPH_CROSS; / / cross

} else {

erosion_type = MORPH_ELLIPSE; / / round

}

/// specify the kernel erosion_size global variable. I’m using slider. value to control erosion_size

Mat element = getStructuringElement(erosion_type, CV: : Size (2 * * erosion_size erosion_size + 1, 2 + 1), CV: : Point (erosion_size erosion_size));

// Expand operation

dilate(src, dst, element);

self.secondImageView.image = MatToUIImage(dst);

}

Effects (one of them)




The resources

6. stranded and Dilating

6. Eroding and Dilating

Image processing corrosion expansion refinement