由于日常工作使用python居多,遂选择使用python完成算法相关部分的实验。
0.解析.raw文件
import struct
# 打开文件
with open('path/of/img.raw', 'rb') as file:
# 读取文件内容
data = file.read()
img = []
# 解析二进制数据
for byte in data:
# 使用struct.unpack将二进制数据转换为无符号整数
value = struct.unpack('B', bytes([byte]))[0]
img.append(value)
1.求解图像直方图
hit = [0] * 256
for i in img:
hit[i]+=1
2.合理显示直方图
import matplotlib.pyplot as plt
indices = list(range(len(hit)))
# 绘制竖线图
plt.bar(indices, data)
# 显示图形
plt.show()