Python图像处理:形态学操作
数据派THU
共 4018字,需浏览 9分钟
·
2021-08-09 00:08
来源:DeepHub IMBA 本文约1400字,建议阅读5分钟 形态学的操作主要是去除影响图像形状和信息的噪声。形态学运算在图像分割中非常有用,可以得到无噪声的二值图像。
形态学方法
膨胀
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)
![](https://filescdn.proginn.com/4cfc034897a7db2ea4a9daba25bba53f/3c1d417a3d2ad2532c0b19986b10b300.webp)
侵蚀
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)
![](https://filescdn.proginn.com/eb76b94efecf0333f9d6fc0e997f2350/68e581bdb5ffe2ba5eb4500df4949804.webp)
开操作
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)
![](https://filescdn.proginn.com/08f359f4f357ced0ef681ff221693c81/4f816decd92cf48b98f185ee2d5e79f1.webp)
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)
![](https://filescdn.proginn.com/b3084942159d2c136618963188b566b3/60b0b413176eaf0fb052be28f4d10474.webp)
形态学梯度
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)
![](https://filescdn.proginn.com/1d3ef880ed67f11b39caf2499cea83be/c4225ce34a298858c1e67b9bad111e12.webp)
总结
编辑:文婧
评论