假设图像矩阵大小为32×32,将其转换为向量,首先创建1×1024的NumPy数组,然后打开给定的文件,循环读出文件的前32行,并将每行的头32个字符值存储在NumPy数组中
import numpy as np
def img2vector(filename):
returnVect = np.zeros((1, 1024))
fr = open(filename)
for i in range(32):
lineStr = fr.readline()
for j in ran
本文实例主要关于Python实现读取MRI(核磁共振成像)为numpy数组,使用imshow显示为灰度。
代码如下:
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import matplotlib.cm as cm
import numpy as np
# Data are 256x256 16 bit integers
with cbook.get_sample_data('s1045.ima.gz') a