【问题标题】:Read in Raw Binary Image in Python在 Python 中读取原始二进制图像
【发布时间】:2013-07-02 23:46:57
【问题描述】:

我在 Matlab 中有一个非常简单的脚本,可以打开一个“原始”二进制图像文件并显示它。在 python 中使用 numpy 可以轻松重现吗?我遇到过各种讨论解包、处理字节序、指定缓冲区等的帖子。但这似乎应该很简单,基于 matlab 接口的简单程度

>> fileID = fopen('sampleX3.raw','rb')

fileID =

     1

>> A = fread(fileID,[1024,1024],'int16');
size(A)

ans =

        1024        1024

>> max(max(A))

ans =

       12345

>> close all; figure; imagesc(A);

【问题讨论】:

  • 你试过openCV吗?它专为计算机视觉设计......

标签: python image numpy binary


【解决方案1】:

这将使用 numpy 和 matplotlib 做同样的事情:

import numpy as np
from matplotlib import pylab as plt

A = np.fromfile(filename, dtype='int16', sep="")
A = A.reshape([1024, 1024])
plt.imshow(A)

我不得不提一下,使用原始二进制文件存储数据通常是个坏主意。

【讨论】:

  • 为什么说在二进制文件中存储数据是个坏主意?
  • 原始问题有一个方形图像,但请记住 MATLAB 使用行优先顺序,python 使用列优先。您必须交换 Python 中 reshape 函数中的数字和 MATLAB 中给 freadsizeA 参数。
猜你喜欢
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
  • 2016-10-14
  • 1970-01-01
  • 2015-09-01
  • 2018-09-15
  • 1970-01-01
  • 2011-12-09
相关资源
最近更新 更多