私が書こうとしているコードの目的は、複数の画像ファイルを読み込み、それらすべてを処理可能な配列に入れることです。データは 86 バイトのヘッダー (スキップします) で、その後に 710*710 の u_int16 桁が続き、これを unsigned short int として読み取ります (同じバイト数であるため、同じであると仮定します)。このバイナリ データを読み込んだら、それを配列「PlaneStack」にコピーし、各画像のサイズ (710*710*unsigned short int) に平面の数を掛けてスキップします。これは、完成したら、各画像をアレイ プレーン スタックに順次追加し、PlaneStack(x+710*y+710*710*z) などのスキームを使用して個々のピクセルにアクセスできるようにすることを望んでいます。コードがコンパイルされて実行され、各画像が試行され、正常に開かれると表示されますが、Image の内容を出力すると、多くの '52685' が相互に分散している、予想される数に近く、選択した場所に近い値が得られます。(実際には、すべての「適切な」値の間に 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;
}