Question

The purpose of the code I am trying to write is to read in multiple image files and put them all into an array that I can process. The data is an 86 byte header (which I skip over), followed by 710*710 u_int16 digits, which I read in as unsigned short int (assuming they are the same since they are the same number of bytes). Once I have this binary data read in I copied it into the array "PlaneStack", skipping the size of each image (710*710*unsigned short int) multiplied by the number of planes. This is hoping that when I am finished I will have each image sequentially added to the array plane stack and be able to access individual pixels by using a scheme such as PlaneStack(x+710*y+710*710*z). The code compiles and runs, saying that it attempts and successfully opens each image but when I output the contents of Image, I get some values that are around the expected number and close to the select location with many '52685's inter dispersed. ( Actually it looks as if there are 3 '52685's between every "good" value).

My questions are:

Am I correctly defining my array to be able to read out the integer values of the files I have read in as binary?

Why am I getting this repeated dreaded '52685' at these repeated intervals, and what is its significance? (Also, assuming that it has a significance, are there other output numbers that can give hints to what errors arise in my code?)

Is it safe to use ifstream the way that I am? As in opening and closing the stream to load multiple files. I have read that it can be dangerous, but I feel like it is implemented ok.

Thanks to everyone that looks at this, and if you have any other constructive critism for a beginner I'll be glad to take them!

#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;

}

Was it helpful?

Solution

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

here you are not copying data from one array into another

consider using memcpy(&PlaneStack[(pnum-1)*sizeof(VImage)], VImage, VImageSize*sizeof(unsigned short int));

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top