图像开运算代码
开运算其实就是先做侵蚀运算,再做扩张运算,闭运算顺序相反
import numpy as np
import matplotlib.pyplot as plt
import scipy
image = np.array([[0, 1, 1, 1, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 1, 1, 1, 0]])
opening1 = scipy.ndimage.grey_erosion(image, size=(1,1))
opening1 = scipy.ndimage.grey_dilation(opening1, footprint=np.ones((1,1)))
opening1 = Image.fromarray(opening1)
opening2 = scipy.ndimage.grey_erosion(image, size=(2,2))
opening2 = scipy.ndimage.grey_dilation(opening2, footprint=np.ones((2,2)))
opening2 = Image.fromarray(opening2)
opening3 = scipy.ndimage.grey_erosion(image, size=(3,3))
opening3 = scipy.ndimage.grey_dilation(opening3, footprint=np.ones((3,3)))
opening3 = Image.fromarray(opening3)
plt.subplot(1, 3, 1)
plt.imshow(opening1)
plt.subplot(1, 3, 2)
plt.imshow(opening2)
plt.subplot(1, 3, 3)
plt.imshow(opening3)
imageArray1 = np.array(opening1)
print("Opening Image1: \n", imageArray1)
imageArray2 = np.array(opening2)
print("Opening Image2: \n", imageArray2)
imageArray3 = np.array(opening3)
print("Opening Image3: \n", imageArray3)
这图像,imshow了个寂寞哈哈。。。
Opening Image1:
[[0 1 1 1 0]
[0 0 1 0 0]
[0 0 1 0 0]
[0 0 1 0 0]
[0 1 1 1 0]]
Opening Image2:
[[0 1 1 1 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]]
Opening Image3:
[[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]
[0 0 0 0 0]]
从结果看,开运算的size越大,效果越强。