In [2]:
import algorithm as al
import numpy as np
import cv2 as cv
import Queue
from patchdb import *
from psi import *
from copyutils import *
from debug import *
import inspect


[INFO              ] [Logger      ] Record log in C:\Users\xiaoy\.kivy\logs\kivy_16-04-08_61.txt
[INFO              ] [Kivy        ] v1.9.1
[INFO              ] [Python      ] v2.7.11 |Continuum Analytics, Inc.| (default, Feb 16 2016, 09:58:36) [MSC v.1500 64 bit (AMD64)]
[INFO              ] [Image       ] Providers: img_tex, img_dds, img_gif, img_sdl2 (img_pil, img_ffpyplayer ignored)

In [3]:
def imshow(img):
    cv.imshow('image', img)
    cv.waitKey(0)
    cv.destroyAllWindows()

In [92]:
_p.readImage('../../test_images/Kanizsa-triangle-tiny.png', 'source')

_p.readImage('../../test_images/Kanizsa-triangle-mask-tiny.png', 'alpha')


Out[92]:
(True, 'Success')

In [93]:
_p._images['filled'] = np.uint8(_p._images['alpha'] > 0)*255
_p._images['inpainted'] = _p._images['source'].copy()

In [94]:
_p._images['source'].shape


Out[94]:
(341L, 320L, 3L)

In [95]:
for i in range(0,3):
    _p._images['inpainted'][:,:,i] *= (_p._images['filled']>0)

#
# Step 1a,b: Identify the fill front deltaOmega and compute
#            initial patch priorities

In [96]:
_p.computeBoundaries()
_p.confidenceInitialize()

In [97]:
_p._w = 5

In [98]:
_p._patchDB = PatchDB(_p._images['inpainted'], _p._w, filled=_p._images['filled'])

In [99]:
_p.iterationsInit()
done = False

In [100]:
boundary = _p._boundaryIterator.next()
_p.fillFrontInitialize(boundary, imviewer=None)

In [101]:
boundaryPixels = boundary
# initialize the pixels on the fill front
_p._images['fillFront'] = \
    np.zeros_like(_p._images['filled'], dtype=np.uint8)
_p._images['fillFront'] = \
    cv.drawContours(_p._images['fillFront'],boundaryPixels,-1,255)

# initialize the priority queue with all points on the fill front
_p._deltaOmega = Queue.PriorityQueue()

In [135]:
# col, row = boundaryPixels[10][0]
col, row = boundary[150:200][0][0]
p = PSI((row, col), _p._w, 
          image=_p._images['inpainted'], 
          filled=_p._images['filled'],
          confidence=_p._images['confidence'],
          fillFront=_p._images['fillFront'])
inpaintedImage=_p._images['inpainted'] 
filledImage=_p._images['filled']
confidence=_p._images['confidence']
fillFront=_p._images['fillFront']

In [151]:
front, _ = copyutils.getWindow(fillFront, p._coords, p._w)
# filled, _ = copyutils.getWindow(filledImage, psiHatP._coords, psiHatP._w)
# Sobel filter
kw = 1
size = 2 * kw + 1
Gx = cv.Sobel(src=front, ddepth=cv.CV_32F, dx=1, dy=0, ksize=size)[p._w][p._w]
Gy = cv.Sobel(src=front, ddepth=cv.CV_32F, dx=0, dy=1, ksize=size)[p._w][p._w]
d = np.sqrt(Gy**2 + Gx**2)
if d != 0:
    Gx /= d
    Gy /= d
Ny = Gy
Nx = -Gx

In [152]:
front


Out[152]:
array([[  0,   0, 255,   0,   0,   0,   0,   0,   0,   0,   0],
       [  0,   0,   0, 255,   0,   0,   0,   0,   0,   0,   0],
       [  0,   0,   0,   0, 255,   0,   0,   0,   0,   0,   0],
       [  0,   0,   0,   0, 255,   0,   0,   0,   0,   0,   0],
       [  0,   0,   0,   0,   0, 255,   0,   0,   0,   0,   0],
       [  0,   0,   0,   0,   0, 255,   0,   0,   0,   0,   0],
       [  0,   0,   0,   0,   0,   0, 255,   0,   0,   0,   0],
       [  0,   0,   0,   0,   0,   0, 255,   0,   0,   0,   0],
       [  0,   0,   0,   0,   0,   0,   0, 255,   0,   0,   0],
       [  0,   0,   0,   0,   0,   0,   0, 255,   0,   0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0, 255,   0,   0]], dtype=uint8)

In [155]:
Gx


Out[155]:
0.70710678118654746

In [106]:
erode_kernel = np.ones((3,3),np.uint8);kernel


Out[106]:
array([[1, 1, 1],
       [1, 1, 1],
       [1, 1, 1]], dtype=uint8)

In [107]:
filled_eroded = cv.erode(inpainted, erode_kernel, borderType=cv.BORDER_REPLICATE,iterations=1);filled_eroded


