In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points of Python OpenCV morphological operation example analysis, the content is detailed, the logic is clear, I believe most people still know too much about this knowledge, so share this article for your reference, hope you can get something after reading this article, let's take a look at it.
1. Corrosion & expansion 1.1 what is corrosion & expansion
Corrosion-expansion are two core operations in image morphology.
Corrosion can be described as allowing the image to contract inward along its own boundary.
Expansion, on the other hand, is the opposite of contraction, which can be described as allowing the image to expand inward along the boundary.
The logic and function of these two operations are similar to those described in the previous section, except that erosion seeks the minimum value of pixels in the filter core, while expansion seeks the maximum value. And copy the calculated value to the pixel of the anchor location.
Similar to smoothing, it can eliminate noise.
Because the corrosion is the minimum and the expansion is the maximum, the overall brightness of the image after the corrosion operation will be reduced, while the overall brightness of the image after the expansion operation will be increased.
To facilitate the example, prepare the following picture material (test1.jpg):
1.2 corrosion method cv2.erode ()
OpenCV in python uses the cv2.erode () method to realize the corrosion operation.
The method syntax is as follows:
Cv2.erode (src, kernel, anchor=None, iterations=None, borderType=None, borderValue=None)
Scr original image
Nuclei to be used for kernel etching
Anchor anchor point
Iterations optional parameter, the number of iterations of the etching operation, default is 1.
BorderType border style, optional.
BorderValue boundary value, optional.
The parameter kernel, kernel, needs to be manually taken to create an array, rather than specifying a size like a filter.
Import cv2import numpy as npimg = cv2.imread ("test1.jpg") # create an array of 3x3 as filter kernel k = np.ones ((3,3), np.uint8) dst = cv2.erode (img, k) cv2.imshow ("dst", dst) cv2.waitKey () cv2.destroyAllWindows ()
The corrosion effect is as follows, as shown in the picture, our fish bones appear to be older, and the fish bones disappear and darken a considerable part of them.
1.3 expansion method cv2.dilate ()
OpenCV in python uses the cv2.dilate () method to implement the bloat operation.
The method syntax is as follows:
Dilate (src, kernel, dst=None, anchor=None, iterations=None, borderType=None, borderValue=None)
As you can see, its parameter usage is the same as that of cv2.erode ().
Import cv2import numpy as npimg = cv2.imread ("test1.jpg") # create an array of 16 to 16 as kernel k = np.ones ((16,16), np.uint8) dst = cv2.dilate (img, k) cv2.imshow ("dst", dst) cv2.waitKey () cv2.destroyAllWindows ()
The expansion effect is as follows, as shown in the picture, the brightness of many fish in the picture is significantly higher.
This kind of image effect is also called "myopia" effect.
two。 Brief introduction of Open Operation & closed Operation 2.1
Open operation is to corrode the advanced nature of the image, and then expand the operation. It can be used to erase the details (noise) outside the image.
Closed operation is the opposite.
Closed operation is the first expansion of the image operation, in the corrosion operation. It can be used to erase the internal details (noise) of the image.
Although corrosion and expansion are inverse operations, open and closed operations will not restore the image to its original state.
2.2 Open operation
Take 3 as the core
The code example is as follows
Import cv2import numpy as npimg = cv2.imread ("test1.jpg") k = np.ones ((3,3), np.uint8) dst = cv2.erode (img, k) dst = cv2.dilate (dst, k) cv2.imshow ("dst", dst) cv2.waitKey () cv2.destroyAllWindows ()
2.3 closed operation
Take 10 as the core
The code example is as follows
Import cv2import numpy as npimg = cv2.imread ("test1.jpg") k = np.ones ((10,10), np.uint8) dst = cv2.dilate (img, k) dst = cv2.erode (dst, k) cv2.imshow ("dst", dst) cv2.waitKey () cv2.destroyAllWindows ()
3. Introduction of morphologyEx () method 3.1 morphologyEx () method
In python, OpenCV also provides morphologyEx () method (morphological method), which can be used to complete all commonly used morphological operations.
The syntax of morphologyEx () is as follows:
MorphologyEx (src, op, kernel, dst=None, anchor=None, iterations=None, borderType=None, borderValue=None)
Among them
Scr represents an image
Op indicates the type of operation
Kernel represents kernel
Anchor stands for anchor point
Iterations is the number of iterations. Default is 1.
BorderType is the boundary style, default is 1
BorderValue is the boundary value. Default is 1.
The types of operations that op can choose from are:
Parameter values describe cv2.MORPH_ERODE corrosion cv2.MORPH_DILATE expansion cv2.MORPH_ OPEN open operation, first corrosion then expansion cv2.MORPH_CLOSE closed operation, first expansion then corrosion cv2.MORPH_GRADIENT gradient operation, expansion graph corrosion reduction graph cv2.MORPH_TOPHAT top cap operation, original graph subtraction operation graph cv2.MORPH_BLACKHAT black hat operation, closed operation graph, subtraction operation graph
Next, we use the picture "test2.jpg" (below) to continue with the following example:
3.2 gradient operation
Do a gradient operation on "test2.jpg" with 4 as the core:
Import cv2import numpy as npimg = cv2.imread ("test2.jpg") k = np.ones ((4,4), np.uint8) dst = cv2.morphologyEx (img, cv2.MORPH_GRADIENT, k) cv2.imshow ("dst", dst) cv2.waitKey () cv2.destroyAllWindows ()
The gradient operation, that is, the expansion graph minus the corrosion graph, because the object in the image obtained by the expansion operation is larger than that in the original image, while the object in the image obtained by the erosion operation is shrunk and smaller than that in the original image, so the result of expansion minus the result of corrosion will get a general and imprecise outline.
The effect of test2.jpg gradient operation is as follows:
3.3 Top hat operation
Do the cap operation on "test2.jpg" with 4 as the core:
Import cv2import numpy as npimg = cv2.imread ("test2.jpg") k = np.ones ((4,4), np.uint8) cv2.imshow ("img", img) dst = cv2.morphologyEx (img, cv2.MORPH_TOPHAT, k) cv2.imshow ("dst", dst) cv2.waitKey () cv2.destroyAllWindows ()
The top hat operation, that is, the original image minus the open operation graph, because the open operation erases the external details of the image, so the top hat operation is "the image with external details minus the image without external details", and the result is only external details.
The processing effect of top cap operation is as follows:
3.4 Black hat operation
Do the cap operation on "test2.jpg" with 4 as the core:
Import cv2import numpy as npimg = cv2.imread ("test2.jpg") k = np.ones ((4,4), np.uint8) dst = cv2.morphologyEx (img, cv2.MORPH_BLACKHAT, k) cv2.imshow ("dst", dst) cv2.waitKey () cv2.destroyAllWindows ()
Black hat operation, that is, the closed operation of the original image minus the original image
Because the closed operation can erase the internal details of the image, the black hat operation is "the image without internal details minus the image with internal details", leaving only the internal details.
The processing effect of black hat operation is as follows:
The above is all the content of this article "Python OpenCV morphological operation example analysis". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.