-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathInpaintingWithLocalSearch.cpp
More file actions
281 lines (223 loc) · 13.1 KB
/
InpaintingWithLocalSearch.cpp
File metadata and controls
281 lines (223 loc) · 13.1 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
/*=========================================================================
*
* Copyright David Doria 2011 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.
*
*=========================================================================*/
// Helpers
//#include "Helpers/OutputHelpers.h"
// Custom
#include "Utilities/IndirectPriorityQueue.h"
#include "SearchRegions/NeighborhoodSearch.hpp"
#include "NearestNeighbor/LinearSearchBest/Property.hpp"
// Pixel descriptors
#include "PixelDescriptors/ImagePatchPixelDescriptor.h"
#include "PixelDescriptors/ImagePatchVectorized.h"
#include "PixelDescriptors/ImagePatchVectorizedIndices.h"
// Acceptance visitors
#include "Visitors/AcceptanceVisitors/AlwaysAccept.hpp"
#include "Visitors/AcceptanceVisitors/DefaultAcceptanceVisitor.hpp"
#include "Visitors/AcceptanceVisitors/AverageDifferenceAcceptanceVisitor.hpp"
#include "Visitors/AcceptanceVisitors/CompositeAcceptanceVisitor.hpp"
#include "Visitors/AcceptanceVisitors/ANDAcceptanceVisitor.hpp"
#include "Visitors/AcceptanceVisitors/DilatedSourceHoleTargetValidAcceptanceVisitor.hpp"
#include "Visitors/AcceptanceVisitors/DilatedSourceValidTargetValidAcceptanceVisitor.hpp"
#include "Visitors/AcceptanceVisitors/SourceHoleTargetValidCompare.hpp"
#include "Visitors/AcceptanceVisitors/SourceValidTargetValidCompare.hpp"
#include "Visitors/AcceptanceVisitors/HoleSizeAcceptanceVisitor.hpp"
#include "Visitors/AcceptanceVisitors/VarianceFunctor.hpp"
#include "Visitors/AcceptanceVisitors/AverageFunctor.hpp"
#include "Visitors/AcceptanceVisitors/ScoreThresholdAcceptanceVisitor.hpp"
#include "Visitors/AcceptanceVisitors/CorrelationAcceptanceVisitor.hpp"
#include "Visitors/AcceptanceVisitors/PatchDistanceAcceptanceVisitor.hpp"
#include "Visitors/AcceptanceVisitors/HistogramDifferenceAcceptanceVisitor.hpp"
#include "Visitors/AcceptanceVisitors/HoleHistogramDifferenceAcceptanceVisitor.hpp"
// #include "Visitors/AcceptanceVisitors/QuadrantHistogramCompareAcceptanceVisitor.hpp"
#include "Visitors/AcceptanceVisitors/AllQuadrantHistogramCompareAcceptanceVisitor.hpp"
// Descriptor visitors
#include "Visitors/DescriptorVisitors/ImagePatchDescriptorVisitor.hpp"
#include "Visitors/DescriptorVisitors/ImagePatchVectorizedIndicesVisitor.hpp"
#include "Visitors/DescriptorVisitors/ImagePatchVectorizedVisitor.hpp"
#include "Visitors/DescriptorVisitors/CompositeDescriptorVisitor.hpp"
// Inpainting visitors
#include "Visitors/InpaintingVisitors/InpaintingVisitor.hpp"
#include "Visitors/ReplayVisitor.hpp"
#include "Visitors/InformationVisitors/LoggerVisitor.hpp"
//#include "Visitors/CompositeInpaintingVisitor.hpp"
#include "Visitors/InformationVisitors/DebugVisitor.hpp"
// Nearest neighbors
#include "NearestNeighbor/LinearSearchKNNProperty.hpp"
#include "NearestNeighbor/DefaultSearchBest.hpp"
//#include "NearestNeighbor/LinearSearchBestProperty.hpp"
//#include "NearestNeighbor/VisualSelectionBest.hpp"
// Nearest neighbors visitor
#include "Visitors/NearestNeighborsDisplayVisitor.hpp"
// Initializers
#include "Initializers/InitializeFromMaskImage.hpp"
#include "Initializers/InitializePriority.hpp"
// Inpainters
#include "Inpainters/PatchInpainter.hpp"
// Difference functions
#include "DifferenceFunctions/Patch/ImagePatchDifference.hpp"
#include "DifferenceFunctions/Pixel/SumAbsolutePixelDifference.hpp"
// Inpainting algorithm
#include "Algorithms/InpaintingAlgorithmWithLocalSearch.hpp"
// Priority
#include "Priority/PriorityRandom.h"
#include "Priority/PriorityCriminisi.h"
// ITK
#include "itkImageFileReader.h"
// Boost
#include <boost/graph/grid_graph.hpp>
#include <boost/property_map/property_map.hpp>
#include <boost/graph/detail/d_ary_heap.hpp>
// Run with: Data/trashcan.mha Data/trashcan_mask.mha 15 filled.mha
int main(int argc, char *argv[])
{
// Verify arguments
if(argc != 6)
{
std::cerr << "Required arguments: image.mha imageMask.mha patchHalfWidth neighborhoodRadius output.mha" << std::endl;
std::cerr << "Input arguments: ";
for(int i = 1; i < argc; ++i)
{
std::cerr << argv[i] << " ";
}
return EXIT_FAILURE;
}
// Parse arguments
std::string imageFileName = argv[1];
std::string maskFileName = argv[2];
std::stringstream ssPatchRadius;
ssPatchRadius << argv[3];
unsigned int patchHalfWidth = 0;
ssPatchRadius >> patchHalfWidth;
// The percent of the image size to use as the neighborhood (0 - 1)
std::stringstream ssNeighborhoodPercent;
ssNeighborhoodPercent << argv[4];
float neighborhoodPercent = 0;
ssNeighborhoodPercent >> neighborhoodPercent;
std::string outputFileName = argv[5];
// Output arguments
std::cout << "Reading image: " << imageFileName << std::endl;
std::cout << "Reading mask: " << maskFileName << std::endl;
std::cout << "Patch half width: " << patchHalfWidth << std::endl;
std::cout << "Neighborhood percent: " << neighborhoodPercent << std::endl;
std::cout << "Output: " << outputFileName << std::endl;
typedef itk::Image<itk::CovariantVector<int, 3>, 2> ImageType;
typedef itk::ImageFileReader<ImageType> ImageReaderType;
ImageReaderType::Pointer imageReader = ImageReaderType::New();
imageReader->SetFileName(imageFileName);
imageReader->Update();
ImageType::Pointer image = ImageType::New();
ITKHelpers::DeepCopy(imageReader->GetOutput(), image.GetPointer());
Mask::Pointer mask = Mask::New();
mask->Read(maskFileName);
std::cout << "hole pixels: " << mask->CountHolePixels() << std::endl;
std::cout << "valid pixels: " << mask->CountValidPixels() << std::endl;
typedef ImagePatchPixelDescriptor<ImageType> ImagePatchPixelDescriptorType;
// Create the graph
typedef boost::grid_graph<2> VertexListGraphType;
boost::array<std::size_t, 2> graphSideLengths = { { imageReader->GetOutput()->GetLargestPossibleRegion().GetSize()[0],
imageReader->GetOutput()->GetLargestPossibleRegion().GetSize()[1] } };
// VertexListGraphType graph(graphSideLengths);
std::shared_ptr<VertexListGraphType> graph(new VertexListGraphType(graphSideLengths));
typedef boost::graph_traits<VertexListGraphType>::vertex_descriptor VertexDescriptorType;
//ImagePatchDescriptorMapType smallImagePatchDescriptorMap(num_vertices(graph), indexMap);
// Create the patch inpainter. The inpainter needs to know the status of each pixel to determine if they should be inpainted.
typedef PatchInpainter<ImageType> ImageInpainterType;
std::shared_ptr<ImageInpainterType> imagePatchInpainter(new
ImageInpainterType(patchHalfWidth, image, mask));
// Create the priority function
typedef PriorityRandom PriorityType;
std::shared_ptr<PriorityType> priorityFunction(new PriorityType);
// typedef PriorityCriminisi<ImageType> PriorityType;
// std::shared_ptr<PriorityType> priorityFunction(new PriorityType(image, mask, patchHalfWidth));
typedef IndirectPriorityQueue<VertexListGraphType> BoundaryNodeQueueType;
std::shared_ptr<BoundaryNodeQueueType> boundaryNodeQueue(new BoundaryNodeQueueType(*graph));
// Create the descriptor map. This is where the data for each pixel is stored.
typedef boost::vector_property_map<ImagePatchPixelDescriptorType, BoundaryNodeQueueType::IndexMapType> ImagePatchDescriptorMapType;
// ImagePatchDescriptorMapType imagePatchDescriptorMap(num_vertices(graph), indexMap);
std::shared_ptr<ImagePatchDescriptorMapType> imagePatchDescriptorMap(new
ImagePatchDescriptorMapType(num_vertices(*graph), *(boundaryNodeQueue->GetIndexMap())));
// Create the descriptor visitor
typedef ImagePatchDescriptorVisitor<VertexListGraphType, ImageType, ImagePatchDescriptorMapType>
ImagePatchDescriptorVisitorType;
// ImagePatchDescriptorVisitorType imagePatchDescriptorVisitor(image, mask, imagePatchDescriptorMap, patchHalfWidth);
std::shared_ptr<ImagePatchDescriptorVisitorType> imagePatchDescriptorVisitor(new
ImagePatchDescriptorVisitorType(image.GetPointer(), mask,
imagePatchDescriptorMap, patchHalfWidth));
/* ImagePatchDescriptorVisitor(TImage* const in_image, Mask* const in_mask,
std::shared_ptr<TDescriptorMap> in_descriptorMap,
const unsigned int in_half_width) : */
typedef ImagePatchDifference<ImagePatchPixelDescriptorType, SumAbsolutePixelDifference<ImageType::PixelType> >
ImagePatchDifferenceType;
// typedef CompositeDescriptorVisitor<VertexListGraphType> CompositeDescriptorVisitorType;
// CompositeDescriptorVisitorType compositeDescriptorVisitor;
// compositeDescriptorVisitor.AddVisitor(imagePatchDescriptorVisitor);
// Create the descriptor visitor
// typedef CompositeAcceptanceVisitor<VertexListGraphType> CompositeAcceptanceVisitorType;
// CompositeAcceptanceVisitorType compositeAcceptanceVisitor;
typedef DefaultAcceptanceVisitor<VertexListGraphType> AcceptanceVisitorType;
std::shared_ptr<AcceptanceVisitorType> acceptanceVisitor(new AcceptanceVisitorType);
// typedef AlwaysAccept<VertexListGraphType> AcceptanceVisitorType;
// AcceptanceVisitorType acceptanceVisitor;
// If the hole is less than 15% of the patch, always accept the initial best match
// HoleSizeAcceptanceVisitor<VertexListGraphType> holeSizeAcceptanceVisitor(mask, patchHalfWidth, .15);
// compositeAcceptanceVisitor.AddOverrideVisitor(&holeSizeAcceptanceVisitor);
// AllQuadrantHistogramCompareAcceptanceVisitor<VertexListGraphType, ImageType>
// allQuadrantHistogramCompareAcceptanceVisitor(image, mask, patchHalfWidth, 8.0f); // Crazy low
// compositeAcceptanceVisitor.AddRequiredPassVisitor(&allQuadrantHistogramCompareAcceptanceVisitor);
// template <typename TGraph, typename TBoundaryNodeQueue,
// typename TDescriptorVisitor, typename TAcceptanceVisitor, typename TPriority>
typedef InpaintingVisitor<VertexListGraphType, BoundaryNodeQueueType,
ImagePatchDescriptorVisitorType, AcceptanceVisitorType, PriorityType>
InpaintingVisitorType;
std::shared_ptr<InpaintingVisitorType> inpaintingVisitor(new InpaintingVisitorType(mask, boundaryNodeQueue,
imagePatchDescriptorVisitor, acceptanceVisitor,
priorityFunction, patchHalfWidth, "InpaintingVisitor"));
// typedef InpaintingVisitor<VertexListGraphType, BoundaryNodeQueueType,
// ImagePatchDescriptorVisitorType, AcceptanceVisitorType, PriorityType>
// InpaintingVisitorType;
// std::shared_ptr<InpaintingVisitorType> inpaintingVisitor(new InpaintingVisitorType(mask, boundaryNodeQueue,
// imagePatchDescriptorVisitor, acceptanceVisitor,
// priorityFunction, patchHalfWidth, "InpaintingVisitor"));
// typedef DebugVisitor<VertexListGraphType, ImageType, BoundaryStatusMapType, BoundaryNodeQueueType> DebugVisitorType;
// DebugVisitorType debugVisitor(image, mask, patchHalfWidth, boundaryStatusMap, boundaryNodeQueue);
LoggerVisitor<VertexListGraphType> loggerVisitor("log.txt");
InitializePriority(mask, boundaryNodeQueue.get(), priorityFunction.get());
// Initialize the boundary node queue from the user provided mask image.
InitializeFromMaskImage<InpaintingVisitorType, VertexDescriptorType>(mask, inpaintingVisitor.get());
// For debugging we use LinearSearchBestProperty instead of DefaultSearchBest because it can output the difference value.
typedef LinearSearchBestProperty<ImagePatchDescriptorMapType,
ImagePatchDifferenceType > BestSearchType;
std::shared_ptr<BestSearchType> linearSearchBest(new BestSearchType(*imagePatchDescriptorMap));
typedef NeighborhoodSearch<VertexDescriptorType, ImagePatchDescriptorMapType> NeighborhoodSearchType;
NeighborhoodSearchType neighborhoodSearch(image->GetLargestPossibleRegion(), image->GetLargestPossibleRegion().GetSize()[0] * neighborhoodPercent, *imagePatchDescriptorMap);
InpaintingAlgorithmWithLocalSearch<VertexListGraphType, InpaintingVisitorType,
BoundaryNodeQueueType, NeighborhoodSearchType,
ImageInpainterType, BestSearchType>(graph, inpaintingVisitor, boundaryNodeQueue,
linearSearchBest, imagePatchInpainter, neighborhoodSearch);
// If the output filename is a png file, then use the RGBImage writer so that it is first
// casted to unsigned char. Otherwise, write the file directly.
if(Helpers::GetFileExtension(outputFileName) == "png")
{
ITKHelpers::WriteRGBImage(image.GetPointer(), outputFileName);
}
else
{
ITKHelpers::WriteImage(image.GetPointer(), outputFileName);
}
return EXIT_SUCCESS;
}