example2d.cpp 1.59 KB
Newer Older
Tobias Lasser's avatar
Tobias Lasser committed
1
2
3
4
5
6
7
8
/// Elsa example program: basic 2d X-ray CT simulation and reconstruction

#include "elsa.h"

#include <iostream>

using namespace elsa;

Tobias Lasser's avatar
Tobias Lasser committed
9
10
11
void example2d()
{
    // generate 2d phantom
12
13
    IndexVector_t size(2);
    size << 128, 128;
Tobias Lasser's avatar
Tobias Lasser committed
14
15
16
    auto phantom = PhantomGenerator<real_t>::createModifiedSheppLogan(size);

    // write the phantom out
17
    EDF::write(phantom, "2dphantom.edf");
Tobias Lasser's avatar
Tobias Lasser committed
18

Tobias Lasser's avatar
Tobias Lasser committed
19
20
    // generate circular trajectory
    index_t noAngles{100}, arc{360};
21
22
    auto [geometry, sinoDescriptor] = CircleTrajectoryGenerator::createTrajectory(
        noAngles, phantom.getDataDescriptor(), arc, size(0) * 100, size(0));
Tobias Lasser's avatar
Tobias Lasser committed
23
24

    // setup operator for 2d X-ray transform
25
    Logger::get("Info")->info("Simulating sinogram using Siddon's method");
26
    SiddonsMethod projector(phantom.getDataDescriptor(), *sinoDescriptor, geometry);
Tobias Lasser's avatar
Tobias Lasser committed
27
28
29
30
31

    // simulate the sinogram
    auto sinogram = projector.apply(phantom);

    // write the sinogram out
32
    EDF::write(sinogram, "2dsinogram.edf");
Tobias Lasser's avatar
Tobias Lasser committed
33
34
35
36
37

    // setup reconstruction problem
    WLSProblem problem(projector, sinogram);

    // solve the reconstruction problem
38
    GradientDescent solver(problem, 1.0 / size.prod());
Tobias Lasser's avatar
Tobias Lasser committed
39
40

    index_t noIterations{50};
41
42
    Logger::get("Info")->info("Solving reconstruction using {} iterations of gradient descent",
                              noIterations);
Tobias Lasser's avatar
Tobias Lasser committed
43
    auto reconstruction = solver.solve(noIterations);
Tobias Lasser's avatar
Tobias Lasser committed
44
45

    // write the reconstruction out
46
    EDF::write(reconstruction, "2dreconstruction.edf");
Tobias Lasser's avatar
Tobias Lasser committed
47
48
}

Tobias Lasser's avatar
Tobias Lasser committed
49
50
int main()
{
Tobias Lasser's avatar
Tobias Lasser committed
51
52
    try {
        example2d();
53
    } catch (std::exception& e) {
Tobias Lasser's avatar
Tobias Lasser committed
54
55
        std::cerr << "An exception occurred: " << e.what() << "\n";
    }
56
}