-
Notifications
You must be signed in to change notification settings - Fork 12
/
fbp.hpp
596 lines (521 loc) · 18.8 KB
/
fbp.hpp
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/**
fbp.hpp
Filtered Back Projection Algorithm
MIT License
Copyright (c) 2019 Fran Piernas Diaz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Created using OpenCV 4.0.0
@author Fran Piernas Diaz ([email protected])
@version 1.1
Based on Peter Toft: "The Radon Transform - Theory and Implementation",
Ph.D. thesis. Department of Mathematical Modelling, Technical University of Denmark, June 1996.
************CHANGELOG************
V1.1:
Changed the way the point cloud is saved. Values are now floating point normalized
from 0 to 255, instead of 0 to 255 but integer, as in V1.0.
Usage to reconstruct a single sinogram, use this code:
Mat sinogram=imread("sinogram.jpg",IMREAD_COLOR); //load the sinogram image "sinogram.jpg"
Mat filtered_sinogram=filter_sinogram(sinogram); //filter the sinogram
Mat reconstruction=iradon(filtered_sinogram,false); //perform back projection. Change false to true if sinogram is a full turn.
renormalize255_frame(reconstruction); //normalize to 255
imwrite("Reconstruction.jpg",reconstruction); //save reconstruction
Usage to reconstuct a CT scan from a video, call this function:
fbp(video,
extra_frames,
pointcloud_threshold,
normalizeByframe,
full_turn)
where: (string) video is the name of the CT video.
(int) extra_frames is the number of interpolated frames between pair of frames. Try 1 and
increase if your results are noisy due to very few images available.
(int) pointcloud_threshold is a 0-255 number wich will remove all points whose intensity
is less than this number when saving the point cloud.
(bool) normalizeByframe, if true, all slices will be normalized to the maximum value
of that slice. If false, the object is normalized to its absolute maximum.
(bool) full_turn, if true, the code assumes that the input CT video is a full turn.
Set it false if the input is half a turn.
Follow any instruction on the terminal
Usage to reconstuct a CT scan from a vector<Mat>, call this function:
fbp(frames,
extra_frames,
pointcloud_threshold,
normalizeByframe,
full_turn)
where frames is a vector<Mat> containing all pictures from 0 to 360 (or 180) degrees. All other
parameters are the same as explained above.
Follow any instruction on the terminal
Special thanks to Nicolas De Francesco ([email protected]) for the help on image processing.
*/
#include <opencv2/opencv.hpp>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <fstream>
#include <cmath>
#define K_PLUS 43
#define K_MINUS 45
#define K_ENTER 13
using namespace cv;
using namespace std;
void gamma_correction_frame(Mat& frame)
{
unsigned int f,c;
for(f=0;f<frame.size().height;f++)
{
for(c=0;c<frame.size().width;c++)
{
frame.at<Vec3f>(f,c)[0]=pow(frame.at<Vec3f>(f,c)[0],2.2);
frame.at<Vec3f>(f,c)[1]=pow(frame.at<Vec3f>(f,c)[1],2.2);
frame.at<Vec3f>(f,c)[2]=pow(frame.at<Vec3f>(f,c)[2],2.2);
}
}
return;
}
void gamma_correction_frames(vector<Mat>& frames)
{
unsigned int i;
for(i=0;i<frames.size();i++) gamma_correction_frame(frames.at(i));
return;
}
vector<Mat> video_to_array(string video)
{
VideoCapture video_input(video.c_str());
Mat frame;
vector<Mat> input_array;
do
{
video_input>>frame;
if(frame.empty()) break;
input_array.push_back(frame.clone());
}while(!frame.empty());
return input_array;
}
unsigned int begin_frame(vector<Mat>& frame_array)
{
namedWindow("Select begin frame of CT.",WINDOW_NORMAL);
cout<<"Select begin frame of CT. Navigate through frames with + or - and press enter to select."<<endl;
unsigned int frame_index=0, key_pressed;
do
{
imshow("Select begin frame of CT.",frame_array.at(frame_index));
key_pressed=waitKey(0);
if(key_pressed==K_PLUS&&frame_index<frame_array.size()-1) frame_index++;
if(key_pressed==K_MINUS&&frame_index>0) frame_index--;
cout<<"Moving to frame: "<<frame_index+1<<"/"<<frame_array.size()<<endl;
if(key_pressed==K_ENTER) break;
}while(true);
destroyWindow("Select begin frame of CT.");
return frame_index;
}
unsigned int end_frame(vector<Mat>& frame_array, unsigned int initial_frame)
{
namedWindow("Select end frame of CT.",WINDOW_NORMAL);
cout<<"Select end frame of CT. Navigate through frames with + or - and press enter to select."<<endl;
unsigned int frame_index=initial_frame, key_pressed;
do
{
imshow("Select end frame of CT.",frame_array.at(frame_index));
key_pressed=waitKey(0);
if(key_pressed==K_PLUS&&frame_index<frame_array.size()-1) frame_index++;
if(key_pressed==K_MINUS&&frame_index>initial_frame) frame_index--;
cout<<"Moving to frame: "<<frame_index+1<<"/"<<frame_array.size()<<endl;
if(key_pressed==K_ENTER) break;
}while(true);
destroyWindow("Select end frame of CT.");
return frame_index;
}
void cut_area(vector<Mat>& frames)
{
cout<<"Select the object. Align the grid to the axis of rotation."<<endl;
Rect2d r=selectROI("Select the object. Align the grid to the axis of rotation.",frames.at(0));
destroyAllWindows();
if(r.empty()) return;
int i=0;
for(i=0;i<frames.size();i++) frames.at(i)=frames.at(i)(r);
return;
}
Mat make_sinogram(vector<Mat> frames, unsigned int height)
{
Mat sinogram(frames.at(0).size().width,frames.size(),frames.at(0).type());
int frame_number=0, j;
for(frame_number=0;frame_number<frames.size();frame_number++)
{
for(j=0;j<frames.at(0).size().width;j++)
{
sinogram.at<float>(j,frame_number)=frames.at(frame_number).at<float>(height,j);
}
}
return sinogram;
}
vector<Mat> make_sinograms(vector<Mat>& frames)
{
unsigned int i;
vector<Mat> sinograms;
for(i=0;i<frames.at(0).size().height;i++) sinograms.push_back(make_sinogram(frames,i).clone());
return sinograms;
}
void remove_frames(vector<Mat>& frames, unsigned int A, unsigned int B)
{
if(B<frames.size())frames.erase(frames.begin()+B+1, frames.begin()+frames.size());
if(A>0)frames.erase(frames.begin(), frames.begin()+A);
return;
}
Mat iradon(Mat& sinogram, bool full_turn) //Sinogram must be a 32bit single channel grayscale image normalized 0-1
{
Mat reconstruction(sinogram.size().height,sinogram.size().height,CV_32FC1);
float delta_t;
if(full_turn) delta_t=2.0*M_PI/sinogram.size().width;
else delta_t=1.0*M_PI/sinogram.size().width;
unsigned int t,f,c,rho;
for(f=0;f<reconstruction.size().height;f++)
{
for(c=0;c<reconstruction.size().width;c++)
{
reconstruction.at<float>(f,c)=0;
for(t=0;t<sinogram.size().width;t++)
{
rho=((f-0.5*sinogram.size().height)*cos(delta_t*t)+(c-0.5*sinogram.size().height)*sin(delta_t*t)+0.5*sinogram.size().height);
if((rho>0)&&(rho<sinogram.size().height)) reconstruction.at<float>(f,c)+=sinogram.at<float>(rho,t);
}
if(reconstruction.at<float>(f,c)<0)reconstruction.at<float>(f,c)=0;
}
}
rotate(reconstruction,reconstruction,ROTATE_90_CLOCKWISE);
return reconstruction;
}
void convert_frame2RGB8(Mat& frame)
{
frame.convertTo(frame,CV_8UC3,1);
cvtColor(frame,frame,COLOR_GRAY2RGB);
return;
}
void convert_frames2RGB8(vector<Mat>& frames)
{
unsigned int i;
for(i=0;i<frames.size();i++) convert_frame2RGB8(frames.at(i));
return;
}
void renormalize255_frame(Mat& frame)
{
unsigned int f,c;
float maxm=0;
for(f=0;f<frame.size().height;f++)
{
for(c=0;c<frame.size().width;c++)
{
if(frame.at<float>(f,c)>maxm)maxm=frame.at<float>(f,c);
}
}
for(f=0;f<frame.size().height;f++)
{
for(c=0;c<frame.size().width;c++)
{
frame.at<float>(f,c)=frame.at<float>(f,c)*255.0/maxm;
}
}
return;
}
void renormalize255_frame(Mat& frame, float maxm)
{
unsigned int f,c;
for(f=0;f<frame.size().height;f++)
{
for(c=0;c<frame.size().width;c++)
{
frame.at<float>(f,c)=frame.at<float>(f,c)*255.0/maxm;
}
}
return;
}
void renormalize255_frame_by_frame(vector<Mat>& frames)
{
unsigned int i;
for(i=0;i<frames.size();i++) renormalize255_frame(frames.at(i));
return;
}
void renormalize255_frames(vector<Mat>& frames)
{
unsigned int f,c,i;
float maxm=0;
for(i=0;i<frames.size();i++)
{
for(f=0;f<frames.at(i).size().height;f++)
{
for(c=0;c<frames.at(i).size().width;c++)
{
if(frames.at(i).at<float>(f,c)>maxm)maxm=frames.at(i).at<float>(f,c);
}
}
}
for(i=0;i<frames.size();i++)renormalize255_frame(frames.at(i),maxm);
return;
}
void convert_frame2bw(Mat& frame)
{
unsigned int f,c;
Mat converted(frame.size().height,frame.size().width,CV_32FC1);
for(f=0;f<frame.size().height;f++)
{
for(c=0;c<frame.size().width;c++)
{
converted.at<float>(f,c)=1.0/3.0*(frame.at<Vec3f>(f,c)[0]+frame.at<Vec3f>(f,c)[1]+frame.at<Vec3f>(f,c)[2]);
//converted.at<float>(f,c)=frame.at<Vec3f>(f,c)[1];
}
}
frame=converted.clone();
return;
}
void convert_frames2bw(vector<Mat>& frames)
{
unsigned int i;
for(i=0;i<frames.size();i++) convert_frame2bw(frames.at(i));
return;
}
void convert_frame2f(Mat& frame)
{
frame.convertTo(frame,CV_32FC3,1.0/255.0);
return;
}
void convert_frames2f(vector<Mat>& frames)
{
unsigned int i;
for(i=0;i<frames.size();i++)convert_frame2f(frames.at(i));
return;
}
Mat filter_sinogram(Mat& sinogram)
{
Mat filtered_sinogram;
transpose(sinogram,filtered_sinogram);
if(filtered_sinogram.type()==CV_8UC3) //Convert to gray scale and 32bit if the input is a 8bit RGB image
{
cout<<"Converting to 32bit grayscale..."<<endl;
convert_frame2f(filtered_sinogram);
convert_frame2bw(filtered_sinogram);
}
Mat dft_sinogram[2]={filtered_sinogram,Mat::zeros(filtered_sinogram.size(),CV_32F)};
Mat dftReady;
merge(dft_sinogram,2,dftReady);
dft(dftReady,dftReady,DFT_ROWS|DFT_COMPLEX_OUTPUT,0);
split(dftReady,dft_sinogram);
unsigned int f,c;
for(f=0;f<dft_sinogram[0].size().height;f++)
{
for(c=0;c<dft_sinogram[0].size().width;c++)
{
//Sine Filter
dft_sinogram[0].at<float>(f,c)*=(1.0/(2.0*M_PI))*1.0*abs(sin(1.0*M_PI*(c)/dft_sinogram[0].size().width));
dft_sinogram[1].at<float>(f,c)*=(1.0/(2.0*M_PI))*1.0*abs(sin(1.0*M_PI*(c)/dft_sinogram[0].size().width));
}
}
merge(dft_sinogram,2,dftReady);
dft(dftReady,filtered_sinogram,DFT_INVERSE|DFT_ROWS|DFT_REAL_OUTPUT,0);
transpose(filtered_sinogram,filtered_sinogram);
return filtered_sinogram;
}
void filter_all_sinograms(vector<Mat>& sinograms)
{
unsigned int i;
for(i=0;i<sinograms.size();i++) sinograms.at(i)=filter_sinogram(sinograms.at(i)).clone();
return;
}
vector<Mat> video_iradon(vector<Mat>& sinograms, bool full_turn)
{
vector<Mat> slices;
unsigned int i, prev_perc;
for(i=0;i<sinograms.size();i++)
{
slices.push_back(iradon(sinograms.at(i),full_turn).clone());
if((int)(100.0*i/sinograms.size()-prev_perc)>0) cout<<(int)100.0*i/sinograms.size()<<"%"<<endl;
prev_perc=100.0*i/sinograms.size();
}
return slices;
}
void save_scan(vector<Mat>& slices, unsigned int i_threshold)
{
ofstream save_slices;
save_slices.open("slices.xyz");
save_slices.precision(6);
unsigned int slice, f, c;
for(slice=0;slice<slices.size();slice++)
{
for(f=0;f<slices.at(slice).size().height;f++)
{
for(c=0;c<slices.at(slice).size().width;c++)
{
if(slices.at(slice).at<float>(f,c)>i_threshold)
{
save_slices<<f<<" "<<c<<" "<<slice<<" ";
save_slices<<scientific<<slices.at(slice).at<float>(f,c);
save_slices<<endl;
}
}
}
}
save_slices.close();
return;
}
float frames_get_bg_intensity(Mat& frame, Mat& display_image)
{
float I0=0.0;
cout<<"Select a background area."<<endl;
Rect2d r=selectROI("Select a background area.",display_image);
destroyAllWindows();
if(r.empty())
{
cout<<"Background intensity set to 1"<<endl;
return 1.0;
}
Mat selected=frame(r);
unsigned int f,c;
for(f=0;f<selected.size().height;f++)
{
for(c=0;c<selected.size().width;c++)
{
I0+=selected.at<float>(f,c);
}
}
I0*=1.0/(selected.size().width*selected.size().height);
cout<<"Background intensity set to "<<I0<<endl;
return I0;
}
void frames_get_transmitance(vector<Mat>& frames, float I0)
{
unsigned int i,f,c;
for(i=0;i<frames.size();i++)
{
for(f=0;f<frames.at(i).size().height;f++)
{
for(c=0;c<frames.at(i).size().width;c++)
{
if(frames.at(i).at<float>(f,c)<I0&&frames.at(i).at<float>(f,c)>0.0)frames.at(i).at<float>(f,c)=-1.0*log10(frames.at(i).at<float>(f,c)/I0);
else if(frames.at(i).at<float>(f,c)>I0) frames.at(i).at<float>(f,c)=0.0;
else if(frames.at(i).at<float>(f,c)==0.0) frames.at(i).at<float>(f,c)=-1.0*log10((1.0/255.0)/I0);
}
}
}
return;
}
Mat create_interpolation_img(Mat& img1, Mat& img2, unsigned int extra_frames, unsigned int extra_frame_number) //expected CV_32FC1 images
{
unsigned int i,f,c;
float slope=0;
Mat interpolated_frame(img1.size().height,img1.size().width,img1.type());
for(f=0;f<img1.size().height;f++)
{
for(c=0;c<img1.size().width;c++)
{
slope=(1.0*(img2.at<float>(f,c)-img1.at<float>(f,c)))/(extra_frames+1);
interpolated_frame.at<float>(f,c)=1.0*extra_frame_number*slope+img1.at<float>(f,c);
}
}
return interpolated_frame;
}
vector<Mat> interpolate(vector<Mat>& frames, unsigned int extra_frames)
{
unsigned int i, j;
vector<Mat> interpolated;
for(i=0;i<frames.size();i++)
{
interpolated.push_back(frames.at(i).clone());
for(j=1;j<=extra_frames;j++)
{
if(i!=frames.size()-1)interpolated.push_back(create_interpolation_img(frames.at(i),frames.at(i+1),extra_frames,j).clone());
}
}
return interpolated;
}
void save_video(vector<Mat>& frames, string name)
{
VideoWriter video_reconstruction(name.c_str(),VideoWriter::fourcc('M','J','P','G'),25,frames.at(0).size(),true);
unsigned int i;
for(i=0;i<frames.size();i++) video_reconstruction.write(frames.at(i));
video_reconstruction.release();
return;
}
void fbp(vector<Mat>& frames, unsigned int extra_frames, unsigned int pointcloud_threshold, bool normalizeByframe, bool full_turn)
{
vector<Mat> sinograms, slices;
cut_area(frames);
Mat display_image=frames.at(0).clone();
convert_frames2f(frames);
gamma_correction_frames(frames);
convert_frames2bw(frames);
if(extra_frames>0)
{
cout<<"Performing interpolation..."<<endl;
frames=interpolate(frames,extra_frames);
cout<<"Number of frames: "<<frames.size()<<endl;
}
frames_get_transmitance(frames,frames_get_bg_intensity(frames.at(0),display_image));
cout<<"Building sinograms..."<<endl;
sinograms=make_sinograms(frames);
cout<<"Filtering sinograms..."<<endl;
filter_all_sinograms(sinograms);
cout<<"Performing Filtered Back Projection..."<<endl;
slices=video_iradon(sinograms,full_turn);
cout<<"Normalizing..."<<endl;
if(!normalizeByframe)renormalize255_frames(slices);
else renormalize255_frame_by_frame(slices);
cout<<"Saving point cloud..."<<endl;
save_scan(slices,pointcloud_threshold);
cout<<"Converting to RGB..."<<endl;
convert_frames2RGB8(slices);
cout<<"Saving video..."<<endl;
save_video(slices,"Reconstruction.avi");
cout<<"Finished."<<endl;
return;
}
void fbp(string input, unsigned int extra_frames, unsigned int pointcloud_threshold, bool normalizeByframe, bool full_turn)
{
unsigned int A_frame, B_frame;
vector<Mat> frames, sinograms, slices;
frames=video_to_array(input.c_str());
A_frame=begin_frame(frames);
B_frame=end_frame(frames,A_frame);
remove_frames(frames,A_frame,B_frame);
cout<<"Number of frames: "<<frames.size()<<endl;
cut_area(frames);
Mat display_image=frames.at(0).clone();
convert_frames2f(frames);
gamma_correction_frames(frames);
convert_frames2bw(frames);
if(extra_frames>0)
{
cout<<"Performing interpolation..."<<endl;
frames=interpolate(frames,extra_frames);
cout<<"Number of frames: "<<frames.size()<<endl;
}
frames_get_transmitance(frames,frames_get_bg_intensity(frames.at(0),display_image));
cout<<"Building sinograms..."<<endl;
sinograms=make_sinograms(frames);
cout<<"Filtering sinograms..."<<endl;
filter_all_sinograms(sinograms);
cout<<"Performing Filtered Back Projection..."<<endl;
slices=video_iradon(sinograms,full_turn);
cout<<"Normalizing..."<<endl;
if(!normalizeByframe)renormalize255_frames(slices);
else renormalize255_frame_by_frame(slices);
cout<<"Saving point cloud..."<<endl;
save_scan(slices,pointcloud_threshold);
cout<<"Converting to RGB..."<<endl;
convert_frames2RGB8(slices);
cout<<"Saving video..."<<endl;
save_video(slices,"Reconstruction.avi");
cout<<"Finished."<<endl;
return;
}