完成作业一:3-1-3
This commit is contained in:
32
0416 2.py
Normal file
32
0416 2.py
Normal file
@@ -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)
|
||||||
24
0416 3.py
Normal file
24
0416 3.py
Normal file
@@ -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}")
|
||||||
29
0416.py
Normal file
29
0416.py
Normal file
@@ -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)
|
||||||
Reference in New Issue
Block a user