Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 问题当我将np数组写入二进制文件时,新文件只有原始文件的一半_Python_Opencv_Binaryfiles_Cv2_Raw - Fatal编程技术网

Python 问题当我将np数组写入二进制文件时,新文件只有原始文件的一半

Python 问题当我将np数组写入二进制文件时,新文件只有原始文件的一半,python,opencv,binaryfiles,cv2,raw,Python,Opencv,Binaryfiles,Cv2,Raw,我试图删除原始文件的前24行,因此我打开了原始原始文件(我们称之为raw1.raw)并将其转换为nparray,然后初始化了一个新数组并删除了前24行,但在将新数组写入新的二进制文件(raw2.raw)后,我发现raw2仅为15.2mb,而原始文件raw1.raw类似于30.6mb,我的代码: import numpy as np import imageio import rawpy import cv2 def ave(): fd = open('raw1.raw',

我试图删除原始文件的前24行,因此我打开了原始原始文件(我们称之为raw1.raw)并将其转换为nparray,然后初始化了一个新数组并删除了前24行,但在将新数组写入新的二进制文件(raw2.raw)后,我发现raw2仅为15.2mb,而原始文件raw1.raw类似于30.6mb,我的代码:

import numpy as np
import imageio
import rawpy
import cv2


def ave():
    
    fd = open('raw1.raw', 'rb')
    rows = 3000 #around 3000, not the real rows
    cols = 5100 #around 5100, not the real cols
    f = np.fromfile(fd, dtype=np.uint8,count=rows*cols)
    I_array = f.reshape((rows, cols)) #notice row, column format
    #print(I_array)
   
    fd.close()

    im = np.zeros((rows - 24 , cols))
    for i in range (len(I_array) - 24):
        for j in range(len(I_array[i])):
            im[i][j] = I_array[i + 24][j]
            
    #print(im)

    newFile = open("raw2.raw", "wb")
    
    im.astype('uint8').tofile(newFile)
    newFile.close()


if __name__ == "__main__":
    ave()

在写入二进制文件时,我尝试使用im.astype('uint16'),但如果使用uint16,则该值将是错误的。

您的“raw1.raw”文件中肯定有更多未使用的数据。您确定该文件不是使用“uint16”数据创建的,而您只是将前半部分作为“uint8”数据取出吗?我刚刚检查了随机数据的写入情况

import os, numpy as np

x = np.random.randint(0,256,size=(3000,5100),dtype='uint8')
x.tofile(open('testfile.raw','w'))
print(os.stat('testfile.raw').st_size) #I get 15.3MB. 
因此,对于3000×5100的硬盘,“uint8”显然占用了15.3MB的空间。我不知道你是怎么得到30+的

############################编辑#########

只是补充说明一下。您是否意识到,dtype只不过是更改数据的“视图”而已?它不会影响存储在内存中的实际数据。这也适用于从文件读取的数据。例如:

import numpy as np

#The way to understand x, is that x is taking 12 bytes in memory and using
#that information to hold 3 values. The first 4 bytes are the first value, 
#the second 4 bytes are the second, etc. 
x = np.array([1,2,3],dtype='uint32') 

#Change x to display those 12 bytes at 6 different values. Doing this does
#NOT change the data that the array is holding. You are only changing the 
#'view' of the data. 
x.dtype = 'uint16'
print(x)
通常(很少有特殊情况),更改数据类型不会更改基础数据。但是,转换函数.astype()确实会更改基础数据。如果有任何12字节的数组被视为“int32”,则running.astype('uint8')将获取每个条目(4字节),并将其转换为一个uint8条目(1字节)。对于3个条目,新数组将只有3个字节。你可以随便看看:

x = np.array([1,2,3],dtype='uint32')
print(x.tobytes())
y = x.astype('uint8')
print(y.tobytes())
所以,当我们说一个文件是30mb时,我们的意思是这个文件(减去一些头信息)是30000000字节,这正是uint8s。1 uint8是1字节。如果任何阵列具有6000by5100uint8s(字节),则该阵列在内存中具有30600000字节的信息


同样,如果您读取一个文件(与文件无关)并写入np.fromfile(,dtype=np.uint8,count=15_300_000),那么您告诉python精确读取15_300_000字节(同样,1字节是1 uint8)的信息(15mb)。如果您的文件是100mb、40mb,甚至是30mb,这将是完全不相关的,因为您告诉python只读取前15mb的数据。

与您的问题无关,但您可以执行
im=I_array[24:,:]
删除前24行。是的,但它们是相同的,我感到困惑的是文件大小。我的意思是原始文件是30+mb,在我用uint 8打开它并写入新的二进制文件后,它变成了15mb。但是如果你打开15mb文件并读取3000B5100 uint8,那么你一定只读取了文件的一半。我的意思是30mb而不是15MB不,文件正好是3000*5100,我只是以uint8的形式读取,我不知道值的原始类型,但我尝试使用uint16,输出的大小是正确的,但像素的值是错误的,我真的很困惑。我不知道该告诉你什么。我对你的文件一无所知,也不知道它是怎么写的。你运行我上面的代码了吗?如果文件是3000by5100uint8s,您可以清楚地看到它应该是15mb。您的文件显然有30mb的数据,即30000000字节,大约是6000by5100uint8s(30600000字节)。我不知道如何用其他方式来解释。