-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBeamHardening.cpp
More file actions
344 lines (314 loc) · 13.8 KB
/
BeamHardening.cpp
File metadata and controls
344 lines (314 loc) · 13.8 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*------------------------------------------------------------------------------*
* File Name: *
* Creation: *
* Purpose: OriginC Source C file *
* Copyright (c) ABCD Corp. 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 *
* All Rights Reserved *
* *
* Modification Log: *
*------------------------------------------------------------------------------*/
////////////////////////////////////////////////////////////////////////////////////
// Including the system header file Origin.h should be sufficient for most Origin
// applications and is recommended. Origin.h includes many of the most common system
// header files and is automatically pre-compiled when Origin runs the first time.
// Programs including Origin.h subsequently compile much more quickly as long as
// the size and number of other included header files is minimized. All NAG header
// files are now included in Origin.h and no longer need be separately included.
//
// Right-click on the line below and select 'Open "Origin.h"' to open the Origin.h
// system header file.
#include <Origin.h>
////////////////////////////////////////////////////////////////////////////////////
//#pragma labtalk(0) // to disable OC functions for LT calling.
////////////////////////////////////////////////////////////////////////////////////
// Include your own header files here.
////////////////////////////////////////////////////////////////////////////////////
// Start your functions here.
//Extract BeamHardening curve from defined x and y pixels
//Matrices must be in an active MatrixObject in different layers. Layers LongName must represent a thickness of irradiated sample
void Extract_beam_Hardening_curve(int x, int y){
int i=0;
vector<int> vector_x, vector_y,vm;
MatrixLayer mlayer = Project.ActiveLayer(); // Active matrix sheet
while(1){
if(!mlayer.SetActive(i)) break; //After we select the last active object break
Matrix m(mlayer);
MatrixObject mobject = mlayer.MatrixObjects(i);
vector_x.Add(atoi(mobject.GetLongName()));
vector_y.Add(m.GetCellValue(x,y));
i++;
}
Worksheet wksResult;
wksResult.Create("Origin");
DataRange dr;
dr.Add(wksResult,0,"X");
dr.Add(wksResult,1,"Y");
dr.SetData(&vector_y,&vector_x);
}
//
void Beam_Hardening_correction_linear(float koeff){
MatrixLayer activelayer = Project.ActiveLayer(); //Get active layer
MatrixObject RAW_object = activelayer.MatrixObjects(-1);
matrix & RAW_matrix=RAW_object.GetDataObject();
//int activelayer_index = activelayer.GetActive();
MatrixPage matPage = activelayer.GetPage(); //Get active page from layer
MatrixLayer first_sheet = matPage.Layers(0); //Acess first sheet in the page
MatrixLayer second_sheet = matPage.Layers(1); //Acess second sheet in the page
int BH_corrected_matrix = first_sheet.Insert(1, -1, -1); //Insert new matrix
MatrixObject mobject = first_sheet.MatrixObjects(BH_corrected_matrix); //Select this new matrix object
matrix & mat=mobject.GetDataObject(); //connect it to a matrix two dimensional array, This is the corrected matrix
vector thickness,cc;
int l=0;
int m[10];
while(1){
if(!second_sheet.SetActive(l)) break; //After we select the last active object break
MatrixObject BH_object = second_sheet.MatrixObjects(l);
thickness.Add(atoi(BH_object.GetLongName()));
l++;
}
//Linear approximation
float c,y0;
for(int i=0;i<256;i++){
for(int j=0;j<256;j++){
for(int k=0;l>k;k++){
MatrixObject BH_object = second_sheet.MatrixObjects(k);
matrix & BH_matrix=BH_object.GetDataObject();
float d1=koeff*RAW_matrix[i][j]; //for debug
float d2=BH_matrix[i][j]; //for debug
if(k==l){
MatrixObject BH_objectx = second_sheet.MatrixObjects(k-1);
matrix & BH_matrixx=BH_objectx.GetDataObject();
c=(BH_matrix[i][j]-BH_matrixx[i][j])/(thickness[k]-thickness[k-1]);
y0=BH_matrix[i][j]-c*thickness[k];
float d3=BH_matrixx[i][j]; //for debug
break;
}
if(koeff*RAW_matrix[i][j]>BH_matrix[i][j] && k == 0){
MatrixObject BH_objectx = second_sheet.MatrixObjects(k+1);
matrix & BH_matrixx=BH_objectx.GetDataObject();
float f = BH_matrixx[i][j];
c=(BH_matrixx[i][j]-BH_matrix[i][j])/(thickness[k+1]-thickness[k]);
y0=BH_matrix[i][j]-c*thickness[k];
float d3=BH_matrixx[i][j]; //for debug
break;
}
if(koeff*RAW_matrix[i][j]>BH_matrix[i][j] && k != l){
MatrixObject BH_objectx = second_sheet.MatrixObjects(k-1);
matrix & BH_matrixx=BH_objectx.GetDataObject();
float f = BH_matrixx[i][j];
c=(BH_matrix[i][j]-BH_matrixx[i][j])/(thickness[k]-thickness[k-1]);
y0=BH_matrix[i][j]-c*thickness[k];
float d3=BH_matrixx[i][j]; //for debug
break;
}
}
mat[i][j]=(koeff*RAW_matrix[i][j]-y0)/c;
}
}
}
//
void Beam_Hardening_correction_exponential(float koeff){
MatrixLayer activelayer = Project.ActiveLayer(); //Get active layer
MatrixObject RAW_object = activelayer.MatrixObjects(-1);
matrix & RAW_matrix=RAW_object.GetDataObject();
//int activelayer_index = activelayer.GetActive();
MatrixPage matPage = activelayer.GetPage(); //Get active page from layer
MatrixLayer first_sheet = matPage.Layers(0); //Acess first sheet in the page
MatrixLayer second_sheet = matPage.Layers(1); //Acess second sheet in the page
int BH_corrected_matrix = first_sheet.Insert(1, -1, -1); //Insert new matrix
MatrixObject mobject = first_sheet.MatrixObjects(BH_corrected_matrix); //Select this new matrix object
matrix & mat=mobject.GetDataObject(); //connect it to a matrix two dimensional array, This is the corrected matrix
vector thickness,cc;
int l=0;
int m[10];
float a,A,O;
float x1,x2,y1,y2;
while(1){
if(!second_sheet.SetActive(l)) break; //After we select the last active object break
MatrixObject BH_object = second_sheet.MatrixObjects(l);
thickness.Add(atoi(BH_object.GetLongName()));
l++;
}
//exponential approximation
float c,y0;
for(int i=0;i<256;i++){
for(int j=0;j<256;j++){
for(int k=0;l>k;k++){
MatrixObject BH_object = second_sheet.MatrixObjects(k);
matrix & BH_matrix=BH_object.GetDataObject();
MatrixObject BH_object_last = second_sheet.MatrixObjects(l-1);
matrix & BH_matrix_last=BH_object_last.GetDataObject();
float d1=koeff*RAW_matrix[i][j]; //for debug
float d2=BH_matrix[i][j]; //for debug
if(k==(l-1)){ //its is not number 1 its the letter 1!!!! If RAW matrix values are smaller than any values from BH matrixes
MatrixObject BH_objectx_y1 = second_sheet.MatrixObjects(k-2);
matrix & BH_matrix_y1=BH_objectx_y1.GetDataObject();
MatrixObject BH_objectx_y2 = second_sheet.MatrixObjects(k-1);
matrix & BH_matrix_y2=BH_objectx_y2.GetDataObject();
O=BH_matrix[i][j];
x1=thickness[k-2], x2=thickness[k-1];
y1=BH_matrix_y1[i][j], y2=BH_matrix_y2[i][j];
float ln1=ln(y1-O);
float ln2=ln(y2-O);
A=exp((((x1/x2)*ln2)-ln1)/((x1/x2)-1));
float ln3=ln((y2-O)/A);
a=(1/x2)*ln3;
break;
}
if(koeff*RAW_matrix[i][j]>BH_matrix[i][j] && k == 0){ //If RAW matrix values are bigger than any values from BH matrixes
MatrixObject BH_objectx = second_sheet.MatrixObjects(3);
matrix & BH_matrixx=BH_objectx.GetDataObject();
MatrixObject BH_object_next = second_sheet.MatrixObjects(k+1);
matrix & BH_matrix_next=BH_object_next.GetDataObject();
O=BH_matrixx[i][j];
x1=thickness[0], x2=thickness[1];
y1=BH_matrix[i][j], y2=BH_matrix_next[i][j];
float ln1=ln(y1-O);
float ln2=ln(y2-O);
A=exp((((x1/x2)*ln2)-ln1)/((x1/x2)-1));
float ln3=ln((y2-O)/A);
a=(1/x2)*ln3;
break;
}
if(koeff*RAW_matrix[i][j]>BH_matrix[i][j] && k != l){ //This is case when RAW matrix values are between BH matrices
MatrixObject BH_objectx = second_sheet.MatrixObjects(k-1);
matrix & BH_matrix_previous=BH_objectx.GetDataObject();
MatrixObject BH_object_next = second_sheet.MatrixObjects(k+1);
matrix & BH_matrix_next=BH_object_next.GetDataObject();
if(k<l-2){ //If the O point (the constant background) is the nearest point int the BH curve (the last point is where is the smallest counts is)
MatrixObject BH_object_last = second_sheet.MatrixObjects(k+2);
matrix & BH_matrix_last=BH_object_last.GetDataObject();
O=BH_matrix_last[i][j];
}
else O=BH_matrix_last[i][j];
//O=BH_matrix_last[i][j]; //If the O point (the constant background) is the last point in the BH curve (last point is where the smallest counts is)
x1=thickness[k-1], x2=thickness[k];
y1=BH_matrix_previous[i][j], y2=BH_matrix[i][j];
float thickness_ratio=x1/x2;
float ln1=ln(y1-O);
float ln2=ln(y2-O);
float B=ln1-ln2;
float e=((x1/x2)*ln2-ln1)/((x1/x2)-1);
A=exp((((x1/x2)*ln2)-ln1)/((x1/x2)-1));
float ln3=ln((y2-O)/A);
a=(1/x2)*ln3;
break;
}
}
float y=RAW_matrix[i][j];
mat[i][j]=(1/a)*ln((y-O)/A);
float result = mat[i][j];
}
}
}
//
void Beam_Hardening_correction_exponential2(float koeff){ //from paper Data processing and image reconstruction
MatrixLayer activelayer = Project.ActiveLayer(); //Get active layer
MatrixObject RAW_object = activelayer.MatrixObjects(-1);
matrix & RAW_matrix=RAW_object.GetDataObject();
//int activelayer_index = activelayer.GetActive();
MatrixPage matPage = activelayer.GetPage(); //Get active page from layer
MatrixLayer first_sheet = matPage.Layers(0); //Acess first sheet in the page
MatrixLayer second_sheet = matPage.Layers(1); //Acess second sheet in the page
int BH_corrected_matrix = first_sheet.Insert(1, -1, -1); //Insert new matrix
MatrixObject mobject = first_sheet.MatrixObjects(BH_corrected_matrix); //Select this new matrix object
matrix & mat=mobject.GetDataObject(); //connect it to a matrix two dimensional array, This is the corrected matrix
vector thickness,cc;
int l=0;
int m[10];
float a1,A1,a2,A2,O;
float x1,x2,x3,y,y1,y2,y3;
while(1){
if(!second_sheet.SetActive(l)) break; //After we select the last active object break
MatrixObject BH_object = second_sheet.MatrixObjects(l);
thickness.Add(atoi(BH_object.GetLongName()));
l++;
}
//Linear approximation
float c,y0;
for(int i=0;i<256;i++){
for(int j=0;j<256;j++){
for(int k=0;l>k;k++){
MatrixObject BH_object = second_sheet.MatrixObjects(k);
matrix & BH_matrix=BH_object.GetDataObject();
float d1=koeff*RAW_matrix[i][j]; //for debug
float d2=BH_matrix[i][j]; //for debug
if(k==l){ //its is not number 1 its the letter 1!!!! If RAW matrix values are smaller than any values from BH matrixes
MatrixObject BH_objectx = second_sheet.MatrixObjects(k-1);
matrix & BH_matrixx=BH_objectx.GetDataObject();
c=(BH_matrix[i][j]-BH_matrixx[i][j])/(thickness[k]-thickness[k-1]);
y0=BH_matrix[i][j]-c*thickness[k];
float d3=BH_matrixx[i][j]; //for debug
break;
}
if(koeff*RAW_matrix[i][j]>BH_matrix[i][j] && k == 0){ //If RAW matrix values are bigger than any values from BH matrixes
MatrixObject BH_objectx = second_sheet.MatrixObjects(k+1);
matrix & BH_matrixx=BH_objectx.GetDataObject();
float f = BH_matrixx[i][j];
MatrixObject BH_object_next = second_sheet.MatrixObjects(k+1);
matrix & BH_matrix_next=BH_object_next.GetDataObject();
O=BH_matrixx[i][j];
x1=thickness[k], x2=thickness[k+1];
y1=BH_matrix[i][j], y2=BH_matrix_next[i][j];
float thickness_ratio=thickness[k]/thickness[k+1];;
A1=exp((((x1/x2)*ln(y2-O))-ln(y1-O))/((x1/x2)-1));
a1=(1/x2)*ln((y2-O)/A1);
float d3=BH_matrixx[i][j]; //for debug
break;
}
if(koeff*RAW_matrix[i][j]>BH_matrix[i][j] && k != 1 && k<l-2){ //This is case when RAW matrix values are between BH matrices
if (k<l-2){
MatrixObject BH_objectx = second_sheet.MatrixObjects(k-1);
matrix & BH_matrix_previous=BH_objectx.GetDataObject();
MatrixObject BH_object_next = second_sheet.MatrixObjects(k+1);
matrix & BH_matrix_next=BH_object_next.GetDataObject();
MatrixObject BH_object_last = second_sheet.MatrixObjects(k+2);
matrix & BH_matrix_last=BH_object_last.GetDataObject();
O=BH_matrix_last[i][j];
//x1=thickness[k-1], x2=thickness[k];
x1=thickness[k-1], x2=thickness[k]; x3=thickness[k+1];
y1=BH_matrix_previous[i][j], y2=BH_matrix[i][j], y3=BH_matrix_next[i][j];
float thickness_ratio=x1/x2;
float ln1=ln(y1-y3);
float ln2=ln(y2-y3);
float B=ln1-ln2;
float e=((x1/x2)*ln2-ln1)/((x1/x2)-1);
A1=exp((((x1/x2)*ln2)-ln1)/((x1/x2)-1));
float ln3=ln((y2-O)/A1);
a1=(1/x2)*ln3;
ln1=ln(y2-O);
ln2=ln(y3-O);
A2=exp((((x2/x3)*ln2)-ln1)/((x2/x3)-1));
ln3=ln((y3-O)/A2);
a2=(1/x3)*ln3;
break;
}
else {
MatrixObject BH_objectx = second_sheet.MatrixObjects(k-1);
matrix & BH_matrix_previous=BH_objectx.GetDataObject();
MatrixObject BH_object_next = second_sheet.MatrixObjects(k+1);
matrix & BH_matrix_next=BH_object_next.GetDataObject();
x1=thickness[k-1], x2=thickness[k];
y1=BH_matrix_previous[i][j], y2=BH_matrix[i][j];
float thickness_ratio=x1/x2;
float ln1=ln(y1-O);
float ln2=ln(y2-O);
float B=ln1-ln2;
float e=((x1/x2)*ln2-ln1)/((x1/x2)-1);
A1=exp((((x1/x2)*ln2)-ln1)/((x1/x2)-1));
float ln3=ln((y2-O)/A1);
a1=(1/x2)*ln3;
break;
}
}
}
//mat[i][j]=(1/a)*ln((y-O)/A);
y=RAW_matrix[i][j];
if(k<l-2) mat[i][j]=((y-y2)/(a2*(y1-y2)))*ln((y-O)/A2) + ((y1-y)/(a1*(y1-y2)))*ln((y-y3)/A1);
else mat[i][j]=(1/a1)*ln((RAW_matrix[i][j]-O)/A1);
//float x1=((y-y2)/(a2*(y1-y2)))*ln((y-O)/A2);
//float x2=((y1-y)/(a1*(y1-y2)))*ln((y-y3)/A1);
float result = mat[i][j];
}
}
}