gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:20:42 29 4
gpt4 key购买 nike

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

我的问题是:

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

为什么我会在这些重复的时间间隔内收到这个可怕的重复“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));

关于c++ - 读取二进制文件并转换为整数数组时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19506477/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com