diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..3d8329d --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,12 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Python Debugger: Current File", + "type": "debugpy", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..b881eff --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.analysis.autoImportCompletions": true +} \ No newline at end of file diff --git a/1/4.ipynb b/1/4.ipynb new file mode 100644 index 0000000..78f82d4 --- /dev/null +++ b/1/4.ipynb @@ -0,0 +1,140 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "hellohellohello\n" + ] + } + ], + "source": [ + "print(\"hello\" * 3)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "x = 2, y = 3.000000\n" + ] + } + ], + "source": [ + "print(\"x = %d, y = %f\" % (2, 3.0))" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(2-3j)\n" + ] + } + ], + "source": [ + "x = 3 + 2j\n", + "y = -1j\n", + "print(x * y)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "abcdefg\n", + "bc\n", + "bcdefg\n", + "abc\n", + "abcdef\n", + "ef\n", + "efg\n", + "abcdefg\n", + "aceg\n", + "gfedcba\n" + ] + } + ], + "source": [ + "strs = \"abcdefg\"\n", + "print(strs[0:7:1])\n", + "print(strs[1:3])\n", + "print(strs[1:])\n", + "print(strs[:3])\n", + "print(strs[:-1])\n", + "print(strs[-3:-1])\n", + "print(strs[-3:])\n", + "print(strs[:])\n", + "print(strs[::2])\n", + "print(strs[::-1])" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "较小值为: 5.7\n" + ] + } + ], + "source": [ + "def minimal(x, y):\n", + " if x > y:\n", + " print(\"较小值为: \", y)\n", + " else:\n", + " print(\"较小值为: \", x)\n", + "\n", + "\n", + "a = float(input(\"请输入第一个数据: \")) # 输入了 5.4\n", + "b = float(input(\"请输入第二个数据: \")) # 输入了 6.8\n", + "minimal(a, b)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/1/5.py b/1/5.py new file mode 100644 index 0000000..3b14152 --- /dev/null +++ b/1/5.py @@ -0,0 +1,9 @@ +import cv2 + +lenna = cv2.imread(r"img\Lenna.png") +print(type(lenna)) +cv2.namedWindow("Lena", cv2.WINDOW_AUTOSIZE) +cv2.imshow("Lena", lenna) +cv2.waitKey(0) +cv2.destroyWindow("Lena") +cv2.imwrite(r"img\test_imwrite.png", lenna, (cv2.IMWRITE_PNG_COMPRESSION, 5)) diff --git a/1/6.py b/1/6.py new file mode 100644 index 0000000..bb83d43 --- /dev/null +++ b/1/6.py @@ -0,0 +1,9 @@ +import cv2 +import matplotlib.pyplot as plt + +plt.rcParams["font.family"] = ["SimHei"] +plt.rcParams["axes.unicode_minus"] = False +img_BGR = cv2.imread(r"img\iris.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 new file mode 100644 index 0000000..973c247 --- /dev/null +++ b/1/7.py @@ -0,0 +1,36 @@ +import cv2 +import matplotlib.pyplot as plt +import math +import numpy as np + + +def createBox(): + box = np.zeros((100, 100), np.uint8) + 255 + print(type(box)) + shape = box.shape + box = cv2.circle(box, (30, 50), 25, 0, -1) + for i in range(shape[0]): + for j in range(shape[1]): + if j in range(45, 95) and i in range(25, 75): + box[i, j] = 195 + return box + + +def histogram(image): + (row, col) = image.shape + hist = [0] * 256 + for i in range(row): + for j in range(col): + hist[image[i, j]] += 1 + 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() diff --git a/1/img/Lenna.png b/1/img/Lenna.png new file mode 100644 index 0000000..1538e24 Binary files /dev/null and b/1/img/Lenna.png differ diff --git a/1/img/iris.jpg b/1/img/iris.jpg new file mode 100644 index 0000000..847face Binary files /dev/null and b/1/img/iris.jpg differ diff --git a/2/1.py b/2/1.py new file mode 100644 index 0000000..d1ece85 --- /dev/null +++ b/2/1.py @@ -0,0 +1,50 @@ +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): + img = im.copy() + maxV = img.max() + minV = img.min() + if maxV == minV: + return np.uint8(img) + for i in range(img.shape[0]): + for j in range(img.shape[1]): + img[i, j] = ((d - c) / (maxV - minV)) * (img[i, j] - minV) + c + return np.uint8(img) + + +def histogram_equalization(im): + return np.uint8(cv.equalizeHist(im)) + + +if __name__ == "__main__": + im = cv.imread(r"E:\OneDrive\Code\Python\2\iris.jpg", cv.IMREAD_GRAYSCALE) + im1 = global_linear_transmation(im, 0, 150) + im2 = global_linear_transmation(im, 100) + im3 = global_linear_transmation(im, 50, 150) + im4 = histogram_equalization(im) + plt.figure() + plt.subplot(241) + plt.imshow(im1, cmap="gray") + plt.title("darker") + plt.subplot(242) + plt.imshow(im2, cmap="gray") + plt.title("brighter") + plt.subplot(243) + plt.imshow(im3, cmap="gray") + plt.title("lower contrast") + plt.subplot(244) + plt.imshow(im4, cmap="gray") + plt.title("equalized") + plt.subplot(245) + plt.hist(im1.flatten(), 256, [0, 256]) + plt.subplot(246) + plt.hist(im2.flatten(), 256, [0, 256]) + plt.subplot(247) + plt.hist(im3.flatten(), 256, [0, 256]) + plt.subplot(248) + plt.hist(im4.flatten(), 256, [0, 256]) + plt.show() diff --git a/2/2.py b/2/2.py new file mode 100644 index 0000000..aaa6c63 --- /dev/null +++ b/2/2.py @@ -0,0 +1,26 @@ +import numpy as np +import cv2 as cv +import matplotlib.pyplot as plt + + +def gamma_trans(img, gamma=1.0): + gamma_table = [np.power(x / 255.0, gamma) * 255.0 for x in range(256)] + gamma_table = np.round(np.array(gamma_table)).astype(np.uint8) + return cv.LUT(img, gamma_table) + + +if __name__ == "__main__": + im = cv.imread(r"E:\OneDrive\Code\Python\2\iris.jpg", cv.IMREAD_GRAYSCALE) + im1 = gamma_trans(im, 0.5) + im2 = gamma_trans(im, 1.5) + plt.figure() + plt.subplot(131) + plt.imshow(im, cmap="gray") + plt.title("original") + plt.subplot(132) + plt.imshow(im1, cmap="gray") + plt.title("gamma = 0.5") + plt.subplot(133) + plt.imshow(im2, cmap="gray") + plt.title("gamma = 1.5") + plt.show() diff --git a/2/3.py b/2/3.py new file mode 100644 index 0000000..cda9dc5 --- /dev/null +++ b/2/3.py @@ -0,0 +1,28 @@ +import cv2 as cv +from matplotlib import pyplot as plt +import numpy as np + +img = cv.imread(r"E:\OneDrive\Code\Python\2\iris.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() diff --git a/2/4.py b/2/4.py new file mode 100644 index 0000000..e1e4e51 --- /dev/null +++ b/2/4.py @@ -0,0 +1,70 @@ +import random as rd +import numpy as np +import cv2 as cv +import matplotlib.pyplot as plt + + +def addSaltAndPepper(src, percentage): + NoiseImg = src.copy() + NoiseNum = int(percentage * src.shape[0] * src.shape[1]) + for i in range(NoiseNum): + randX = rd.randint(0, src.shape[0] - 1) + randY = rd.randint(0, src.shape[1] - 1) + if rd.randint(0, 1) == 0: + NoiseImg[randX, randY] = 0 + else: + NoiseImg[randX, randY] = 255 + return NoiseImg + + +def addGaussianNoise(src, means, sigma): + NoiseImg = src / src.max() + rows = NoiseImg.shape[0] + cols = NoiseImg.shape[1] + for i in range(rows): + for j in range(cols): + NoiseImg[i, j] = NoiseImg[i, j] + rd.gauss(means, sigma) + if NoiseImg[i, j] < 0: + NoiseImg[i, j] = 0 + if NoiseImg[i, j] > 1: + NoiseImg[i, j] = 1 + NoiseImg = np.uint8(NoiseImg * 255) + return NoiseImg + + +if __name__ == "__main__": + im = cv.imread(r"E:\OneDrive\Code\Python\2\iris.jpg", cv.IMREAD_GRAYSCALE) + im1 = addSaltAndPepper(im, 0.1) + im11 = cv.blur(im1, (3, 3)) + im12 = cv.medianBlur(im1, 3) + im13 = cv.GaussianBlur(im1, (3, 3), 1) + im2 = addGaussianNoise(im, 0, 0.1) + im21 = cv.blur(im2, (3, 3)) + im22 = cv.medianBlur(im2, 3) + im23 = cv.GaussianBlur(im2, (3, 3), 1) + plt.figure() + plt.subplot(241) + plt.imshow(im1, cmap="gray") + plt.title("salt and pepper") + plt.subplot(242) + plt.imshow(im11, cmap="gray") + plt.title("blur") + plt.subplot(243) + plt.imshow(im12, cmap="gray") + plt.title("median") + plt.subplot(244) + plt.imshow(im13, cmap="gray") + plt.title("gaussian") + plt.subplot(245) + plt.imshow(im2, cmap="gray") + plt.title("gaussian noise") + plt.subplot(246) + plt.imshow(im21, cmap="gray") + plt.title("blur") + plt.subplot(247) + plt.imshow(im22, cmap="gray") + plt.title("median") + plt.subplot(248) + plt.imshow(im23, cmap="gray") + plt.title("gaussian") + plt.show() diff --git a/2/5.py b/2/5.py new file mode 100644 index 0000000..816a171 --- /dev/null +++ b/2/5.py @@ -0,0 +1,21 @@ +import cv2 as cv +from matplotlib import pyplot as plt +import numpy as np + +img = cv.imread(r"E:\OneDrive\Code\Python\2\iris.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() diff --git a/2/iris.jpg b/2/iris.jpg new file mode 100644 index 0000000..143b0bb Binary files /dev/null and b/2/iris.jpg differ