0

我尝试编写的代码的目的是读取多个图像文件并将它们全部放入我可以处理的数组中。数据是一个 86 字节的标头(我跳过),然后是 710*710 u_int16 数字,我将其读入为 unsigned short int(假设它们是相同的,因为它们的字节数相同)。读取此二进制数据后,我将其复制到数组“PlaneStack”中,跳过每个图像的大小(710*710*unsigned short int)乘以平面数。这是希望当我完成后,我会将每个图像顺序添加到阵列平面堆栈中,并能够通过使用 PlaneStack(x+710*y+710*710*z) 之类的方案来访问单个像素。代码编译并运行,说它尝试并成功打开每个图像但是当我输出图像的内容时,我得到了一些值,这些值在预期数字附近,并且靠近选择位置,许多'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;

}

4

1 回答 1

1

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

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

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

于 2013-10-21T23:25:27.400 回答