From 7a0ea130aa5f6921bcb9e061981bb0042c70a305 Mon Sep 17 00:00:00 2001 From: 2509165030 <2509165030@student.edu.cn> Date: Thu, 16 Apr 2026 16:05:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BD=9C=E4=B8=9AX?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 9999.py | 32 ++++++++++++++++++++++++++++++++ import numpy as nddd.py | 21 +++++++++++++++++++++ import numpy as np.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 9999.py create mode 100644 import numpy as nddd.py create mode 100644 import numpy as np.py diff --git a/9999.py b/9999.py new file mode 100644 index 0000000..7834707 --- /dev/null +++ b/9999.py @@ -0,0 +1,32 @@ +import numpy as np + +# 定义4×4图像矩阵 +img = np.array([ + [255, 255, 0, 0 ], + [255, 255, 0, 0 ], + [0, 0, 255, 255], + [0, 0, 255, 255] +], dtype=np.uint8) + +print("原始图像:") +print(img) +print("-" * 30) + +# 1. 统计白色(255)和黑色(0)像素数量 +white_pixels = np.sum(img == 255) # 等于255的元素求和 +black_pixels = np.sum(img == 0) # 等于0的元素求和 +print(f"白色像素(255)数量:{white_pixels}") +print(f"黑色像素(0)数量:{black_pixels}") +print("-" * 30) + +# 2. 水平翻转(左右翻转) +img_flip_lr = np.fliplr(img) +print("水平翻转后的图像:") +print(img_flip_lr) +print("-" * 30) + +# 3. 逆时针旋转90度(转置 + 上下翻转) +# 方法:先转置,再上下翻转 +img_rot90_ccw = np.flipud(img.T) +print("逆时针旋转90度后的图像:") +print(img_rot90_ccw) \ No newline at end of file diff --git a/import numpy as nddd.py b/import numpy as nddd.py new file mode 100644 index 0000000..d1606a7 --- /dev/null +++ b/import numpy as nddd.py @@ -0,0 +1,21 @@ +import numpy as np + +# 假设这是从图像中提取的2个特征图 +feature_map1 = np.array([[1, 0, 1], [0, 1, 0], [1, 0, 1]]) +feature_map2 = np.array([[1, 1, 1], [1, 0, 0], [1, 0, 0]]) + +# 补全代码:将特征图展平为向量 +vector1 = feature_map1.flatten() # 展平 +vector2 = feature_map2.flatten() # 展平 + +print("vector1:", vector1) +print("vector2:", vector2) +print("-" * 40) + +# 1. 计算欧几里得距离 +euclidean_dist = np.linalg.norm(vector1 - vector2) +print("欧几里得距离:", euclidean_dist) + +# 2. 计算余弦相似度 +cos_sim = np.dot(vector1, vector2) / (np.linalg.norm(vector1) * np.linalg.norm(vector2)) +print("余弦相似度:", cos_sim) \ No newline at end of file diff --git a/import numpy as np.py b/import numpy as np.py new file mode 100644 index 0000000..e065bbd --- /dev/null +++ b/import numpy as np.py @@ -0,0 +1,30 @@ +import numpy as np + +# 定义原始图像矩阵 +image = np.array([ + [100, 150, 200], + [80, 120, 180], + [60, 90, 140] +], dtype=np.uint8) + +print("原图:") +print(image) +print("-" * 20) + +# 1. 变暗20:每个像素值减20 +# 注意:uint8类型不会出现负数,自动取模(小于0会变成255附近) +image_dark = image - 20 +print("1. 变暗20后的图像:") +print(image_dark) +print("-" * 20) + +# 2. 裁剪左上角:保留 image[0:2, 0:2] +image_crop = image[0:2, 0:2] +print("2. 裁剪左上角2*2区域:") +print(image_crop) +print("-" * 20) + +# 3. 水平翻转:使用 np.fliplr() +image_flip = np.fliplr(image) +print("3. 水平翻转后的图像:") +print(image_flip) \ No newline at end of file