Files
task-3-1-3-Matrix-Fundament…/9999.py
2026-04-16 16:05:17 +08:00

32 lines
850 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)