-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCContrastImage.cpp
More file actions
226 lines (197 loc) · 5.37 KB
/
CContrastImage.cpp
File metadata and controls
226 lines (197 loc) · 5.37 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "CContrastImage.h"
using namespace std;
CContrastImage::CContrastImage(string sz, vector<string> *o)
// Constructor
// Input: string(sz) - File to be read containing intensity details
{
output_content = o;
readImage(sz);
}
CContrastImage::CContrastImage(int nRows, int nColumns, float gridSize, vector<string> *o)
// Constructor
// Input: int(nRows) - Y Total
// int(ncolumns) - X Total
// float(gridSize) - Point spacing
{
output_content = o;
m_iRows = nRows;
m_iColumns = nColumns;
m_fGridSize = gridSize;
int total = nRows*nColumns;
m_dArray.resize(total);
}
void CContrastImage::fourier(float nScale)
// Perform fourier transform on data
// This is lifted from an example file supplied with fftw3
// Though cout's have been removed to make viewing easier
// Data is centralised afterwards
// Inputs: float(nScale) - This is our scaling factor for the images,
// Otherwise the spread is too great.
{
int i = 0;
int j = 0;
double *in;
int nx = getWidth();
int ny = getHeight();
fftw_complex *out;
fftw_plan plan_forward;
// INPUT ARRAY
in = (double*) malloc ( sizeof ( double ) * nx * ny );
for ( i = 0; i < nx; i++ )
for ( j = 0; j < ny; j++ )
{
in[i*nx+j] = getIntensity(j,i);
// Addition to clear array ready for out put Fourier
setIntensity(j,i,0);
}
// OUTPUT ARRAY
int nyh = ( ny / 2 ) + 1; //half+1 for Fourier transform
int ny_half = ( ny / 2 ); //true halves
int nx_half = ( nx / 2 ); //true halves
// PERFORM FFT
out = (fftw_complex*) fftw_malloc ( sizeof ( fftw_complex ) * nx * nyh );
plan_forward = fftw_plan_dft_r2c_2d ( nx, ny, in, out, FFTW_ESTIMATE );
fftw_execute ( plan_forward );
// TRANSFORM COMPLETE //
// GET MAX FOR SCALING //
double MAX = 0;
for ( i = 0; i < nx; i++ )
for ( j = 0; j < ny_half; j++ )
{
if (out[i*nyh+j][0] > 0 && out[i*nyh+j][0] > MAX)
{
MAX = out[i*nyh+j][0];
}
else if (0-out[i*nyh+j][0] > MAX)
{
MAX = 0-out[i*nyh+j][0];
}
}
// IMAGED CENTRALISING
// SCALE is 3000 / log (1+INPUT)
// Puts everything in close proximity for imaging
double SCALE_VALUE = 3000; // COULD SOFTCODE THIS
double SCALAR = SCALE_VALUE / log(1 + MAX*nScale);
for ( i = 0; i < nx; i++ )
{
for ( j = 0; j < (ny_half); j++)
{
if ( i < nx_half)
{
if (out[i*nyh+j][0] > 0)
{
setIntensity(ny_half-1+j,nx_half-i,SCALAR*log(1+out[i*nyh+j][0]*nScale));
setIntensity(ny_half-j,nx_half-1+i,SCALAR*log(1+out[i*nyh+j][0]*nScale));
}
else
{
setIntensity(ny_half-1+j,nx_half-i,SCALAR*log(1-out[i*nyh+j][0]*nScale));
setIntensity(ny_half-j,nx_half-1+i,SCALAR*log(1-out[i*nyh+j][0]*nScale));
}
}
else
{
if (out[i*nyh+j][0] > 0)
{
setIntensity(ny_half-1+j,nx_half-1+(nx-i),SCALAR*log(1+out[i*nyh+j][0]*nScale));
setIntensity(ny_half-j,nx_half-(nx-i),SCALAR*log(1+out[i*nyh+j][0]*nScale));
}
else
{
setIntensity(ny_half-1+j,nx_half-1+(nx-i),SCALAR*log(1-out[i*nyh+j][0]*nScale));
setIntensity(ny_half-j,nx_half-(nx-i),SCALAR*log(1-out[i*nyh+j][0]*nScale));
}
}
}
}
// CLEAR OUT MEMORY //
fftw_destroy_plan(plan_forward);
free(in);
fftw_free(out);
}
void CContrastImage::readImage(string sz)
// Reads in data from file
// Input: string(sz) - Filename
{
Information new_file;
new_file.readFile(sz);
// SET DATA //
setHeight((int)new_file.getN());
setWidth((int)new_file.getM());
setStep(new_file.getStep());
setDataArray(new_file.getDataArray());
}
void CContrastImage::setIntensity(int y, int x, double value)
// Sets intensity at point
// Inputs: int(y) - Y COORDINATE
// int(x) - X COORDINATE
// double(value) - VALUE TO INSERT
{
if(y >= 0 && y < m_iRows && x >= 0 && x < m_iColumns)
{
int position = (y*m_iRows) + x;
m_dArray[position] = value;
}
else
{
string xS;
string yS;
NumberToString(x,xS);
NumberToString(y,yS);
#pragma omp critical
{
output_content->push_back("Failed to write 2D array out of bounds: x =" + xS + ", y = " + yS);
}
}
}
double CContrastImage::getIntensity(int y, int x)
// Returns intensity at point
// Inputs: int(y) - Y coordinate
// int(x) - X coordinate
// Output: double - Value at point
{
if(y >= 0 && y < m_iRows && x >= 0 && x < m_iColumns)
{
int position = (y*m_iRows) + x;
return m_dArray[position];
}
else
{
#pragma omp critical
{
output_content->push_back("Out of 2D Array Bounds returning 0.0");
}
return 0.0;
}
}
void CContrastImage::saveImageMatrix(string sz)
// Saves image matrix (akin to .txt file)
// Input: string(sz) - Filename
{
Information new_file;
new_file.saveImageMatrix(sz,m_dArray,m_iColumns,m_iRows,m_fGridSize);
}
void CContrastImage::saveImage(string sz)
// Saves image (akin to .zcon file)
// Input: string(sz) - Filename
{
Information new_file;
new_file.saveImage(sz,m_dArray,m_iColumns,m_iRows,m_fGridSize);
}
void CContrastImage::saveCrossSection(const string &sz,const int &n)
{
vector<float> crossSection;
for (int i = 0; i < m_iColumns; i++)
crossSection.push_back(getIntensity(n,i));
Information new_file;
new_file.saveImage(sz,crossSection,m_iColumns,1,m_fGridSize);
}
Information CContrastImage::getInformation(const string &filename)
// Returns data from file
// Output - Information class of file data
{
Information new_file;
new_file.setDataArray(m_dArray,m_iColumns,m_iRows,m_fGridSize);
new_file.setFilename(filename);
return new_file;
}