# 二、膨胀操作（dilate）

3×3的卷积核

`以3×3为卷积核从左往右(从上往下)开始运行，若这卷积核内存在1，则用1替代中心元素。`

## 膨胀的作用：

`二值图`

``````import cv2 as cv
import numpy as np

def dilate(image):
print(image.shape)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
cv.imshow("binary", binary)
kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
dst = cv.dilate(binary, kernel)
cv.imshow("dilate", dst)

cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
dilate(src)

cv.waitKey(0)
cv.destroyAllWindows()

``````

·彩色图·

``````import cv2 as cv
import numpy as np

cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)

kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
dst = cv.dilate(src, kernel)
cv.imshow("result", dst)

cv.waitKey(0)
cv.destroyAllWindows()
``````

# 三、腐蚀操作（erode）

3×3的卷积核

`以3×3为卷积核从左往右(从上往下)开始运行，若这卷积核内存在0，则用0替代中心元素。`

`二值图`

``````import cv2 as cv
import numpy as np

def erode(image):
print(image.shape)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)#图像二值化
cv.imshow("binary", binary)
kernel = cv.getStructuringElement(cv.MORPH_RECT, (15, 15))
dst = cv.erode(binary, kernel)
cv.imshow("erode", dst)

cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
erode_demo(src)

cv.waitKey(0)
cv.destroyAllWindows()
``````

`彩色图`

``````import cv2 as cv
import numpy as np

cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)

kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
dst = cv.erode(src, kernel)
cv.imshow("erode", dst)

cv.waitKey(0)
cv.destroyAllWindows()
``````

`总结：膨胀---白变多黑变少、腐蚀---黑变多白变少`

# 四、开操作（open）

`cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)`，若出现反转可以将参数设置为`cv.THRESH_BINARY_INV`

``````import cv2 as cv
import numpy as np

def open(image):
print(image.shape)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
cv.imshow("binary", binary)
kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (5, 5))
binary = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel)
cv.imshow("open-result", binary)

cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
open(src)

cv.waitKey(0)
cv.destroyAllWindows()
``````

# 五、闭操作（close）

``````import cv2 as cv
import numpy as np

def close(image):
print(image.shape)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
cv.imshow("binary", binary)
kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))
binary = cv.morphologyEx(binary, cv.MORPH_CLOSE, kernel)
cv.imshow("close_demo", binary)

cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
close(src)

cv.waitKey(0)
cv.destroyAllWindows()
``````

`总结：开操作消除白点，闭操作消除黑点`

# 六、顶帽（tophat）

`彩色图像`

``````import cv2 as cv
import numpy as np

def tophat(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
kernel = cv.getStructuringElement(cv.MORPH_RECT, (15, 15))
dst = cv.morphologyEx(gray, cv.MORPH_TOPHAT, kernel)
"""
若图片不够亮可以对图像进行整体增加亮度
cimage = np.array(gray.shape, np.uint8)
cimage = 120;
"""
cv.imshow("tophat", dst)

cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
tophat(src)
cv.waitKey(0)

cv.destroyAllWindows()

``````

`二值图像`

``````import cv2 as cv
import numpy as np

def tophat_binary(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))
dst = cv.morphologyEx(binary, cv.MORPH_TOPHAT, kernel)
cv.imshow("tophat_binary", dst)

cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
tophat_binary(src)
cv.waitKey(0)

cv.destroyAllWindows()
``````

# 七、黑帽（blackhat）

`彩色图像`

``````import cv2 as cv
import numpy as np

def blackhat(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
kernel = cv.getStructuringElement(cv.MORPH_RECT, (15, 15))
dst = cv.morphologyEx(gray, cv.MORPH_BLACKHAT, kernel)
"""
若图片不够亮可以对图像进行整体增加亮度
cimage = np.array(gray.shape, np.uint8)
cimage = 120;
"""
cv.imshow("blackhat", dst)

cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
blackhat(src)
cv.waitKey(0)

cv.destroyAllWindows()

``````

`二值图像`

``````import cv2 as cv
import numpy as np

def blackhat_binary(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))
dst = cv.morphologyEx(binary, cv.MORPH_BLACKHAT, kernel)
cv.imshow("blackhat_binary", dst)

cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
blackhat_binary(src)
cv.waitKey(0)

cv.destroyAllWindows()
``````

`基本梯度`

``````import cv2 as cv
import numpy as np

gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))

cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)

cv.waitKey(0)
cv.destroyAllWindows()
``````

`内梯度和外梯度`

``````import cv2 as cv
import numpy as np

kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
dm = cv.dilate(image, kernel)
em = cv.erode(image, kernel)
dst1 = cv.subtract(image, em) # internal gradient
dst2 = cv.subtract(dm, image) # external gradient
cv.imshow("internal", dst1)
cv.imshow("external", dst2)

cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)

cv.waitKey(0)
cv.destroyAllWindows()
``````

THE END