OpenCV (5): image erosion and expansion, the concept of convolution

 Image Corrosion:

The function of image erosion is to remove burrs, which are generally used for binary images.

Suppose we now have a binary image:

We are now performing an erosion operation on the above image to remove the burrs around the text. 

The method used for image erosion in OpenCV is cv2.erode(input image, convolution kernel, number of iterations)

import cv2
import numpy as np

#读取图像
img = cv2.imread('Hello.png')

#写一个展示图像的方法
def cv_show(title,img):
    cv2.imshow(title,img)
    cv2.waitKey(0) 
    cv2.destroyAllWindows()
    return

# cv_show('img',img)

kernel = np.ones((3,3),dtype = np.uint8)#定义卷积核
erode = cv2.erode(img,kernel,iterations = 2)#调用腐蚀方法,迭代两次

cv_show('erode',erode)#展示腐蚀后的图像

 The returned result is:

 The principle of image erosion is to use the convolution kernel (the same thing as the filter kernel mentioned in the previous article) to determine pixel by pixel in the graphics matrix.

Assuming that the convolution kernel is a 3*3 matrix, if all the pixels in a pixel adjacent to the convolution kernel are white or black, then this point is fine, if a point is adjacent to 3*3 pixels there are White and black, then the point will be corroded.

The result of this method is that for a binary image (an image with only two values ​​in the image matrix), its border will be corroded, because only the pixel neighborhood of the border contains two values. The iteration parameter represents the number of iterations, and one iteration will corrode the image boundary once. The size of the convolution kernel will also affect the erosion. The larger the convolution kernel, the deeper the erosion boundary will be after one iteration.

See the figure below for details:

 image bloat

Contrary to image erosion, image dilation is a feature used to enlarge a binary image.
The cv2.dilate() method in opencv is used for image dilation. The parameters are consistent with erosion, which are the input image, convolution kernel, and number of iterations. The
expansion of the image is essentially similar to the operation performed by erosion. Erosion is to give black to the border of the image, and enlargement is to give white.

import cv2
import numpy as np

#读取图像
img = cv2.imread('Hello.png')

#展示图像的方法
def cv_show(title,img):
    cv2.imshow(title,img)
    cv2.waitKey(0) 
    cv2.destroyAllWindows()
    return

#定义卷积核
kernel = np.ones((3,3),dtype = np.uint8)
#图像膨胀
dilate = cv2.dilate(img, kernel, iterations = 2)
cv_show('dilate',dilate)

return result:

 It can be found that the edge of the image is enlarged, and it can also be said that the features of the image are enlarged.

The principle of image expansion is almost the same as that of image erosion, but the final assignment is different. Corrosion assigns black, and expansion assigns white. Of course, it is not necessarily black and white. It depends on which two values ​​your binary image is. 0 and 255 correspond to black and white.

Image convolution:

Then, through the learning of filtering in the previous chapter and the learning of opening and closing operations in this chapter, everyone should have some understanding of what convolution is.

Use the picture below to help understand. The essence of an image is a matrix. There is a one-dimensional matrix (grayscale image), a three-dimensional matrix (RGB image, panchromatic image), and a multispectral image (many dimensions). Convolution is to calculate a new matrix through the convolution kernel operation on the image matrix itself. The convolution kernel is the red box in the figure below. The elements in the red box will get a new value through a series of calculations (different algorithms have different calculation methods), and then a new matrix can be obtained by calculating according to the step size of one, and this matrix is ​​the new one you need image.

Open operation:

In addition to removing burrs, the erosion of the image itself will also eliminate some features of the original image, that is, the edges of the image you need are also eroded. Looking at the picture below is a comparison before and after erosion. It can be found that although the burrs have been removed, the 'Hello World' itself has also become thinner.

The way to solve this problem is: first corrode the image, remove the burrs, and then use the image expansion to enlarge the eroded features of the image.

Above code:

import cv2
import numpy as np

img = cv2.imread('Hello.png')
#显示图图像的方法
def cv_show(title,img):
    cv2.imshow(title,img)
    cv2.waitKey(0) 
    cv2.destroyAllWindows()
    return
#开运算:先侵蚀再膨胀
def clean(img):
    kernel = np.ones((3,3),dtype = np.uint8)
    erode = cv2.erode(img,kernel,iterations = 2 )#图像腐蚀
    dilate = cv2.dilate(erode, kernel, iterations = 2)#图像膨胀
    return dilate

#调用开运算
dilate = clean(img)

#展示图片
cv_show('dilate',dilate)

return:

 Compared with the previous image erosion operation, it can be found that the edges of the eroded image have been restored, but the eliminated burrs have not returned.

Then the relevant code has been encapsulated in OpenCV itself, and we don't need to define the opening operation method ourselves.

The method in OpenCV is cv2.morphologyEx(src, op, kernel), src passes in the image, op refers to the graphics operation method, and the kernel convolution kernel.

The most important of them is the op parameter, op = cv2.MORPH_OPEN means to open the operation.

import cv2
import numpy as np

def cv_show(title,img):
    cv2.imshow(title,img)
    cv2.waitKey(0) 
    cv2.destroyAllWindows()
    return

img = cv2.imread('Hello.png')
kernel = np.ones((5,5),dtype = np.uint8)
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN,kernel)#开运算

cv_show('open',opening)

Close operation

The principle of the closing operation is opposite to that of the opening operation, expanding first and then corroding.

The closing operation is mainly for small holes in the image. The opening operation removes the burrs, but it cannot solve the holes in the image. Because it is useless to shrink the hole first and enlarge it, you have to enlarge it first, plug the hole, and then shrink it to restore it to its original shape.

Just like the picture below. If you corrode first, and then expand, you will only enlarge and then shrink the hole (the black point in the image), but if you expand first, eliminate the hole, and then corrode, corrode the edge of the image after expansion, you can remove the hole , which is the closing operation.

 The closed operation also uses the cv2.morphologyEx(src, op, kernel) method, the difference is that op=cv2.MORPH_CLOSE at this time

Above code:

import cv2
import numpy as np

def cv_show(title,img):
    cv2.imshow(title,img)
    cv2.waitKey(0) 
    cv2.destroyAllWindows()
    return

img = cv2.imread('dongdong.png')
kernel = np.ones((5,5),dtype = np.uint8)
opening = cv2.morphologyEx(img, cv2.MORPH_CLOSE,kernel)#闭运算

cv_show('open',opening)

return

 It can be seen that the hole has been eliminated, and the size of the ellipse itself has not changed

Guess you like

Origin blog.csdn.net/m0_50317149/article/details/129768790