denote corrupted pixels with $K \in \{0,1\}^{512 \times 512}$
$X_\mathrm{corr} \in \mathbf{R}^{512 \times 512 \times 3}$ is corrupted image
In [1]:
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
image_file = cbook.get_sample_data('lena.png')
X_orig = plt.imread(image_file)
plt.imshow(X_orig)
Out[1]:
In [2]:
from PIL import Image, ImageDraw, ImageFont
import itertools, textwrap
from itertools import cycle, islice
def drawText(image, fontsize, length):
text = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, \
sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna \
aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud \
exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea \
commodo consequat. Duis autem vel eum iriure dolor in hendrerit in \
vulputate velit esse molestie consequat, vel illum dolore eu feugiat \
nulla facilisis at vero eros et accumsan et iusto odio dignissim qui \
blandit praesent luptatum zzril delenit augue duis dolore te feugait \
nulla facilisi. Nam liber tempor cum soluta nobis eleifend option \
congue nihil imperdiet doming id quod mazim placerat facer possim'
imshape = (image.shape[0], image.shape[1])
dmg = Image.new("L",imshape)
draw = ImageDraw.Draw(dmg)
#fontsize = 80
font = ImageFont.truetype("/Library/Fonts/Georgia.ttf", fontsize)
text = "".join(islice(cycle(text),length))
lines = textwrap.wrap(text, width = 40)
w, h = dmg.size
y_text = 0
for line in lines:
width, height = font.getsize(line)
draw.text((0, y_text), line, font = font, fill = 255)
y_text += height
return (np.array(dmg) == 0).astype(int)
In [3]:
K = drawText(X_orig,40,600)
rows, cols, colors = X_orig.shape
X_corr = np.ones((rows,cols,colors))
X_corr[K==1,:] = X_orig[K==1,:]
plt.imshow(X_corr)
Out[3]:
In [4]:
%%time
from cvxpy import *
variables = []
constr = []
for i in range(colors):
X = Variable(rows, cols)
variables += [X]
constr += [multiply(K, X - X_corr[:, :, i]) == 0]
prob = Problem(Minimize(tv(*variables)), constr)
prob.solve(verbose=True, solver=SCS)
In [7]:
# the recovered image
X_rec = np.zeros((rows,cols,3))
for i,x in enumerate(variables):
X_rec[:,:,i] = x.value
In [8]:
def showImgs(im1,im2,labels,filename=None):
fig, ax = plt.subplots(1, 2, sharex=True, sharey=True, figsize=(10, 5))
matplotlib.rcParams.update({'font.size': 14})
fig.tight_layout()
ax[0].imshow(im1)
ax[1].imshow(im2)
ax[0].axis('off')
ax[1].axis('off')
ax[0].set_title(labels[0])
ax[1].set_title(labels[1])
if filename:
fig.savefig(filename, bbox_inches='tight')
In [9]:
showImgs(X_orig,X_corr,['Original','Corrupted'],filename='inpaint_text1.pdf')
In [10]:
showImgs(X_orig,X_rec,['Original','Recovered'],filename='inpaint_text2.pdf')
In [11]:
showImgs(X_corr,X_rec,['Corrupted','Recovered'],filename='inpaint_text_cr.pdf')
In [10]:
rows, cols, colors = X_orig.shape
K = (np.random.rand(rows,cols) < .2).astype(int)
X_corr = np.ones((rows,cols,colors))
X_corr[K==1,:] = X_orig[K==1,:]
plt.imshow(X_corr)
Out[10]:
In [11]:
%%time
from cvxpy import *
variables = []
constr = []
for i in range(colors):
X = Variable(rows, cols)
variables += [X]
constr += [multiply(K, X - X_corr[:, :, i]) == 0]
prob = Problem(Minimize(tv(*variables)), constr)
prob.solve(verbose=True, solver=SCS)
In [12]:
# the recovered image
X_rec = np.zeros((rows,cols,3))
for i,var in enumerate(variables):
X_rec[:,:,i] = var.value
In [13]:
showImgs(X_orig,X_corr,['Original','Corrupted'],filename='inpaint80_1.pdf')
In [14]:
showImgs(X_orig,X_rec,['Original','Recovered'],filename='inpaint80_2.pdf')
In [ ]: