InpaintingConverger.cpp 5.5 KB
Newer Older
sarthou's avatar
sarthou committed
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
//
// Created by Jonathan on 23/02/2016.
//

#include "InpaintingConverger.h"

InpaintingConverger::InpaintingConverger(int const downsamplingLevels, int const downsamplingRate,
                     int const nbIterations, float const residualThreshold, InpaintingSearcher * const iSearcher,
                     InpaintingReconstructor * const iReconstructor, InpaintingEvaluator * const iEvaluator)
        : downsamplingLevels(downsamplingLevels), downsamplingRate(downsamplingRate),
          nbIterations(nbIterations), residualThreshold(residualThreshold),
          iSearcher(iSearcher), iReconstructor(iReconstructor), iEvaluator(iEvaluator)
{}

InpaintingConverger::~InpaintingConverger() {}

void InpaintingConverger::Inpaint(InpaintingDataSource * const iDataSource, InpaintingProfiler * const iProfiler) {
    IDSDDownsampling downsampledDataSource = IDSDDownsampling(iDataSource, downsamplingLevels, downsamplingRate);

    InpaintingOpenCVIO openCVIO = InpaintingOpenCVIO();

    for(int curLevel = downsamplingLevels - 1; curLevel >= 0; curLevel--){
        iProfiler->BeforeInpaintLevel(curLevel);

        downsampledDataSource.SetCurLevel(curLevel);
        IDSDDetailedOcclusion detailedOcclusionDataSource = IDSDDetailedOcclusion(&downsampledDataSource,\
          iEvaluator->GetPatchDim().GetSizeX(), iEvaluator->GetPatchDim().GetSizeY(),\
          iEvaluator->GetPatchDim().GetSizeZ());

        if(curLevel < downsamplingLevels - 1)
            iReconstructor->Reconstruct(&detailedOcclusionDataSource, false);

        float residual = FLT_MAX;
        iProfiler->BeforeInpaintingIterations(curLevel);
        for(int iteration = 0; iteration < nbIterations && residual > residualThreshold; iteration++){
            iProfiler->BeforeInpaintingIteration(curLevel, iteration);
            if(curLevel == downsamplingLevels-1 && iteration == 0)
38
39
40
41
42
            {
              //OnionPeelInitialisation(&downsampledDataSource, iProfiler);
              LoadingInitialisation(&downsampledDataSource,iProfiler);
            }

sarthou's avatar
sarthou committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
            else{
                iSearcher->Search(&detailedOcclusionDataSource, false);
                residual = iReconstructor->Reconstruct(&detailedOcclusionDataSource, false);
            }
            iProfiler->AfterInpaintingIteration(curLevel, iteration);
        }

        if(curLevel != 0)
            downsampledDataSource.UpsampleShiftMatrices();

        iProfiler->AfterInpaintingIterations(curLevel);
        iProfiler->AfterInpaintLevel(curLevel);
    }
}

void InpaintingConverger::OnionPeelInitialisation(InpaintingDataSource * const iDataSource, InpaintingProfiler * const iProfiler) {
    iProfiler->BeforeInitialisation();

    IDSDDetailedOcclusionPeeling peelingDataSource = \
    IDSDDetailedOcclusionPeeling(iDataSource, iEvaluator->GetPatchDim().GetSizeX(),\
    iEvaluator->GetPatchDim().GetSizeY(), iEvaluator->GetPatchDim().GetSizeZ());

65
66
    InpaintingOpenCVIO openCVIO = InpaintingOpenCVIO();

sarthou's avatar
sarthou committed
67
68
69
70
71
72
73
74
75
76
77
78
79
    // Slowly peel back the layers!
    int peelNo = 0;
    do{
        iProfiler->BeforeInitialisationPeel(peelNo);


        iSearcher->Search(&peelingDataSource, true);
        iReconstructor->Reconstruct(&peelingDataSource, true);

        iProfiler->AfterInitialisationPeel(peelNo++);
    }while(peelingDataSource.Peel());

    iProfiler->AfterInitialisation();
80
81
82
83
84
85
86
87
}
void InpaintingConverger::LoadingInitialisation(InpaintingDataSource * const iDataSource,InpaintingProfiler * const iProfiler){
  iProfiler->BeforeInitialisation();
  InpaintingOpenCVIO openCVIO = InpaintingOpenCVIO();

  openCVIO.LoadRGBMatrixFromVideo("Data/onion_debug/peel_compare/umbrella_after_peel.avi",\
  iDataSource->GetRed(), iDataSource->GetGreen(), iDataSource->GetBlue());

88
89
90
  PaddedMatrix<int> * const frame_x = new PaddedMatrix<int>(iDataSource->GetDim());
  PaddedMatrix<int> * const frame_y = new PaddedMatrix<int>(iDataSource->GetDim());
  PaddedMatrix<int> * const frame_z = new PaddedMatrix<int>(iDataSource->GetDim());
91
92
93
  PaddedMatrix<float> * const frame_a = new PaddedMatrix<float>(iDataSource->GetDim());
  PaddedMatrix<float> * const frame_gx = new PaddedMatrix<float>(iDataSource->GetDim());
  PaddedMatrix<float> * const frame_gy = new PaddedMatrix<float>(iDataSource->GetDim());
94

95
96
97
  for(int nbFrame=0;nbFrame<iDataSource->GetDim()->GetSizeZ();nbFrame++)
  {
    openCVIO.LoadMatrixFromCSV("Data/onion_debug/peel_compare/end_peel_disp/M_EndPeel_DispX_"\
98
99
100
    +std::to_string(nbFrame)+".csv",frame_x,1);
    iDataSource->GetDispX()->SetFrame(frame_x,nbFrame);

101
    openCVIO.LoadMatrixFromCSV("Data/onion_debug/peel_compare/end_peel_disp/M_EndPeel_DispY_"\
102
103
    +std::to_string(nbFrame)+".csv",frame_y,1);
    iDataSource->GetDispY()->SetFrame(frame_y,nbFrame);
104

105
106
107
    openCVIO.LoadMatrixFromCSV("Data/onion_debug/peel_compare/end_peel_disp/M_EndPeel_DispZ_"\
    +std::to_string(nbFrame)+".csv",frame_z,1);
    iDataSource->GetDispZ()->SetFrame(frame_z,nbFrame);
108
109
110
111
112
113
114
115
116
117
118
119
120

    openCVIO.LoadMatrixFromCSV("Data/onion_debug/peel_compare/end_peel_disp/M_EndPeel_DispA_"\
    +std::to_string(nbFrame)+".csv",frame_a,1);
    iDataSource->GetDispA()->SetFrame(frame_a,nbFrame);

    openCVIO.LoadMatrixFromCSV("Data/onion_debug/peel_compare/end_peel_disp/M_EndPeel_GradX_"\
    +std::to_string(nbFrame)+".csv",frame_gx,1);
    iDataSource->GetGradX()->SetFrame(frame_gx,nbFrame);

    openCVIO.LoadMatrixFromCSV("Data/onion_debug/peel_compare/end_peel_disp/M_EndPeel_GradY_"\
    +std::to_string(nbFrame)+".csv",frame_gy,1);
    iDataSource->GetGradY()->SetFrame(frame_gy,nbFrame);

121
  }
122
123
124

  //iDataSource->GetDispX()->GetFrame(27)->Mprint();

125
  iProfiler->AfterInitialisation();
sarthou's avatar
sarthou committed
126
127

}