Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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;

}

share|improve this question
    
52685 == 0xcdcd, and apparently MSVC sometimes fills uninitialized memory with 0xcd bytes.... –  aschepler Oct 21 '13 at 23:38
    
So that is why the number is 52685, any idea why it would only be filling every 4th array position with a real value and leaving the 3 positions in between uninitialized? –  David Oct 22 '13 at 0:59

1 Answer 1

up vote 1 down vote accepted

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

share|improve this answer
1  
It might be easier to just read the image into the final destination directly and avoid the copy, but this works too. –  Retired Ninja Oct 21 '13 at 23:28
    
Thank you very much your solution worked! This brings up another question, if I am not copying data from one array to another, why am I getting some values in PlaneStack when I loop through to see the output? (especially because the values, other than the uninitialized memory, seem to be correct) @RetiredNinja In order to avoid the copy what kind of file read would I use in order to read the binary into an array where I can work with the integer values? The reason I am doing it in this way is when I started this was the only way I could have access to the pixel values. –  David Oct 22 '13 at 0:59
    
in you code you need to copy data from array with single image into array where all images, you're getting values because it is memory, and it has something it in, if you're getting zeroes - maybe there are zeros? or it is really a problem during reading of file –  Lashane Oct 22 '13 at 1:01
    
The problem was I changed workstations and not file paths, my apologies. I edited the above comment to reflect this and accepted your answer, thank you very much for your help! –  David Oct 22 '13 at 1:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.