From bc6c115d453538b59eeb3b40371759e49aecd716 Mon Sep 17 00:00:00 2001 From: 2509165031 <2509165031@student.edu.cn> Date: Thu, 16 Apr 2026 15:54:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BD=9C=E4=B8=9A=E4=B8=80?= =?UTF-8?q?=EF=BC=9A3-1-3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 0416 2.py | 32 ++++++++++++++++++++++++++++++++ 0416 3.py | 24 ++++++++++++++++++++++++ 0416.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 0416 2.py create mode 100644 0416 3.py create mode 100644 0416.py diff --git a/0416 2.py b/0416 2.py new file mode 100644 index 0000000..dd70bc0 --- /dev/null +++ b/0416 2.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_count = np.sum(img == 255) +black_count = np.sum(img == 0) +print(f"白色像素(255)数量:{white_count}") +print(f"黑色像素(0)数量:{black_count}") +print("-" * 30) + +# 2. 水平翻转并打印 +img_flip = np.fliplr(img) +print("水平翻转后图像:") +print(img_flip) +print("-" * 30) + +# 3. 逆时针旋转90度(转置+上下翻转) +# 方法:先转置,再上下翻转(np.flipud) +img_rot90_ccw = np.flipud(img.T) +print("逆时针旋转90度后图像:") +print(img_rot90_ccw) \ No newline at end of file diff --git a/0416 3.py b/0416 3.py new file mode 100644 index 0000000..993b47d --- /dev/null +++ b/0416 3.py @@ -0,0 +1,24 @@ +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]]) + +# 1) 全代码:将特征图展平为向量 +vector1 = feature_map1.flatten() # 展平 +vector2 = feature_map2.flatten() # 展平 + +print("vector1:", vector1) +print("vector2:", vector2) +print("-" * 40) + +# 1. 计算 vector1 和 vector2 的欧几里得距离 +euclidean_dist = np.linalg.norm(vector1 - vector2) +print(f"欧几里得距离: {euclidean_dist:.4f}") + +# 2. 计算 vector1 和 vector2 的余弦相似度 +dot_product = np.dot(vector1, vector2) +norm_a = np.linalg.norm(vector1) +norm_b = np.linalg.norm(vector2) +cos_similarity = dot_product / (norm_a * norm_b) +print(f"余弦相似度: {cos_similarity:.4f}") \ No newline at end of file diff --git a/0416.py b/0416.py new file mode 100644 index 0000000..690d3fa --- /dev/null +++ b/0416.py @@ -0,0 +1,29 @@ +import numpy as np + +# 1. 定义原始3×3灰度图像 +image = np.array([ + [100, 150, 200], + [80, 120, 180], + [60, 90, 140] +], dtype=np.uint8) + +print("原图:") +print(image) +print("-" * 20) + +# 2. 变暗20:每个像素值减20(uint8会自动截断负数,这里所有值减20后均为正,无溢出) +image_dark = image - 20 +print("变暗20后:") +print(image_dark) +print("-" * 20) + +# 3. 裁剪左上角2×2区域:image[0:2, 0:2] +image_crop = image_dark[0:2, 0:2] +print("裁剪左上角2×2后:") +print(image_crop) +print("-" * 20) + +# 4. 水平翻转:使用np.fliplr() +image_flip = np.fliplr(image_crop) +print("水平翻转后:") +print(image_flip) \ No newline at end of file