完成作业一: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)
|
||||
Reference in New Issue
Block a user