我尝试编写的代码的目的是读取多个图像文件,并将它们全部放入可以处理的数组中。数据是一个86字节的标头(我跳过了),后跟710 * 710 u_int16个数字,我以无符号short int形式读取(假设它们是相同的,因为它们是相同的字节数)。读完二进制数据后,将其复制到“ PlaneStack”数组中,跳过每个图像的大小(710 * 710 * unsigned short int)乘以平面数。希望当我完成后,可以将每个图像顺序添加到阵列平面堆栈中,并能够使用诸如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));

09-19 11:37