完成作业3-1-3
This commit is contained in:
25
练习1.py
Normal file
25
练习1.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import numpy as np
|
||||
|
||||
image = np.array([
|
||||
[100, 150, 200],
|
||||
[80, 120, 180],
|
||||
[60, 90, 140]
|
||||
], dtype=np.uint8)
|
||||
|
||||
print("原图:")
|
||||
print(image)
|
||||
print()
|
||||
|
||||
darkened = np.clip(image.astype(np.int16) - 20, 0, 255).astype(np.uint8)
|
||||
print("变暗20后的图像:")
|
||||
print(darkened)
|
||||
print()
|
||||
|
||||
cropped = image[0:2, 0:2]
|
||||
print("裁剪左上角(2×2区域):")
|
||||
print(cropped)
|
||||
print()
|
||||
|
||||
flipped = np.fliplr(image)
|
||||
print("水平翻转后的图像:")
|
||||
print(flipped)
|
||||
30
练习2.py
Normal file
30
练习2.py
Normal file
@@ -0,0 +1,30 @@
|
||||
import numpy as np
|
||||
|
||||
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()
|
||||
|
||||
white_pixels = np.sum(img == 255)
|
||||
black_pixels = np.sum(img == 0)
|
||||
|
||||
print(f"白色像素(255)的数量:{white_pixels}")
|
||||
print(f"黑色像素(0)的数量:{black_pixels}")
|
||||
print()
|
||||
|
||||
|
||||
flipped_horizontal = np.fliplr(img)
|
||||
print("水平翻转后的图像:")
|
||||
print(flipped_horizontal)
|
||||
print()
|
||||
|
||||
rotated_ccw = np.flipud(img.T)
|
||||
|
||||
print("逆时针旋转90度后的图像:")
|
||||
print(rotated_ccw)
|
||||
23
练习3.py
Normal file
23
练习3.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import numpy as np
|
||||
|
||||
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()
|
||||
|
||||
euclidean_distance = np.linalg.norm(vector1 - vector2)
|
||||
print(f"欧几里得距离: {euclidean_distance:.4f}")
|
||||
|
||||
dot_product = np.dot(vector1, vector2)
|
||||
norm1 = np.linalg.norm(vector1)
|
||||
norm2 = np.linalg.norm(vector2)
|
||||
cosine_similarity = dot_product / (norm1 * norm2)
|
||||
print(f"余弦相似度: {cosine_similarity:.4f}")
|
||||
|
||||
cosine_distance = 1 - cosine_similarity
|
||||
print(f"余弦距离: {cosine_distance:.4f}")
|
||||
Reference in New Issue
Block a user