/
Propagator.hpp
165 lines (129 loc) · 5.33 KB
/
Propagator.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
/*=========================================================================
*
* Copyright David Doria 2012 daviddoria@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef Propagator_HPP
#define Propagator_HPP
#include "Propagator.h"
#include <algorithm>
#include "itkImageRegionIteratorWithIndex.h"
template <typename TPatchDistanceFunctor>
unsigned int Propagator<TPatchDistanceFunctor>::
Propagate(NNFieldType* const nnField)
{
assert(this->PatchDistanceFunctor);
// Pixels near the border do not have fully defined patches (the patches that they are the center of are not fully inside the image)
itk::ImageRegion<2> internalRegion = ITKHelpers::GetInternalRegion(nnField->GetLargestPossibleRegion(), this->PatchRadius);
if(this->TargetPixels.size() == 0)
{
this->TargetPixels = PatchMatchHelpers::GetAllPixelIndices(internalRegion);
}
std::vector<itk::Index<2> > targetPixels = this->TargetPixels;
if(!this->Forward)
{
std::reverse(targetPixels.begin(), targetPixels.end());
}
// std::cout << "Propagation(): There are " << targetPixels.size()
// << " pixels that would like to be processed." << std::endl;
unsigned int numberOfPropagatedPixels = 0;
for(size_t targetPixelId = 0; targetPixelId < targetPixels.size(); ++targetPixelId)
{
itk::Index<2> targetPixel = targetPixels[targetPixelId];
//ProcessPixelSignal(targetPixel);
itk::ImageRegion<2> targetRegion =
ITKHelpers::GetRegionInRadiusAroundPixel(targetPixel, this->PatchRadius);
std::vector<itk::Offset<2> > propagationOffsets = GetPropagationOffsets();
bool propagated = false;
for(size_t propagationOffsetId = 0;
propagationOffsetId < propagationOffsets.size();
++propagationOffsetId)
{
itk::Offset<2> propagationOffset = propagationOffsets[propagationOffsetId];
// The potential match is the opposite (hence the " - offset" in the following line)
// of the offset of the neighbor. Consider the following case:
// - We are at (4,4) and potentially propagating from (3,4)
// - The best match to (3,4) is (10,10)
// - potentialMatch should be (11,10), because since the current pixel is 1 to the right
// of the neighbor, we need to consider the patch one to the right of the neighbors best match
itk::Index<2> nnFieldLocation = targetPixel + propagationOffset;
if(!internalRegion.IsInside(nnFieldLocation))
{
continue; // We don't want to propagate information from outside of the
// viable NN field region
}
NNFieldType::PixelType nnFieldPixel = nnField->GetPixel(nnFieldLocation);
itk::Index<2> bestMatchPixel =
ITKHelpers::GetRegionCenter(nnFieldPixel.GetRegion());
itk::Index<2> potentialMatchPixel = bestMatchPixel - propagationOffset;
if(!internalRegion.IsInside(potentialMatchPixel))
{
continue; // We don't want to propagate information from outside of the
// viable NN field region
}
itk::ImageRegion<2> potentialMatchRegion =
ITKHelpers::GetRegionInRadiusAroundPixel(potentialMatchPixel, this->PatchRadius);
float distance = this->PatchDistanceFunctor->Distance(potentialMatchRegion, targetRegion);
Match potentialMatch;
potentialMatch.SetRegion(potentialMatchRegion);
potentialMatch.SetScore(distance);
// If there were previous matches, add this one if it is better
Match currentMatch = nnField->GetPixel(targetPixel);
if(potentialMatch.GetScore() < currentMatch.GetScore())
{
nnField->SetPixel(targetPixel, potentialMatch);
}
//PropagatedSignal(nnField);
propagated = true;
} // end loop over potentialPropagationPixels
if(propagated)
{
numberOfPropagatedPixels++;
}
} // end loop over target pixels
// Reverse the propagation for the next iteration
this->Forward = !this->Forward;
//std::cout << "Propagation() propagated " << propagatedPixels << " pixels." << std::endl;
//std::cout << "AcceptanceTest failed " << acceptanceTestFailed << std::endl;
return numberOfPropagatedPixels;
}
template <typename TPatchDistanceFunctor>
std::vector<itk::Offset<2> > Propagator<TPatchDistanceFunctor>::
GetPropagationOffsets()
{
std::vector<itk::Offset<2> > propagationOffsets;
if(this->Forward)
{
itk::Offset<2> offset;
offset[0] = -1;
offset[1] = 0;
propagationOffsets.push_back(offset);
offset[0] = 0;
offset[1] = -1;
propagationOffsets.push_back(offset);
}
else
{
itk::Offset<2> offset;
offset[0] = 1;
offset[1] = 0;
propagationOffsets.push_back(offset);
offset[0] = 0;
offset[1] = 1;
propagationOffsets.push_back(offset);
}
return propagationOffsets;
}
#endif