我正在尝试在python中实现以下c ++代码。但是,当我打印出来时,两者的数据有所不同。我阅读了这篇文章Receiving 16-bit integers in Python,并尝试了“> h”和“ python代码int16_t* loadDepthImageCompressed( const char* fname ){
//now read the depth image
FILE* pFile = fopen(fname, "rb");
if(!pFile){
std::cerr << "could not open file " << fname << std::endl;
return NULL;
}
int im_width = 0;
int im_height = 0;
bool success = true;
std::cout << sizeof(int) << std::endl;
std::cout << sizeof(int16_t) << std::endl;
success &= ( fread(&im_width,sizeof(int),1,pFile) == 1 ); // read width of depthmap
success &= ( fread(&im_height,sizeof(int),1,pFile) == 1 ); // read height of depthmap
int16_t* depth_img = new int16_t[im_width*im_height];
std::cout << im_width << std::endl;
std::cout << im_height << std::endl;
int numempty;
int numfull;
int p = 0;
while(p < im_width*im_height ){
success &= ( fread( &numempty,sizeof(int),1,pFile) == 1 );
for(int i = 0; i < numempty; i++)
depth_img[ p + i ] = 0;
success &= ( fread( &numfull,sizeof(int), 1, pFile) == 1 );
success &= ( fread( &depth_img[ p + numempty ], sizeof(int16_t), numfull, pFile) == (unsigned int) numfull );
p += numempty+numfull;
}
fclose(pFile);
if(success)
return depth_img;
else{
delete [] depth_img;
return NULL;
}
}
def loadDepthImageCompressed(fname):
with open(fname,'rb') as depth_file:
im_width = struct.unpack('i',depth_file.read(4))[0]
im_height = struct.unpack('i',depth_file.read(4))[0]
depth_img = [None] * (im_width * im_height)
p = 0
while(p < im_width * im_height):
numempty = struct.unpack('i',depth_file.read(4))[0]
for i in range(numempty):
depth_img[p + i] = 0;
numfull = struct.unpack('i',depth_file.read(4))[0]
for i in range(numfull):
depth_img[p+numempty + i] = struct.unpack('>h',depth_file.read(2))[0]
p += numempty+numfull
return depth_img