我尝试编写的代码的目的是读入多个图像文件并将它们全部放入我可以处理的数组中。数据是一个86字节的标题(我跳过),接着是710 * 710 u_int16数字,我读作无符号短整数(假设它们是相同的,因为它们是相同的字节数)。一旦我读入了这个二进制数据,我将其复制到数组“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;
}
答案 0 :(得分:1)
PlaneStack[(pnum-1)*sizeof(VImage)] = *VImage;
这里你没有将数据从一个数组复制到另一个数组
考虑使用memcpy(&PlaneStack[(pnum-1)*sizeof(VImage)], VImage, VImageSize*sizeof(unsigned short int));