In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you about how to deal with binary image corrosion in Python image processing. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
1 introduction
Morphological operation is an image processing method developed for binary images based on the set theory of mathematical morphology. Its main content is to design a set of transformation concepts and algorithms to describe the basic features of the image.
In image processing, the application of morphology mainly has the following two points: using the basic operation of morphology to observe and process the image in order to improve the image quality, and to describe and define various geometric parameters and features of the image.
2 corrosion concept
The operation of mathematical morphology is based on the two basic operations of corrosion and expansion. the function of corrosion operation in mathematical morphology is to eliminate the boundary points of objects and make the boundaries contract inwards. it is mainly used to remove objects that are smaller than the structural elements of the object. For example, there is a small connection between two objects, and the two objects can be separated by corrosion operation. The mathematical expression of corrosion is:
In the above formula:
S represents the corroded binary image.
B represents the structural element used for etching operations. Each element in the structural element has a value of 0 or 1, and it can form a figure of any shape.
X represents the collection of pixels after binarization of the original image.
The meaning of this formula is that the set S S obtained by corroding X by B is the set of the current position of B when B is completely included in X.
3 lift a chestnut
Just look at the above formula, is there a feeling in the clouds, then we might as well give a chestnut to explain, please take a look at the following picture:
The left side an is the processed binary image, the white part represents the background, and the gray part represents the target X.
The middle is the structural element B, the black dot is the central point of the structural element, and the gray square represents the neighborhood.
The black part in the right c represents the result of corrosion, and the gray part represents the corroded part of the target image.
We can understand the above corrosion process in this way, that is, compare the center point of B with the point on X one by one, and if all the corresponding points on B are within the range of X, the point will be retained; otherwise, the point will be removed.
Generally speaking, the structural element is moved in the image. If the structural element is completely contained in the target image X, the pixel corresponding to the central point in the target image is preserved, otherwise the pixel is deleted.
4 horizontal corrosion 4.1 theoretical basis
According to the types of structural elements used, image etching operations can be divided into the following three types: horizontal corrosion, vertical corrosion and omni-directional corrosion. Among them, the structural element used in horizontal etching is [0memo], and the specific steps are as follows:
According to the width and height of the original image, the initialization result image is all white (the background is white).
Because we use the horizontal etching operation and the structural element is 1X3, we do not deal with the leftmost and rightmost pixels. Starting from the second row and the second column, we iterate through each row one by one to determine whether the previous pixel and the next pixel of the element contain a background spot. Some indicate that the point needs to be eroded on the result map, and the gray value of the pixel is assigned to 255. otherwise, it remains unchanged.
Cycle through the above steps until all the pixels in the original image are processed.
4.2 Code implementation
Using python to implement the above process, the core code is as follows:
Def horizon_erode (bin_img): out_img = np.zeros (shape=bin_img.shape, dtype=np.uint8) + 255h = bin_img.shape [0] w = bin_img.shape [1] for i in range (h): for j in range (1) WMur1): out_ IMG [I] [j] = 0 for k in range (3): if bin_ IMG [I] [j+k-1] > 127: out_ IMG [I] [j] = 255 return out_img
The running results are as follows:
In the above picture, the original color image is shown from left to right, the result image after binarization, and the effect image after horizontal etching. It can be seen that the etched image is eroded vertically compared with the periphery of the binary map.
5 Vertical corrosion 5.1 theoretical basis
The principle of vertical corrosion is similar to that of horizontal corrosion, except that the structural elements used for vertical corrosion are different. The structural elements used for vertical corrosion are [0je 0je 0] T [0je 0je 0] ^ T [0J 0J 0] T. the detailed implementation steps are as follows:
According to the width and height of the original image, the initialization result image is all white (the background is white).
Because we use the vertical etching operation and the structural element is 3X1, we do not deal with the top and bottom two rows of pixels. Starting from the second row and the second column, we traverse each row one by one to determine whether the previous pixel and the next pixel of the element contain a background spot. Some indicate that the point needs to be eroded on the result graph, and the gray value of the pixel is assigned to 255.Otherwise, it remains unchanged.
Cycle through the above steps until all the pixels in the original image are processed.
5.2 Code implementation
Using python to implement the above process, the core code is as follows:
Def vertical_erode (bin_img): out_img = np.zeros (shape=bin_img.shape, dtype=np.uint8) + 255h = bin_img.shape [0] w = bin_img.shape [1] for i in range (1) For j in range (w): out_ IMG [I] [j] = 0 for k in range (3): if bin_ IMG [I + KMur1] [j] > 127: out_ IMG [I] [j] = 255 return out_img
The running results are as follows:
In the above picture, the original color image is shown from left to right, the result image after binarization, and the effect picture after vertical etching, it can be seen that the etched image is eroded one circle horizontally compared with the binary image.
6. Theoretical basis of omni-directional corrosion 6.1
Omni-directional corrosion combines vertical corrosion and horizontal corrosion, and the structural elements used are cross-shaped. The general steps to realize omni-directional corrosion are as follows:
According to the width and height of the original image, the initialization result image is all white (the background is white).
Omni-directional corrosion includes vertical corrosion and horizontal corrosion. Here we use the structural elements of 3X3, as follows:
In order to prevent crossing the boundary, we do not deal with the top, right, bottom and leftmost elements. Starting from the second row and the second column, we traverse each element one by one to determine whether there are background spots in the previous pixel of the element, the next pixel, the previous pixel, and the next pixel (that is, the four zeros in the array except the central point). It is stated that the point needs to be eroded on the result map, and the grayscale value of the pixel of the point is assigned to 255, otherwise it remains unchanged. Of course, different shapes of structural elements B can be defined for different corrosion effects, but the processing method is to check whether the corresponding pixels in B are all objects, and if so, keep the point, otherwise set it to 255.
Cycle through the above steps until all the pixels in the original image are processed.
6.2 Code implementation
Using python to implement the above process, the core code is as follows:
Def all_erode (bin_img): out_img = np.zeros (shape=bin_img.shape, dtype=np.uint8) + 255h = bin_img.shape [0] w = bin_img.shape [1] B = [1 for i in range (1): for j in range (1) WMUR 1): out_ IMG [I] [j] = 0 for m in range (3): for n in range (3): if B [mu 3m n] = = 1: continue if bin_ IMG [I + MMI 1] [j+n-1] > 127: Out_ IMG [I] [j] = 255 return out_img
The running results are as follows:
In the above picture, the original color image is shown from left to right, the result image after binarization, and the effect image after vertical etching. It can be seen that the etched image is eroded one circle horizontally and vertically compared with the binary image.
The above is how to deal with the binary image corrosion in the Python image processing shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.