From c59622aec4ca13051ef87a002452ba8ad612485b Mon Sep 17 00:00:00 2001 From: Xi Xu Date: Sat, 27 Apr 2024 22:48:30 +0800 Subject: [PATCH] refactor --- 1/4.ipynb | 2 +- 1/5.py | 15 ++++++++------- 1/6.py | 13 +++++++------ 1/7.py | 19 ++++++++++--------- 2/1.py | 1 - 2/3.py | 47 ++++++++++++++++++++++++----------------------- 2/5.py | 33 +++++++++++++++++---------------- 7 files changed, 67 insertions(+), 63 deletions(-) diff --git a/1/4.ipynb b/1/4.ipynb index 86bce9d..8484a6f 100644 --- a/1/4.ipynb +++ b/1/4.ipynb @@ -83,7 +83,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.2" + "version": "3.12.3" } }, "nbformat": 4, diff --git a/1/5.py b/1/5.py index 89cf8d9..7b7337b 100644 --- a/1/5.py +++ b/1/5.py @@ -1,9 +1,10 @@ import cv2 -lenna = cv2.imread(r"src.jpg") -print(type(lenna)) -cv2.namedWindow("Lena", cv2.WINDOW_AUTOSIZE) -cv2.imshow("Lena", lenna) -cv2.waitKey(0) -cv2.destroyWindow("Lena") -cv2.imwrite(r"1\imwrite.png", lenna, (cv2.IMWRITE_PNG_COMPRESSION, 5)) +if __name__ == "__main__": + lenna = cv2.imread(r"src.jpg") + print(type(lenna)) + cv2.namedWindow("Lena", cv2.WINDOW_AUTOSIZE) + cv2.imshow("Lena", lenna) + cv2.waitKey(0) + cv2.destroyWindow("Lena") + cv2.imwrite(r"1\imwrite.png", lenna, (cv2.IMWRITE_PNG_COMPRESSION, 5)) diff --git a/1/6.py b/1/6.py index 7087944..a5b0040 100644 --- a/1/6.py +++ b/1/6.py @@ -1,9 +1,10 @@ import cv2 import matplotlib.pyplot as plt -plt.rcParams["font.family"] = ["SimHei"] -plt.rcParams["axes.unicode_minus"] = False -img_BGR = cv2.imread(r"src.jpg") -img_RGB = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2RGB) -plt.imshow(img_RGB) -plt.show() +if __name__ == "__main__": + plt.rcParams["font.family"] = ["SimHei"] + plt.rcParams["axes.unicode_minus"] = False + img_BGR = cv2.imread(r"src.jpg") + img_RGB = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2RGB) + plt.imshow(img_RGB) + plt.show() diff --git a/1/7.py b/1/7.py index 973c247..3d26922 100644 --- a/1/7.py +++ b/1/7.py @@ -25,12 +25,13 @@ def histogram(image): return hist -image0 = createBox() -plt.figure() -plt.subplot(1, 2, 1) -plt.imshow(image0, vmin=0, vmax=255, cmap=plt.cm.gray) -plt.title("idel image") -image_hist0 = histogram(image0) -plt.subplot(1, 2, 2) -plt.bar(range(256), image_hist0) -plt.show() +if __name__ == "__main__": + image0 = createBox() + plt.figure() + plt.subplot(1, 2, 1) + plt.imshow(image0, vmin=0, vmax=255, cmap=plt.cm.gray) + plt.title("idel image") + image_hist0 = histogram(image0) + plt.subplot(1, 2, 2) + plt.bar(range(256), image_hist0) + plt.show() diff --git a/2/1.py b/2/1.py index b1b8b89..0138107 100644 --- a/2/1.py +++ b/2/1.py @@ -1,7 +1,6 @@ import numpy as np import cv2 as cv import matplotlib.pyplot as plt -from sympy import im def global_linear_transmation(im, c=0, d=255): diff --git a/2/3.py b/2/3.py index 793fc78..e46c4df 100644 --- a/2/3.py +++ b/2/3.py @@ -2,27 +2,28 @@ import cv2 as cv from matplotlib import pyplot as plt import numpy as np -img = cv.imread(r"src.jpg", 0) -fil1 = 1 / 16 * np.array([[1, 2, 1], [2, 4, 2], [1, 2, 1]]) -fil2 = 1 / 9 * np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) -fil3 = 1 / 10 * np.array([[1, 1, 1], [1, 2, 1], [1, 1, 1]]) -fil4 = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]]) -ImgSmoothed1 = cv.filter2D(img, -1, fil1, borderType=cv.BORDER_DEFAULT) -ImgSmoothed2 = cv.filter2D(img, -1, fil2, borderType=cv.BORDER_DEFAULT) -ImgSmoothed3 = cv.filter2D(img, -1, fil3, borderType=cv.BORDER_DEFAULT) -ImgSharp = cv.filter2D(img, -1, fil4, borderType=cv.BORDER_DEFAULT) +if __name__ == "__main__": + img = cv.imread(r"src.jpg", 0) + fil1 = 1 / 16 * np.array([[1, 2, 1], [2, 4, 2], [1, 2, 1]]) + fil2 = 1 / 9 * np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]]) + fil3 = 1 / 10 * np.array([[1, 1, 1], [1, 2, 1], [1, 1, 1]]) + fil4 = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]]) + ImgSmoothed1 = cv.filter2D(img, -1, fil1, borderType=cv.BORDER_DEFAULT) + ImgSmoothed2 = cv.filter2D(img, -1, fil2, borderType=cv.BORDER_DEFAULT) + ImgSmoothed3 = cv.filter2D(img, -1, fil3, borderType=cv.BORDER_DEFAULT) + ImgSharp = cv.filter2D(img, -1, fil4, borderType=cv.BORDER_DEFAULT) -plt.figure() -plt.subplot(221) -plt.imshow(ImgSmoothed1, cmap="gray") -plt.title("smoothed1") -plt.subplot(222) -plt.imshow(ImgSmoothed2, cmap="gray") -plt.title("smoothed2") -plt.subplot(223) -plt.imshow(ImgSmoothed3, cmap="gray") -plt.title("smoothed3") -plt.subplot(224) -plt.imshow(ImgSharp, cmap="gray") -plt.title("sharp") -plt.show() + plt.figure() + plt.subplot(221) + plt.imshow(ImgSmoothed1, cmap="gray") + plt.title("smoothed1") + plt.subplot(222) + plt.imshow(ImgSmoothed2, cmap="gray") + plt.title("smoothed2") + plt.subplot(223) + plt.imshow(ImgSmoothed3, cmap="gray") + plt.title("smoothed3") + plt.subplot(224) + plt.imshow(ImgSharp, cmap="gray") + plt.title("sharp") + plt.show() diff --git a/2/5.py b/2/5.py index 9918d9b..d63837f 100644 --- a/2/5.py +++ b/2/5.py @@ -2,20 +2,21 @@ import cv2 as cv from matplotlib import pyplot as plt import numpy as np -img = cv.imread(r"src.jpg", 0) -lplc = np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]]) -lplcEnhance = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) -ImgLplc = cv.filter2D(img, -1, lplc, borderType=cv.BORDER_DEFAULT) -ImgLplcEnhance = cv.filter2D(img, -1, lplcEnhance, borderType=cv.BORDER_DEFAULT) +if __name__ == "__main__": + img = cv.imread(r"src.jpg", 0) + lplc = np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]]) + lplcEnhance = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) + ImgLplc = cv.filter2D(img, -1, lplc, borderType=cv.BORDER_DEFAULT) + ImgLplcEnhance = cv.filter2D(img, -1, lplcEnhance, borderType=cv.BORDER_DEFAULT) -plt.figure() -plt.subplot(131) -plt.imshow(img, cmap="gray") -plt.title("original") -plt.subplot(132) -plt.imshow(ImgLplc, cmap="gray") -plt.title("laplacian") -plt.subplot(133) -plt.imshow(ImgLplcEnhance, cmap="gray") -plt.title("laplacian enhanced") -plt.show() + plt.figure() + plt.subplot(131) + plt.imshow(img, cmap="gray") + plt.title("original") + plt.subplot(132) + plt.imshow(ImgLplc, cmap="gray") + plt.title("laplacian") + plt.subplot(133) + plt.imshow(ImgLplcEnhance, cmap="gray") + plt.title("laplacian enhanced") + plt.show()