读取二进制文件并转换为整数数组时出现问题

Problems reading in binary files and converting to integer array

本文关键字:问题 数组 整数 二进制文件 转换 读取      更新时间:2023-10-16

我尝试编写的代码的目的是读取多个图像文件并将它们全部放入我可以处理的数组中。数据是一个 86 字节的标头(我跳过),后跟 710*710 u_int16 位数字,我将其读作无符号的 short int(假设它们是相同的,因为它们是相同的字节数)。一旦我读入了这个二进制数据,我就将其复制到数组"PlaneStack"中,跳过每个图像的大小(710 * 710 * 无符号短整型)乘以平面数。我希望当我完成后,我将按顺序将每个图像添加到阵列平面堆栈中,并能够使用 PlaneStack(x+710*y+710*710*z) 等方案访问单个像素。代码编译并运行,说它尝试并成功打开每个图像,但是当我输出 Image 的内容时,我得到一些值,这些值大约在预期数字附近,并且接近选择位置,其中许多 '52685 相互分散。(实际上,看起来每个"好"值之间有 3 个 '52685)。

我的问题是:

是否正确定义了我的数组,以便能够以二进制形式读出我读入的文件的整数值?

为什么我会在这些重复的时间间隔内得到这个重复的可怕的"52685",它的意义是什么?(另外,假设它具有重要性,是否有其他输出数字可以提示我的代码中出现哪些错误?

我的方式使用 ifstream 安全吗?就像打开和关闭流以加载多个文件一样。我已经读到它可能很危险,但我觉得它实施得很好。

感谢所有关注此内容的人,如果您对初学者有任何其他建设性的批评,我很乐意接受它们!

#include "math.h"
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <fstream>
using namespace std;
/////////////////global variables///////////////////
unsigned short int *VImage = NULL;  


int main(int argc, char *argv[])
{   //$$$ To do: have this take in values and turn to usable function
///// Have args be path to Frames in Matlab output format, frame number, and number of planes/////
///// Call for multiple frames if desired                                                    /////
///// LoadVimageFrame ( path, frame #, # of planes)                                          /////
//Test for argc being correct number
char Path[1024];  //Base path to folder of Plane images
char FullPath[1024]; // Full path to image to open
int NumberofPlanes = 78; // NUmber of images to in a planestack
long int VImageSize = 710*710;                  // total number of pixels for Vimage 710X710
unsigned short int* PlaneStack = new unsigned short int[NumberofPlanes*VImageSize]; //array of unsigned short ints the length of all pixels in planestack

VImage = new unsigned short int[VImageSize];  // Initialize VImage
memset(VImage,0,VImageSize*sizeof(unsigned short int));

for (int pnum = 1; pnum <= NumberofPlanes; pnum++) //Loop through each plane image
{
    ifstream in;
    strcpy(Path, "C:/Users/dunkerley/Desktop/frame150/frame150"); //This will be path from argv[1]
    if ( NumberofPlanes<9 )
        sprintf(FullPath, "%s/recon_p%d.vimage",Path,pnum);
    if ( NumberofPlanes>9 && NumberofPlanes<100)
        sprintf(FullPath, "%s/recon_p%02d.vimage",Path,pnum);
    if ( NumberofPlanes>100)
        sprintf(FullPath, "%s/recon_p%03d.vimage",Path,pnum);

    //read in single Vimage as binary
    cout << "Attempting to Open Image: " << FullPath << endl;
    in.open(FullPath,ios::in | ios::binary); //This is the path to file in future will have to do for all planes
    if(in)
    {   
        cout << "Opening Image: " << FullPath << endl;
        in.seekg(86); ///Skip Header (86 bits for vimage)
        in.read((char*)VImage, VImageSize*sizeof(unsigned short int));//reads image data 
    }
    else
        cout << "Can't open file n";
    in.clear();
    in.close();
    PlaneStack[(pnum-1)*sizeof(VImage)] = *VImage; //Assign plane to correct location in planestack

}
for (int i = 0; i < 250; i++)
{
    //Test if the ith value is the ith pixel in the image (compared to imageJ)
    cout  << i <<" "<< PlaneStack[i] << endl; // output pixels
    // This has unexpected output
}

return 0;

}

PlaneStack[(pnum-1)*sizeof(VImage)] = *VImage;

在这里,您不会将数据从一个数组复制到另一个数组

考虑使用memcpy(&PlaneStack[(pnum-1)*sizeof(VImage)], VImage, VImageSize*sizeof(unsigned short int));