-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinpaint.m
239 lines (198 loc) · 9.09 KB
/
inpaint.m
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
function [inpaintedImg,origImg,fillImg,C,D,fillMovie] = inpaint(imgFilename,depthFilename,fillFilename,fillColor,scaleFactor)
%INPAINT Exemplar-based inpainting.
%
% Usage: [inpaintedImg,origImg,fillImg,C,D,fillMovie] ...
% = inpaint(imgFilename,fillFilename,fillColor)
% Inputs:
% imgFilename Filename of the original image.
% fillFilename Filename of the image specifying the fill region.
% fillColor 1x3 RGB vector specifying the color used to specify
% the fill region.
% Outputs:
% inpaintedImg The inpainted image; an MxNx3 matrix of doubles.
% origImg The original image; an MxNx3 matrix of doubles.
% fillImg The fill region image; an MxNx3 matrix of doubles.
% C MxN matrix of confidence values accumulated over all iterations.
% D MxN matrix of data term values accumulated over all iterations.
% fillMovie A Matlab movie struct depicting the fill region over time.
%
% Example:
% [i1,i2,i3,c,d,mov] = inpaint('img.png','depth.png','mask.png',[0 255 0],scale);
% plotall; % quick and dirty plotting script
% close; movie(mov); % grab some popcorn
%
% author: Sooraj Bhat
% Modified by Marcel Davey & John Gu on
% 11/30/05 to run on Matlab 7.0.4.365 (R14) Service Pack 2
warning off MATLAB:divideByZero
% Load from files
[img_hd,depth_hd,fillImg_hd,fillRegion_hd] = loadimgs(imgFilename,depthFilename,fillFilename,fillColor,scaleFactor);
img = imresize(img_hd, scaleFactor);
depth = imresize(depth_hd, scaleFactor);
fillImg = imresize(fillImg_hd, scaleFactor);
fillRegion = imresize(fillRegion_hd, scaleFactor);
img = double(img);
depth = double(depth);
origImg = img;
origDepth = depth;
origFillRegion = fillRegion;
% Depth gradient map
[Dx Dy] = gradient(depth);
depth_gradient = cat(3, Dx, Dy);
origGradient = depth_gradient;
ind = img2ind(img);
ind_hd = img2ind(img_hd); % High resolution index map
sz = [size(img,1) size(img,2)]; % Size.
sourceRegion = ~fillRegion;
% Initialize isophote values
[Ix(:,:,3) Iy(:,:,3)] = gradient(img(:,:,3));
[Ix(:,:,2) Iy(:,:,2)] = gradient(img(:,:,2));
[Ix(:,:,1) Iy(:,:,1)] = gradient(img(:,:,1));
Ix = sum(Ix,3)/(3*255); Iy = sum(Iy,3)/(3*255);
temp = Ix; Ix = -Iy; Iy = temp; % Rotate gradient 90 degrees to get isophote
IDx = -Dy; IDy = Dx; % Isophote of depth
% Initialize confidence and data terms
C = double(sourceRegion);
D = repmat(-.1,sz);
iter = 1;
% Visualization stuff
if nargout==6
fillMovie(1).cdata=uint8(img);
fillMovie(1).colormap=[];
origImg(1,1,:) = fillColor;
iter = 2;
end
% Seed 'rand' for reproducible results (good for testing)
rand('state',0);
% Parameter for priority: lambda * RGB + (1 - lambda) * depth
lambda = 0.8;
% Loop until entire fill region has been covered
while any(fillRegion(:))
% Find contour & normalized gradients of fill region
fillRegionD = double(fillRegion); % Marcel 11/30/05
dR = find(conv2(fillRegionD,[1,1,1;1,-8,1;1,1,1],'same')>0); % Marcel 11/30/05
%dR = find(conv2(fillRegion,[1,1,1;1,-8,1;1,1,1],'same')>0); % Original
[Nx,Ny] = gradient(double(~fillRegion)); % Marcel 11/30/05
%[Nx,Ny] = gradient(~fillRegion); % Original
N = [Nx(dR(:)) Ny(dR(:))];
N = normr(N);
N(~isfinite(N))=0; % handle NaN and Inf
% Compute confidences along the fill front
for k=dR'
Hp = getpatch(sz,k);
q = Hp(~(fillRegion(Hp))); % Get source region in the patch
C(k) = sum(C(q))/numel(Hp);
end
% Compute patch priorities = confidence term * data term
% D(dR) = abs(Ix(dR).*N(:,1)+Iy(dR).*N(:,2)) + 0.001; % Added a little constant to data term.
D(dR) = lambda * abs(Ix(dR).*N(:,1)+Iy(dR).*N(:,2)) + (1-lambda) * abs(IDx(dR).*N(:,1)+IDy(dR).*N(:,2)) + 0.001; % Modified data term for depth.
priorities = C(dR).* D(dR);
% Find patch with maximum priority, Hp
[unused,ndx] = max(priorities(:));
p = dR(ndx(1));
[Hp,rows,cols] = getpatch(sz,p);
toFill = fillRegion(Hp);
% Find exemplar that minimizes error, Hq
[Hq, rowsq, colsq] = bestexemplar(img,img(rows,cols,:),depth_gradient, depth_gradient(rows,cols,:),toFill',sourceRegion, rows(1), cols(1));
% Update fill region
toFill = logical(toFill); % Marcel 11/30/05
fillRegion(Hp(toFill)) = false;
% Propagate confidence & isophote values
C(Hp(toFill)) = C(p);
Ix(Hp(toFill)) = Ix(Hq(toFill));
Iy(Hp(toFill)) = Iy(Hq(toFill));
% Copy image data from Hq to Hp
ind(Hp(toFill)) = ind(Hq(toFill));
img(rows,cols,:) = ind2img(ind(rows,cols),origImg);
depth(rows,cols) = origDepth(ind(rows,cols));
depth_gradient(rows,cols,:) = ind2img_n(ind(rows,cols),origGradient,2);
% Transfer high resolution RGB patch simultaniously
rows_hd = floor(rows(1)/scaleFactor)-1:floor(rows(1)/scaleFactor)+floor((length(rows)-1)/scaleFactor);
cols_hd = floor(cols(1)/scaleFactor)-1:floor(cols(1)/scaleFactor)+floor((length(cols)-1)/scaleFactor);
rowsq_hd = floor(rowsq(1)/scaleFactor)-1:floor(rowsq(1)/scaleFactor)+floor((length(rowsq)-1)/scaleFactor);
colsq_hd = floor(colsq(1)/scaleFactor)-1:floor(colsq(1)/scaleFactor)+floor((length(colsq)-1)/scaleFactor);
ind_hd(rows_hd, cols_hd) = ind_hd(rowsq_hd, colsq_hd);
% Visualization stuff
if nargout==6
ind2 = ind;
ind2(logical(fillRegion)) = 1; % Marcel 11/30/05
%ind2(fillRegion) = 1; % Original
fillMovie(iter).cdata=uint8(ind2img(ind2,origImg));
fillMovie(iter).colormap=[];
end
iter = iter+1;
end
inpaintedImg=img;
% Depth Reconstruction and high resolution interpolation.
reconstructedDepth = reconstruct(depth, origFillRegion, depth_gradient(:,:,1), depth_gradient(:,:,2));
reconstructedDepthScaled = imresize(reconstructedDepth, 'OutputSize', size(depth_hd));
fillDepth = depth_hd;
fillDepth(fillRegion_hd) = reconstructedDepthScaled(fillRegion_hd);
% Output depth
imwrite(uint8(fillDepth), strcat('inpainted_', depthFilename));
% High resolution RGB
inpaintedImgHD=ind2img(ind_hd, img_hd);
% Output image
imwrite(uint8(inpaintedImgHD), strcat('inpainted_', imgFilename));
%---------------------------------------------------------------------
% Scans over the entire image (with a sliding window)
% for the exemplar with the lowest error. Calls a MEX function.
%---------------------------------------------------------------------
function [Hq rows cols] = bestexemplar(img,Ip,depth,Dp,toFill,sourceRegion,center_y,center_x)
m=size(Ip,1); mm=size(img,1); n=size(Ip,2); nn=size(img,2);
best = bestexemplarhelper(mm,nn,m,n,img,Ip,depth,Dp,toFill,sourceRegion,center_y, center_x);
Hq = sub2ndx(best(1):best(2),(best(3):best(4))',mm);
rows = best(1):best(2);
cols = best(3):best(4);
%---------------------------------------------------------------------
% Returns the indices for a 9x9 patch centered at pixel p.
%---------------------------------------------------------------------
function [Hp,rows,cols] = getpatch(sz,p)
% [x,y] = ind2sub(sz,p); % 2*w+1 == the patch size
w=4; p=p-1; y=floor(p/sz(1))+1; p=rem(p,sz(1)); x=floor(p)+1;
rows = max(x-w,1):min(x+w,sz(1));
cols = (max(y-w,1):min(y+w,sz(2)))';
Hp = sub2ndx(rows,cols,sz(1));
%---------------------------------------------------------------------
% Converts the (rows,cols) subscript-style indices to Matlab index-style
% indices. Unforunately, 'sub2ind' cannot be used for this.
%---------------------------------------------------------------------
function N = sub2ndx(rows,cols,nTotalRows)
X = rows(ones(length(cols),1),:);
Y = cols(:,ones(1,length(rows)));
N = X+(Y-1)*nTotalRows;
%---------------------------------------------------------------------
% Converts an indexed image into an RGB image, using 'img' as a colormap
%---------------------------------------------------------------------
function img2 = ind2img(ind,img)
for i=3:-1:1, temp=img(:,:,i); img2(:,:,i)=temp(ind); end;
function img2 = ind2img_n(ind,img,channel)
for i=channel:-1:1, temp=img(:,:,i); img2(:,:,i)=temp(ind); end;
%---------------------------------------------------------------------
% Converts an RGB image into a indexed image, using the image itself as
% the colormap.
%---------------------------------------------------------------------
function ind = img2ind(img)
s=size(img); ind=reshape(1:s(1)*s(2),s(1),s(2));
%---------------------------------------------------------------------
% Loads the an image and it's fill region, using 'fillColor' as a marker
% value for knowing which pixels are to be filled.
%---------------------------------------------------------------------
function [img,depth,fillImg,fillRegion] = loadimgs(imgFilename,depthFilename,fillFilename,fillColor,scaleFactor)
img = imread(imgFilename); fillImg = imread(fillFilename);
depth = imread(depthFilename);
% NYU Depth crop
% img = img(7:474, 8:632, :);
% fillImg = fillImg(7:474, 8:632, :);
% depth = depth(7:474, 8:632);
if(size(fillImg,3)>1)
fillRegion = fillImg(:,:,1)==fillColor(1) & fillImg(:,:,2)==fillColor(2) & fillImg(:,:,3)==fillColor(3);
else
fillRegion = fillImg;
end
% fillRegion = fillImg(:,:,1)==fillColor(1) & ...
% fillImg(:,:,2)==fillColor(2) & fillImg(:,:,3)==fillColor(3);
function [A] = normr(N)
for ii=1:size(N,1)
A(ii,:) = N(ii,:)/norm(N(ii,:));
end