Out[107]:
array([[  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [  0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [255,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [255,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [255, 255,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [255, 255,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [255, 255, 255,   0,   0,   0,   0,   0,   0,   0,   0],
       [255, 255, 255,   0,   0,   0,   0,   0,   0,   0,   0],
       [255, 255, 255, 255,   0,   0,   0,   0,   0,   0,   0]], dtype=uint8)

In [108]:
dx = cv.Sobel(src=inpainted, ddepth=cv.CV_32F, dx=1, dy=0, ksize=3, borderType=cv.BORDER_REPLICATE);dx


Out[108]:
array([[    0.,     0.,     0.,     0.,     0.,     0.,     0.,     0.,
            0.,     0.,     0.],
       [ -255.,  -255.,     0.,     0.,     0.,     0.,     0.,     0.,
            0.,     0.,     0.],
       [ -510.,  -765.,  -255.,     0.,     0.,     0.,     0.,     0.,
            0.,     0.,     0.],
       [ -255., -1020.,  -765.,     0.,     0.,     0.,     0.,     0.,
            0.,     0.,     0.],
       [    0.,  -765., -1020.,  -255.,     0.,     0.,     0.,     0.,
            0.,     0.,     0.],
       [    0.,  -255., -1020.,  -765.,     0.,     0.,     0.,     0.,
            0.,     0.,     0.],
       [    0.,     0.,  -765., -1020.,  -255.,     0.,     0.,     0.,
            0.,     0.,     0.],
       [    0.,     0.,  -255., -1020.,  -765.,     0.,     0.,     0.,
            0.,     0.,     0.],
       [    0.,     0.,     0.,  -765., -1020.,  -255.,     0.,     0.,
            0.,     0.,     0.],
       [    0.,     0.,     0.,  -255., -1020.,  -765.,     0.,     0.,
            0.,     0.,     0.],
       [    0.,     0.,     0.,     0., -1020., -1020.,     0.,     0.,
            0.,     0.,     0.]], dtype=float32)

In [109]:
dx *= filled_eroded > 0

In [110]:
dx


Out[110]:
array([[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [-0., -0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [-0., -0., -0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [-0., -0., -0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0., -0., -0., -0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0., -0., -0., -0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0., -0., -0., -0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0., -0., -0., -0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0., -0., -0., -0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0., -0., -0., -0.,  0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0., -0., -0.,  0.,  0.,  0.,  0.,  0.]], dtype=float32)

In [111]:
Dx * (filled>0)


Out[111]:
array([[    0.,     0.,     0.,     0.,     0.,     0.,     0.,     0.,
            0.,     0.,     0.],
       [    0.,     0.,     0.,     0.,     0.,     0.,     0.,     0.,
            0.,     0.,     0.],
       [ 1020.,     0.,     0.,     0.,     0.,     0.,     0.,     0.,
            0.,     0.,     0.],
       [  510.,   510.,     0.,     0.,     0.,     0.,     0.,     0.,
            0.,     0.,     0.],
       [    0.,   255.,     0.,     0.,     0.,     0.,     0.,     0.,
            0.,     0.,     0.],
       [    0.,   255.,   510.,     0.,     0.,     0.,     0.,     0.,
            0.,     0.,     0.],
       [    0.,     0.,   255.,     0.,     0.,     0.,     0.,     0.,
            0.,     0.,     0.],
       [    0.,     0.,   255.,   510.,     0.,     0.,     0.,     0.,
            0.,     0.,     0.],
       [    0.,     0.,     0.,   255.,     0.,     0.,     0.,     0.,
            0.,     0.,     0.],
       [    0.,     0.,     0.,   255.,   510.,     0.,     0.,     0.,
            0.,     0.,     0.],
       [    0.,     0.,     0.,     0.,     0.,     0.,     0.,     0.,
            0.,     0.,     0.]], dtype=float32)

In [112]:
d


Out[112]:
array([[    0.        ,     0.        ,     0.        ,     0.        ,
            0.        ,     0.        ,     0.        ,     0.        ,
            0.        ,     0.        ,     0.        ],
       [  510.        ,   360.62445068,     0.        ,     0.        ,
            0.        ,     0.        ,     0.        ,     0.        ,
            0.        ,     0.        ,     0.        ],
       [ 1020.        ,  1081.87341309,   360.62445068,     0.        ,
            0.        ,     0.        ,     0.        ,     0.        ,
            0.        ,     0.        ,     0.        ],
       [  510.        ,  1140.39465332,   806.38079834,     0.        ,
            0.        ,     0.        ,     0.        ,     0.        ,
            0.        ,     0.        ,     0.        ],
       [    0.        ,   806.38079834,  1140.39465332,   360.62445068,
            0.        ,     0.        ,     0.        ,     0.        ,
            0.        ,     0.        ,     0.        ],
       [    0.        ,   360.62445068,  1140.39465332,   806.38079834,
            0.        ,     0.        ,     0.        ,     0.        ,
            0.        ,     0.        ,     0.        ],
       [    0.        ,     0.        ,   806.38079834,  1140.39465332,
          360.62445068,     0.        ,     0.        ,     0.        ,
            0.        ,     0.        ,     0.        ],
       [    0.        ,     0.        ,   360.62445068,  1140.39465332,
          806.38079834,     0.        ,     0.        ,     0.        ,
            0.        ,     0.        ,     0.        ],
       [    0.        ,     0.        ,     0.        ,   806.38079834,
         1140.39465332,   360.62445068,     0.        ,     0.        ,
            0.        ,     0.        ,     0.        ],
       [    0.        ,     0.        ,     0.        ,   360.62445068,
         1140.39465332,   806.38079834,     0.        ,     0.        ,
            0.        ,     0.        ,     0.        ],
       [    0.        ,     0.        ,     0.        ,     0.        ,
         1020.        ,  1020.        ,     0.        ,     0.        ,
            0.        ,     0.        ,     0.        ]], dtype=float32)

In [113]:
Dx[dmax][0]


Out[113]:
510.0

In [114]:
Dy[dmax][0]


Out[114]:
1020.0

In [ ]: