0


Python图像处理:形态学操作

形态学方法

当图像经过预处理进行增强和阈值等性能操作时,图像就有可能得到一些噪声。从而导致图像中存在像素信息不平衡的问题。

形态学的操作主要是去除影响图像形状和信息的噪声。形态学运算在图像分割中非常有用,可以得到无噪声的二值图像。

基本的形态操作是侵蚀和膨胀。下面对这两种操作进行说明:

膨胀

在放大操作中,如果物体是白色的,那么白色像素周围的像素就会增大。它增加的区域取决于物体像素的形状。膨胀过程增加了对象的像素数,减少了非对象的像素数。

具有不同内核大小和迭代的膨胀的Python代码

 import numpy as np
 import imutils
 import cv2#reading the input image
 img = cv2.imread('thumb.png') #reads the image
 
 #cv2.imwrite('Input_image.jpg',image)
 
 #Resizing the image
 scale_percent = 70
 width = int(img.shape[1] * scale_percent / 100)
 height = int(img.shape[0] * scale_percent / 100)
 dim = (width, height)
   
 # resize the input image
 image = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
 
 kernel = np.ones((1,1), dtype = "uint8")/9
 dilation = cv2.dilate(image,kernel,iterations = 1)
 cv2.imwrite('dilation.jpg', dilation)
 
 kernel = np.ones((2,2), dtype = "uint8")/9
 dilation = cv2.dilate(image,kernel,iterations = 1)
 cv2.imwrite('dilation.jpg', dilation)
 
 kernel = np.ones((2,2), dtype = "uint8")/9
 dilation = cv2.dilate(image,kernel,iterations = 3)
 cv2.imwrite('dilation.jpg', dilation)
 
 kernel = np.ones((2,2), dtype = "uint8")/9
 dilation = cv2.dilate(image,kernel,iterations = 5)
 cv2.imwrite('dilation.jpg', dilation)
 
 kernel = np.ones((3,3), dtype = "uint8")/9
 dilation = cv2.dilate(image,kernel,iterations = 2)
 cv2.imwrite('dilation.jpg', dilation)

侵蚀

侵蚀函数正好与膨胀功函数相反。侵蚀作用使物体体积变小。侵蚀过程增加了非目标像素,减少了目标像素。

具有不同内核大小和迭代的侵蚀的Python代码

 import numpy as np
 import imutils
 import cv2
 
 #reading the input image
 img = cv2.imread('thumb.png')
 #cv2.imwrite('Input_image.jpg',image)
 
 #Resizing the image
 scale_percent = 70
 width = int(img.shape[1] * scale_percent / 100)
 height = int(img.shape[0] * scale_percent / 100)
 dim = (width, height)
 
 # resize the input image
 image = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
 
 kernel = np.ones((1,1), dtype = "uint8")/9
 erosion = cv2.erode(image, kernel, iterations = 1)
 cv2.imwrite('erosion.jpg', erosion)
 
 kernel = np.ones((2,2), dtype = "uint8")/9
 erosion = cv2.erode(image, kernel, iterations = 2)
 cv2.imwrite('erosion.jpg', erosion)
 
 kernel = np.ones((2,2), dtype = "uint8")/9
 erosion = cv2.erode(image, kernel, iterations = 3)
 cv2.imwrite('erosion.jpg', erosion)
 
 kernel = np.ones((2,2), dtype = "uint8")/9
 erosion = cv2.erode(image, kernel, iterations = 5)
 cv2.imwrite('erosion.jpg', erosion)
 
 kernel = np.ones((5,5), dtype = "uint8")/9
 erosion = cv2.erode(image, kernel, iterations = 2)
 cv2.imwrite('erosion.jpg', erosion)

开操作

此方法可用于从图像中去除噪声。该方法的工作功能是先腐蚀再膨胀,以保持物体像素的原始性,去除背景中的小噪声。

 import numpy as np
 import imutils
 import cv2
 #reading the input image
 img = cv2.imread('11.png')
 
 kernel = np.ones((5,5), dtype = "uint8")/9
 opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
 cv2.imwrite('opening.jpg', opening)

闭操作

此方法可用于从图像中去除噪声。这种方法的工作功能是先膨胀再腐蚀,去除内部的小噪声。

 import numpy as np
 import imutils
 import cv2
 #reading the input image
 img = cv2.imread('thumb.png')
 
 kernel = np.ones((9,9), dtype = "uint8")/9
 closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
 cv2.imwrite('closing.jpg', closing)

形态学梯度

这种方法是膨胀图与腐蚀图之差。

 import numpy as np
 import imutils
 import cv2
 
 #reading the input image
 img = cv2.imread('g1.png')
 
 kernel = np.ones((6,6), dtype = "uint8")/9
 gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
 cv2.imwrite('gradient.jpg', gradient)

总结

这些操作是处理二进制图像的一种非常简单的方法,也是图像处理应用程序中预处理的一部分。

作者:Amit Chauhan

标签:

“Python图像处理:形态学操作”的评论:

还没有评论