-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVolImage.cpp
211 lines (168 loc) · 6.88 KB
/
VolImage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include "VolImage.h"
#define PRINT(x) std::cout << x;
CHNJAR003::VolImage::VolImage() : width(0), height(0){};
CHNJAR003::VolImage::~VolImage()
{
for (int i = 0; i < slices.size(); i++) //Loop through all the individual 2D slices
{
for (int j = 0; j < height; j++) //Loop through the rows of each 2D slice
{
delete[] slices[i][j]; //Destroy each row
}
delete[] slices[i]; //Destroy the slice itself
}
}
bool CHNJAR003::VolImage::readImages(std::string baseName)
{
std::ifstream baseNameFile;
//try to open the image base header file
baseNameFile.open(("./brain_mri_raws/" + baseName + ".data").c_str());
if (!baseNameFile)
{
std::cerr << "File with base name: " << baseName << " could not be opened for reading.\n";
return false;
}
else //was able to open the image base header file
{
CHNJAR003::VolImage::slices.clear();
std::string line;
getline(baseNameFile, line);
std::istringstream iss(line);
std::string token;
std::vector<std::string> tokens;
while (getline(iss, token, ' '))
{
tokens.push_back(token);
}
/*
for (std::string temp : tokens)
{
PRINT(temp + '\n');
}*/
baseNameFile.close();
width = std::stoi(tokens[0]);
height = std::stoi(tokens[1]);
int numSlices = std::stoi(tokens[2]);
//Loop through all the image slices - essentially the number of images
for (int i = 0; i < numSlices; i++)
{
std::ifstream ifs;
//open the image slice for binary reading
ifs.open(("./brain_mri_raws/" + baseName + std::to_string(i) + ".raw").c_str(), std::ios::in | std::ios::binary);
//if was able to open the image for binary processing
if (ifs.is_open())
{
//Create new slice element to add to the slices vector
//Array of unsigned char pointers for the columns
unsigned char **tempSliceCols = new unsigned char *[height];
/*
//temporary array to read in the binary data from the .raw file
char *temp = new char[width];
*/
//Process the rows of pixels of the .raw image
for (int row = 0; row < height; row++)
{
unsigned char *tempSliceRow = new unsigned char[width];
ifs.read((char *)tempSliceRow, width);
tempSliceCols[row] = tempSliceRow;
}
slices.push_back(tempSliceCols);
ifs.close();
/*PRINT("Slices size: ");
PRINT(slices.size());
PRINT("\n");*/
}
else
{
PRINT("Unable to process file: " + baseName + std::to_string(i) + "\n");
}
}
return true;
}
}
void CHNJAR003::VolImage::extract(int sliceId, std::string output_prefix)
{
PRINT("VolImage extract() method called -> Extracting requested image slice.\n");
int numSlices = slices.size();
//If the sliceId is valid
if ((sliceId >= 0) && (sliceId < numSlices))
{
//Pointer to temporarily store the reference to the slice
unsigned char **tempSliceHolder = slices[sliceId];
std::ofstream headerFile;
headerFile.open(("./brain_mri_raws_output/" + output_prefix + ".data").c_str());
headerFile << width << " " << height << " 1" << std::endl;
headerFile.close();
std::ofstream outputFile;
outputFile.open(("./brain_mri_raws_output/" + output_prefix + ".raw").c_str(), std::ios::binary | std::ios::out);
PRINT("Writing extracted image slice to file: " + output_prefix + ".raw\n");
for (int i = 0; i < height; i++)
{
outputFile.write((char *)tempSliceHolder[i], width);
}
outputFile.close();
PRINT("Extract method finished extracting requested slice.\n\n");
}
}
void CHNJAR003::VolImage::diffmap(int sliceI, int sliceJ, std::string output_prefix)
{
PRINT("VolImage diffmap() method called -> Computing difference map between requested image slices.\n");
int numSlices = slices.size();
//If the slice indices are valid
if ((sliceI >= 0) && (sliceI < numSlices) && (sliceJ >= 0) && (sliceJ < numSlices))
{
unsigned char *tempDiffRow = new unsigned char[width];
std::ofstream outputSliceRaw;
outputSliceRaw.open(("./brain_mri_raws_output/" + output_prefix + ".raw").c_str(), std::ios::binary | std::ios::out);
PRINT("Writing computed difference map to file: " + output_prefix + ".raw\n");
for (int r = 0; r < height; r++)
{
for (int c = 0; c < width; c++)
{
tempDiffRow[c] = (unsigned char)(abs((float)slices[sliceI][r][c] - (float)slices[sliceJ][r][c]) / 2);
}
outputSliceRaw.write((char *)tempDiffRow, width);
}
outputSliceRaw.close();
delete[] tempDiffRow;
PRINT("Difference Map method finished computing difference map.\n\n");
}
}
int CHNJAR003::VolImage::volImageSize(void)
{
//Store the number of images in the slices vector
int numImages = slices.size();
PRINT("Number of images: " + std::to_string(numImages) + "\n");
//Store the number of bytes required for a char pointer
int byteSizeUnsignedCharPointer = sizeof(unsigned char *);
int byteSizeUnsignedChar = sizeof(unsigned char);
//Calculate the total number of bytes required to store all the image data
int numBytesRequired = numImages * (height * width * byteSizeUnsignedChar + height * byteSizeUnsignedCharPointer) +
byteSizeUnsignedCharPointer * numImages;
return numBytesRequired;
}
void CHNJAR003::VolImage::extractRow(int row, std::string output_prefix)
{
PRINT("Extra credit extractRow() method called -> Extracting image along row " + std::to_string(row) + " of the volume of slices.\n");
int numSlices = slices.size();
std::string outFileName = "./brain_mri_raws_output/" + output_prefix + ".raw";
std::ofstream outputRowSliceStream;
outputRowSliceStream.open(outFileName.c_str(), std::ios::binary | std::ios::out);
PRINT("Writing extracted image slice to file: " + outFileName + "\n");
unsigned char *tempRow = new unsigned char[width];
for (int i = 0; i < numSlices; i++)
{
for (int rowPixel = 0; rowPixel < width; rowPixel++)
{
tempRow[rowPixel] = slices[i][row][rowPixel];
}
outputRowSliceStream.write((char *)tempRow, width);
}
outputRowSliceStream.close();
delete[] tempRow;
PRINT("Image extracted along row " + std::to_string(row) + " of the volume of slices.\n\n");
}