完成作业X
This commit is contained in:
32
9999.py
Normal file
32
9999.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_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)
|
||||||
21
import numpy as nddd.py
Normal file
21
import numpy as nddd.py
Normal file
@@ -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)
|
||||||
30
import numpy as np.py
Normal file
30
import numpy as np.py
Normal file
@@ -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)
|
||||||
Reference in New Issue
Block a user