From 3ca50553ee3f9c1b45666ab025bb7450ef4881e7 Mon Sep 17 00:00:00 2001 From: Xi Xu Date: Thu, 16 May 2024 09:46:25 +0800 Subject: [PATCH] Add 7 --- 7/1.py | 34 ++++++++++++++++++++++++++++++++++ 7/2.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 7/3.py | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 7/1.py create mode 100644 7/2.py create mode 100644 7/3.py diff --git a/7/1.py b/7/1.py new file mode 100644 index 0000000..bd6a5ee --- /dev/null +++ b/7/1.py @@ -0,0 +1,34 @@ +import cv2 as cv +import numpy as np +import matplotlib.pyplot as plt + +plt.rcParams["font.sans-serif"] = ["SimSun"] + +img = cv.imread(r"img\paopao.jpg", cv.IMREAD_GRAYSCALE) +G1 = np.zeros(img.shape, np.uint8) +G2 = np.zeros(img.shape, np.uint8) +T1 = np.mean(img) # type: ignore +diff = 255 +T0 = 0.01 +while diff > T0: + _, G1 = cv.threshold(img, T1, 255, cv.THRESH_TOZERO_INV) + _, G2 = cv.threshold(img, T1, 255, cv.THRESH_TOZERO) + loc1 = np.where(G1 > 0.001) # type: ignore + loc2 = np.where(G2 > 0.001) # type: ignore + ave1 = np.mean(G1[loc1]) # type: ignore + ave2 = np.mean(G2[loc2]) # type: ignore + T2 = (ave1 + ave2) / 2 + diff = np.abs(T1 - T2) + T1 = T2 +_, img_result = cv.threshold(img, T1, 255, cv.THRESH_BINARY) +plt.figure() +plt.subplot(121) +plt.axis("off") +plt.imshow(img, cmap="gray") +plt.title("原灰度图像") +plt.subplot(122) +plt.axis("off") +plt.imshow(img_result, cmap="gray") +plt.title("迭代全阈值分割二值图像") +plt.tight_layout() +plt.show() diff --git a/7/2.py b/7/2.py new file mode 100644 index 0000000..839809b --- /dev/null +++ b/7/2.py @@ -0,0 +1,53 @@ +import cv2 as cv +import numpy as np +import matplotlib.pyplot as plt + +plt.rcParams["font.sans-serif"] = ["SimSun"] + + +def histogram(img): + row, col = img.shape + hist = [0] * 256 + for i in range(row): + for j in range(col): + hist[img[i, j]] += 1 + return hist + + +if __name__ == "__main__": + img = cv.imread(r"img\polygon_draw.jpg", cv.IMREAD_GRAYSCALE) + img_noisy = np.uint8(img + 0.8 * img.std() * np.random.standard_normal(img.shape)) + img_noisy_blur = cv.GaussianBlur(img_noisy, (9, 9), 0) # type: ignore + _, img_result = cv.threshold(img_noisy, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU) # type: ignore + _, img_result_blur = cv.threshold( + img_noisy_blur, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU + ) + plt.figure() + plt.subplot(231) + plt.axis("off") + plt.imshow(img_noisy, cmap="gray") + plt.title("带噪声的图像") + plt.subplot(232) + plt.xlabel("灰度值") + plt.ylabel("像素个数") + plt.bar(range(256), histogram(img_noisy)) + plt.title("噪声图像直方图") + plt.subplot(233) + plt.axis("off") + plt.imshow(img_result, cmap="gray") + plt.title("带噪声图像的 OTSU 分割") + plt.subplot(234) + plt.axis("off") + plt.imshow(img_noisy_blur, cmap="gray") + plt.title("高斯平滑的图像") + plt.subplot(235) + plt.xlabel("灰度值") + plt.ylabel("像素个数") + plt.bar(range(256), histogram(img_noisy_blur)) + plt.title("平滑图像直方图") + plt.subplot(236) + plt.axis("off") + plt.imshow(img_result_blur, cmap="gray") + plt.title("平滑图像的 OTSU 分割") + plt.tight_layout() + plt.show() diff --git a/7/3.py b/7/3.py new file mode 100644 index 0000000..c24f292 --- /dev/null +++ b/7/3.py @@ -0,0 +1,41 @@ +import cv2 as cv +import matplotlib.pyplot as plt + +plt.rcParams["font.sans-serif"] = ["SimSun"] + +img = cv.imread(r"img\light_circle.jpg", cv.IMREAD_GRAYSCALE) +kernalSize = 19 +img_adapt = cv.adaptiveThreshold( + img, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, kernalSize, 6 +) +img_tophat = cv.morphologyEx( + img, cv.MORPH_TOPHAT, cv.getStructuringElement(cv.MORPH_ELLIPSE, (45, 45)) +) +plt.figure() +plt.subplot(121) +plt.imshow(img, cmap="gray") +plt.title("待处理灰度图像") +plt.axis("off") +plt.subplot(122) +plt.imshow(img_adapt, cmap="gray") +plt.title("自适应阈值分割结果") +plt.axis("off") +plt.tight_layout() +plt.show() + +# # 交互式调整参数 +# kernalSize = 7 +# plt.ion() +# for i in range(10): +# c = 4 +# for j in range(10): +# plt.cla() +# img_seg_adapt = cv.adaptiveThreshold( +# img0, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, kernalSize, c +# ) +# plt.imshow(img_seg_adapt, cmap="gray") +# print(kernalSize, c) +# plt.pause(0.01) +# c += 2 +# kernalSize += 2 +# plt.show()