Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problems of Image corrosion and Image expansion in Python Image processing

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you the "Python image processing how to solve the problem of image corrosion and image expansion", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to solve the problem of image corrosion and image expansion in Python image processing" this article.

Image Dilation and Erosion are two basic morphological operations, which are mainly used to find the maximum and minimum regions in the image. The expansion is similar to "domain expansion", which expands the highlighted area or white part of the image, and the resulting image is larger than the highlighted area of the original image; corrosion is similar to "the field is eroded". The highlighted area or white part of the image is reduced and refined, and the resulting image is smaller than the highlighted area of the original image.

1. Image expansion

The operator for inflation is "⊕", which is defined as follows:

The formula indicates that B is used to inflate image A, where B is a convolution template or convolution kernel, its shape can be square or circular, convolution calculation is carried out with template B and image A, every pixel in the image is scanned, and operation is done with template elements and binary image elements, if both are, then the target pixel is, otherwise it is 1. Thus, the maximum value of the pixel in the B coverage area is calculated, and this value is used to replace the pixel value of the reference point to achieve expansion. The following image is the expansion of the original image An on the left into the effect picture A ⊕ B on the right.

two。 Image corrosion

The operator for corrosion is "-", which is defined as follows:

The formula indicates that image An is corroded by convolution template B. through the convolution calculation of template B and image A, the minimum value of pixels in B coverage area is obtained, and this minimum value is used to replace the pixel value of reference points. As shown in the figure, the original image An on the left is etched into the effect picture Amurb on the right.

The processing result is shown in the following figure:

two。 Implementation of image corrosion code

1. Basic theory

Morphological transformation is mainly aimed at binary images (or 1). Image corrosion is similar to "domain erosion", which reduces and refines the highlighted area or white part of the image, and the result image is smaller than the highlighted area of the original image. It mainly includes two input objects:

(1) binary image

(2) convolution kernel

Convolution kernel is a key array in etching, which can be generated by numpy library. The center of the convolution core scans the original image pixel by pixel, as shown in the following figure:

The value of the pixel in the scanned original image is 1 only when the element value of the convolution kernel is 1, otherwise its value is changed to. In other words, the yellow dots traversed are all white around and remain white, otherwise the image becomes black and the image corrosion becomes smaller.

two。 Function prototype

The main function used for image erosion is erode, and its prototype is as follows:

Dst = cv2.erode (src, kernel, iterations)

The parameter dst represents the result of processing, src represents the original image, kernel represents the convolution kernel, and iterations represents the number of iterations. The following figure shows the convolution kernel of 5 ~ 5, which can be built with the function np.ones ((5 ~ 5), np.uint8).

Note: the default number of iterations is 1, which means one corrosion, or multiple iterations as needed.

3. Code implementation

The complete code is as follows:

The output is shown in the following figure:

As can be seen from the figure, the interfering thin lines have been cleaned, but there are still some contours, and the number of iterations can be set for corrosion.

Erosion = cv2.erode (src, kernel,iterations=9)

The output is shown in the following figure:

three。 Implementation of Image expansion Code

1. Basic theory

Image expansion is the reverse operation of corrosion operation, which is similar to "domain expansion". When the highlighted area or white part of the image is expanded, the highlight area of the image is larger than that of the original image, and the lines become thicker, which is mainly used for denoising.

(1) after the image is eroded, the noise is removed, but the image is compressed.

(2) by dilating the corroded image, the noise can be removed and the original shape can be maintained.

It also includes two input objects:

(1) binary image or original image

(2) convolution kernel

Convolution kernel is a key array in etching, which can be generated by numpy library. The center of the convolution core scans the original image pixel by pixel, as shown in the following figure:

For the pixels in the scanned original image, when the element value corresponding to the convolution kernel is 1, the value is 1, otherwise it is.

two。 Function prototype

The main function used for image expansion is dilate, and its prototype is as follows:

Dst = cv2.dilate (src, kernel, iterations)

The parameter dst represents the result of processing, src represents the original image, kernel represents the convolution kernel, and iterations represents the number of iterations. The following figure shows the convolution kernel of 5 ~ 5, which can be built with the function np.ones ((5 ~ 5), np.uint8).

Note: the default number of iterations is 1, which means that you can inflate once, or you can do multiple iterations as needed. It is usually possible to expand once.

3. Code implementation

The complete code is as follows:

The output is as follows:

Image denoising usually requires corrosion and then expansion, which is also known as open operation, which will be described in detail in the next article. As shown in the following figure:

Erosion = cv2.erode (src, kernel)

Result = cv2.dilate (erosion, kernel)

These are all the contents of this article entitled "how to solve the problem of image corrosion and image expansion in Python image processing". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report