-
Notifications
You must be signed in to change notification settings - Fork 17
/
InpaintingWithTopPatchDisplay.cpp
221 lines (174 loc) · 9.56 KB
/
InpaintingWithTopPatchDisplay.cpp
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
/*=========================================================================
*
* Copyright David Doria 2011 [email protected]
*
* 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.
*
*=========================================================================*/
// Pixel descriptors
#include "PixelDescriptors/ImagePatchPixelDescriptor.h"
// Descriptor visitors
#include "Visitors/DescriptorVisitors/ImagePatchDescriptorVisitor.hpp"
// Information visitors
#include "Visitors/InformationVisitors/DisplayVisitor.hpp"
// Inpainting visitors
#include "Visitors/InpaintingVisitor.hpp"
#include "Visitors/CompositeInpaintingVisitor.hpp"
// Nearest neighbors
#include "NearestNeighbor/LinearSearchBest/Property.hpp"
#include "NearestNeighbor/LinearSearchKNNProperty.hpp"
#include "NearestNeighbor/TwoStepNearestNeighbor.hpp"
// Visitors
#include "Visitors/NearestNeighborsDisplayVisitor.hpp"
#include "Visitors/AcceptanceVisitors/DefaultAcceptanceVisitor.hpp"
// Initializers
#include "Initializers/InitializeFromMaskImage.hpp"
#include "Initializers/InitializePriority.hpp"
// Inpainters
#include "Inpainters/PatchInpainter.hpp"
// Difference functions
#include "DifferenceFunctions/SumSquaredPixelDifference.hpp"
#include "DifferenceFunctions/ImagePatchDifference.hpp"
// Inpainting
#include "Algorithms/InpaintingAlgorithm.hpp"
// Priority
#include "Priority/PriorityRandom.h"
// ITK
#include "itkImageFileReader.h"
// Boost
#include <boost/graph/grid_graph.hpp>
#include <boost/property_map/property_map.hpp>
// Custom
#include "Utilities/IndirectPriorityQueue.h"
// Qt
#include <QApplication>
#include <QtConcurrentRun>
// GUI
#include "Interactive/BasicViewerWidget.h"
#include "Interactive/TopPatchesWidget.h"
// Manual selection
#include "Visitors/ManualSelectionDefaultVisitor.hpp"
// Run with: Data/trashcan.mha Data/trashcan_mask.mha 15 filled.mha
int main(int argc, char *argv[])
{
// Verify arguments
if(argc != 5)
{
std::cerr << "Required arguments: image.mha imageMask.mha patch_half_width 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;
std::string outputFilename = argv[4];
// 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 << "Output: " << outputFilename << std::endl;
typedef itk::VectorImage<float, 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);
typedef boost::graph_traits<VertexListGraphType>::vertex_descriptor VertexDescriptorType;
// Queue
typedef IndirectPriorityQueue<VertexListGraphType> BoundaryNodeQueueType;
BoundaryNodeQueueType boundaryNodeQueue(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), boundaryNodeQueue.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> InpainterType;
InpainterType patchInpainter(patchHalfWidth, image.GetPointer(), mask);
// Create the priority function
typedef PriorityRandom PriorityType;
PriorityType priorityFunction;
// Create the descriptor visitor
typedef ImagePatchDescriptorVisitor<VertexListGraphType, ImageType, ImagePatchDescriptorMapType> ImagePatchDescriptorVisitorType;
ImagePatchDescriptorVisitorType imagePatchDescriptorVisitor(image, mask, imagePatchDescriptorMap, patchHalfWidth);
// Setup acceptance visitor
typedef DefaultAcceptanceVisitor<VertexListGraphType> AcceptanceVisitorType;
AcceptanceVisitorType acceptanceVisitor;
// Create the inpainting visitor
typedef InpaintingVisitor<VertexListGraphType, BoundaryNodeQueueType,
ImagePatchDescriptorVisitorType, AcceptanceVisitorType, PriorityType, ImageType>
InpaintingVisitorType;
InpaintingVisitorType inpaintingVisitor(mask, boundaryNodeQueue,
imagePatchDescriptorVisitor, acceptanceVisitor,
&priorityFunction, patchHalfWidth, "InpaintingVisitor", image.GetPointer());
typedef DisplayVisitor<VertexListGraphType, ImageType> DisplayVisitorType;
DisplayVisitorType displayVisitor(image, mask, patchHalfWidth);
typedef CompositeInpaintingVisitor<VertexListGraphType> CompositeVisitorType;
CompositeVisitorType compositeVisitor;
compositeVisitor.AddVisitor(&inpaintingVisitor);
compositeVisitor.AddVisitor(&displayVisitor);
InitializePriority(mask, boundaryNodeQueue, &priorityFunction);
// Initialize the boundary node queue from the user provided mask image.
InitializeFromMaskImage<InpaintingVisitorType, VertexDescriptorType>(mask, &inpaintingVisitor);
std::cout << "PatchBasedInpaintingNonInteractive: There are " << boundaryNodeQueue.size()
<< " nodes in the boundaryNodeQueue" << std::endl;
// Create the nearest neighbor finders
typedef SumSquaredPixelDifference<ImageType::PixelType> PixelDifferenceType;
typedef ImagePatchDifference<ImagePatchPixelDescriptorType, PixelDifferenceType> PatchDifferenceType;
typedef LinearSearchKNNProperty<ImagePatchDescriptorMapType,
PatchDifferenceType > KNNSearchType;
KNNSearchType knnSearch(imagePatchDescriptorMap, 1000);
typedef LinearSearchBestProperty<ImagePatchDescriptorMapType, PatchDifferenceType > BestSearchType;
BestSearchType linearSearchBest(imagePatchDescriptorMap);
NearestNeighborsDisplayVisitor nearestNeighborsDisplayVisitor;
typedef TwoStepNearestNeighbor<KNNSearchType, BestSearchType, NearestNeighborsDisplayVisitor> TwoStepSearchType;
TwoStepSearchType twoStepSearch(knnSearch, linearSearchBest, &nearestNeighborsDisplayVisitor);
// Setup the GUI
QApplication app( argc, argv );
BasicViewerWidget<ImageType> basicViewerWidget(image, mask);
basicViewerWidget.show();
// These connections are Qt::BlockingQueuedConnection because the algorithm quickly goes on to fill the hole, and since we are sharing the image memory, we want to make sure these things are
// refreshed at the right time, not after the hole has already been filled (this actually happens, it is not just a theoretical thing).
QObject::connect(&displayVisitor, SIGNAL(signal_RefreshImage()), &basicViewerWidget, SLOT(slot_UpdateImage()), Qt::BlockingQueuedConnection);
QObject::connect(&displayVisitor, SIGNAL(signal_RefreshSource(const itk::ImageRegion<2>&, const itk::ImageRegion<2>&)),
&basicViewerWidget, SLOT(slot_UpdateSource(const itk::ImageRegion<2>&, const itk::ImageRegion<2>&)), Qt::BlockingQueuedConnection);
QObject::connect(&displayVisitor, SIGNAL(signal_RefreshTarget(const itk::ImageRegion<2>&)), &basicViewerWidget, SLOT(slot_UpdateTarget(const itk::ImageRegion<2>&)), Qt::BlockingQueuedConnection);
QObject::connect(&displayVisitor, SIGNAL(signal_RefreshResult(const itk::ImageRegion<2>&, const itk::ImageRegion<2>&)),
&basicViewerWidget, SLOT(slot_UpdateResult(const itk::ImageRegion<2>&, const itk::ImageRegion<2>&)), Qt::BlockingQueuedConnection);
TopPatchesWidget<ImageType> topPatchesWidget(image, patchHalfWidth);
topPatchesWidget.show();
QObject::connect(&nearestNeighborsDisplayVisitor, SIGNAL(signal_Refresh(const std::vector<Node>&)), &topPatchesWidget, SLOT(SetNodes(const std::vector<Node>&)));
QtConcurrent::run(boost::bind(InpaintingAlgorithm<VertexListGraphType, CompositeVisitorType,
BoundaryNodeQueueType, TwoStepSearchType, InpainterType>,
graph, compositeVisitor, &boundaryNodeQueue,
twoStepSearch, &patchInpainter));
return app.exec();
}