与 ImageJ 相比,在 Python 上打开原始图像会产生不同的图像

Opening raw images on Python resulting in a different image compared to ImageJ

我写这个脚本是为了打开原始图像并进行一些处理。

import numpy as np 
import matplotlib.pyplot as plt
PATH = "C:\Users\Documents\script_testing_folder\"
IMAGE_PATH = PATH +"simulation01\15x15_output_det_00001_raw_df_00000.bin"
raw_image = np.fromfile(IMAGE_PATH, dtype=np.uint64)
raw_image.shape = (15,15)
plt.imshow(raw_image,cmap = 'gray')
total_intensity = ndimage.sum(raw_image)
print total_intensity
plt.show()

使用这个脚本我得到了这样的图像:

相比之下...当我在 ImageJ(文件>导入>原始(64 位真实,15x15 长度和宽度))上打开相同的图像时,我有这个:

我试过环顾四周,但我不确定在 python 上重现相同图像时哪里出错了。任何帮助将不胜感激。

此外,当我使用以下方法对图像中的强度求和时:

total_intensity = ndimage.sum(raw_image)
print total_intensity

是 我得到 4200794456581938015,而在 ImageJ 上我得到 0.585。

我不确定这些步骤哪里出错了...

谢谢!

编辑:原始文件如果有人想重现我得到的结果https://www.dropbox.com/s/po82z4uf2ku7k0e/15x15_output_det_00001_raw_df_00000.bin?dl=0

问题是endianness of your data (the order of the single bytes of a 64bit float). Fortunately, numpy has the functionality解决这个问题:

import numpy as np 
import matplotlib.pyplot as plt

# load the image
raw_image = np.fromfile('15x15_output_det_00001_raw_df_00000.bin')
raw_image = np.reshape(raw_image, (15, 15))

# swap the byte order
raw_image = raw_image.byteswap()

# output the sum of the intensities to check
total_intensity = np.sum(raw_image)
print "total intensity:", total_intensity

# plot the image
plt.imshow(raw_image,cmap = 'gray', interpolation='nearest')
plt.show()

输出:

total intensity: 0.585123878711

结果: