Files
digital-image-processing/6/1.py
T
2024-05-14 10:40:16 +08:00

24 lines
691 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import cv2 as cv
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"] = ["SimSun"]
img = cv.imread(r"img\wirebond-mask.tif", cv.IMREAD_GRAYSCALE)
_, img_binary = cv.threshold(img, 128, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
plt.figure()
plt.subplot(141)
plt.axis("off")
plt.imshow(img_binary, cmap="gray")
plt.title("原图像")
i = 1
for kernelSize in [11, 15, 45]:
kernel = cv.getStructuringElement(cv.MORPH_RECT, (kernelSize, kernelSize))
img_result = cv.erode(img_binary, kernel, iterations=1)
i += 1
plt.subplot(140 + i)
plt.axis("off")
plt.imshow(img_result, cmap="gray")
plt.title(f"{kernelSize}×{kernelSize}")
plt.tight_layout()
plt.show()