簡體   English   中英

等同於在MATLAB for Python中讀取原始字節文件

[英]Equivalent Of Reading In Raw Byte Files In MATLAB for Python

我有原始字節文件,這些文件具有28x28字節的塊,它們代表一個圖像。 每個文件中有1000個這些塊,這些只是我要分析的數據。 因此,例如“數據”具有1000個28x28字節塊,代表1000個28x28像素圖像。

現在,我知道如何使用MATLAB來閱讀它:

fid=fopen(‘data’,’r’); // open the file 
[t1,N]=fread(fid,[28 28],’uchar’); // read in the first example and store it in a 28x28 size matrix t1
[t2,N]=fread(fid,[28 28],uchar); // read the second example into t2 and so on
//To display the image use imshow(t1) or imagesc(t1)

我想知道如何用Python做同樣的事情。 我無法使其正常工作。

我這樣做可以使它正常工作。

fid = open('data/data' + str(digit), 'rb')
dim = np.fromfile(fid, dtype=np.uint8) # read in entire dataset

# loop through the thousand examples and smartly index them to get 28x28 images
for i in xrange(0, 1000):
    # indices to go through data file
    index = i*28*28        
    nextindex = (i+1)*28*28

    # get the images in 28x28 format & convert all pixels to binary
    newdim = dim[index:nextindex]
    newdim = newdim.reshape(28, 28)

我沒有嘗試逐字節讀取文件,而是讀取了整個文件,然后一次索引28 * 28。 如果我在這里的任何地方出錯,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM