图像侵蚀
用于消除小的白色噪声
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('number.jpg')
kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(img,kernel,iterations= 1)
plt.subplot(121),plt.imshow(img),plt.title('original'),plt.xticks([]),plt.yticks([])
plt.subplot(122),plt.imshow(erosion),plt.title('erosion'),plt.xticks([]),plt.yticks([])
plt.show()
图片扩展
与侵蚀相反
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('number.jpg')
kernel = np.ones((5,5),np.uint8)
erosion = cv2.dilate(img,kernel,iterations= 1)
plt.subplot(121),plt.imshow(img),plt.title('original'),plt.xticks([]),plt.yticks([])
plt.subplot(122),plt.imshow(erosion),plt.title('dilate'),plt.xticks([]),plt.yticks([])
plt.show()
开运算
向侵蚀后扩展,用于消除噪音
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('number.jpg')
kernel = np.ones((5,5),np.uint8)
erosion = cv2.morphologyEx(img,cv2.MORPH_OPEN,kernel)
plt.subplot(121),plt.imshow(img),plt.title('original'),plt.xticks([]),plt.yticks([])
plt.subplot(122),plt.imshow(erosion),plt.title('openning'),plt.xticks([]),plt.yticks([])
plt.show()
闭运算
与开运算相反
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('number.jpg')
kernel = np.ones((5,5),np.uint8)
erosion = cv2.morphologyEx(img,cv2.MORPH_CLOSE,kernel)
plt.subplot(121),plt.imshow(img),plt.title('original'),plt.xticks([]),plt.yticks([])
plt.subplot(122),plt.imshow(erosion),plt.title('close'),plt.xticks([]),plt.yticks([])
plt.show()
形态学梯度
利用对图像的膨胀和腐蚀的组合使用,从而提取了物体的轮廓
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('number.jpg')
kernel = np.ones((5,5),np.uint8)
erosion = cv2.morphologyEx(img,cv2.MORPH_GRADIENT,kernel)
plt.subplot(121),plt.imshow(img),plt.title('original'),plt.xticks([]),plt.yticks([])
plt.subplot(122),plt.imshow(erosion),plt.title('gradient'),plt.xticks([]),plt.yticks([])
plt.show()
顶帽
礼帽指的是原始图像与其进行开运算后的图像进行一个差,对于差别之处显示其原有图色
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('number.jpg')
kernel = np.ones((5,5),np.uint8)
openning = cv2.morphologyEx(img,cv2.MORPH_OPEN,kernel)
tophat = cv2.morphologyEx(img,cv2.MORPH_TOPHAT,kernel)
plt.subplot(131),plt.imshow(img),plt.title('original'),plt.xticks([]),plt.yticks([])
plt.subplot(132),plt.imshow(openning),plt.title('openning'),plt.xticks([]),plt.yticks([])
plt.subplot(133),plt.imshow(tophat),plt.title('tophat'),plt.xticks([]),plt.yticks([])
plt.show()
黑帽
黑帽指的是原始图像与其进行闭运算后的图像进行一个差,对于差别之处显示原有图色的反颜色
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('number.jpg')
kernel = np.ones((5,5),np.uint8)
close = cv2.morphologyEx(img,cv2.MORPH_CLOSE,kernel)
blackhat = cv2.morphologyEx(img,cv2.MORPH_BLACKHAT,kernel)
plt.subplot(131),plt.imshow(img),plt.title('original'),plt.xticks([]),plt.yticks([])
plt.subplot(132),plt.imshow(close),plt.title('close'),plt.xticks([]),plt.yticks([])
plt.subplot(133),plt.imshow(blackhat),plt.title('blackhat'),plt.xticks([]),plt.yticks([])
plt.show()