In [9]:
%load_ext autoreload
%autoreload 2
import matplotlib.pyplot as plt
import scipy.ndimage.filters as filters;
%matplotlib inline

from itertools import product
from PIL import Image, ImageFilter
import numpy.fft
import numpy as np
import sys
sys.path.append("..")
from goscore import image_to_numpy_contour, transform, draw_line


The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload

In [42]:
im = Image.open("../images/perspective/clean_20degrees.jpg")
arr = image_to_numpy_contour(im, 7)
arr2 = np.zeros((600, 400))
transform(arr, arr2)


Out[42]:
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.]])

In [41]:
im


Out[41]:

In [504]:
arr2=arr.copy()
draw_line(arr2, (600, 400), 300, 40)
draw_line(arr2, (600, 400), 296, 75)
draw_line(arr2, (600, 400), 296, 79)
arr2 = arr2.astype(np.uint8)
Image.fromarray(arr2)


Out[504]:

In [37]:
larr = find_large_points(arr)
larr = 255.*larr/larr.max()

In [40]:
Image.fromarray(arr.astype(np.uint8))


Out[40]:

In [671]:
x = transform(arr, (600, 400))
y = (x*255 / x.max()).astype(np.uint8)


61344000

In [40]:
arr.dtype


Out[40]:
dtype('float64')

In [ ]:


In [43]:
y = (arr2*255 / arr2.max()).astype(np.uint8)
Image.fromarray(y)


Out[43]:

Image.fromarray(y_b)


In [148]:
y = (arr2*255 / arr2.max()).astype(np.uint8)
Image.fromarray(y)


Out[148]:

In [23]:
def find_large_points(inp):
    out = filters.gaussian_filter(inp, 0.2)  - filters.gaussian_filter(inp, 2)
    for (i,j) in product(range(out.shape[0]), range(out.shape[1])):
        out[i,j] = max(out[i,j], 0)
    out /= out.max()
    return out

In [44]:
a = find_large_points(arr2)
b = (a*255).astype(np.uint8)
Image.fromarray(b)


Out[44]:

In [154]:
arr3=np.zeros((600,400))
transform_c(a*255, arr3)
arr3_norm = (255*arr3)/arr3.max()
Image.fromarray(arr3_norm.astype(np.uint8))


Out[154]:

In [155]:
z = find_large_points(arr3)
b = (z*255).astype(np.uint8)
Image.fromarray(b)


Out[155]:

In [ ]:


In [46]:
offsets = b.argsort(axis=None)[::-1][:70]
q=[ (o // b.shape[1], o % b.shape[1]) for o in offsets]
arr2=arr.copy()
print(q)
for (p,l) in q:
        draw_line(arr2, (600, 400), p,l)
arr2 = arr2.astype(np.uint8)
Image.fromarray(arr2)


[(452, 116), (152, 283), (151, 294), (451, 105), (151, 284), (451, 115), (152, 274), (452, 125), (452, 132), (152, 267), (152, 261), (452, 138), (151, 275), (451, 124), (150, 295), (450, 104), (452, 106), (152, 293), (452, 144), (152, 255), (453, 117), (153, 282), (234, 133), (534, 266), (453, 126), (233, 134), (153, 273), (533, 265), (453, 133), (153, 266), (153, 250), (453, 149), (453, 139), (153, 260), (353, 140), (53, 259), (40, 265), (340, 134), (64, 253), (352, 140), (364, 146), (52, 259), (26, 271), (242, 131), (542, 268), (326, 128), (243, 130), (543, 269), (554, 273), (254, 126), (39, 265), (339, 134), (453, 157), (153, 242), (553, 272), (253, 127), (555, 274), (311, 124), (266, 124), (11, 275), (566, 275), (255, 125), (27, 271), (327, 128), (567, 276), (450, 115), (267, 123), (150, 284), (25, 270), (324, 129)]
Out[46]:

In [711]:
def good(lst, val, period):
    for t in lst:
        q = (val - t + 5*period)% period
        if q < (period // 10) or (q > period - (period//10)):
            return False
    return True

In [737]:
def good2(lst, val):
    (x_v, y_v) = val
    for (x, y) in lst:
        if (x-x_v)*(x-x_v) + (y-y_v)*(y-y_v) <= 7:
            return False
    return True

In [747]:
def get_band(ba, start, stop):
    offsets = ba.argsort(axis=None)[::-1]
    q=[ (o // ba.shape[1], o % ba.shape[1]) for o in offsets]
    l=[]
    for (u, v) in q:
        if start <= u <= stop and good2(l, (u,v)):
            l.append((u,v))
    return l

In [755]:
def draw_band(ba, start, stop, img):
    s = get_band(ba, start, stop)[:20]
    for (p,l) in s:
        draw_line(img, (600, 400), p,l)

In [757]:
xx = np.zeros(b.shape[0])
for i in range(b.shape[0]):
    xx[i] = b[i].sum()

plt.plot(range(len(xx)), xx)
period = 300
dd = xx.argsort()[::-1]
l = []
for i in range(2):
    for j in range(len(dd)):
        if good(l, dd[j], period) and (period//10 < dd[j] < 2*period - (period//10)):
            l.append(dd[j])
            break
arr2 = arr.copy()        
for i in l:
    draw_band(b, i-30, i+30, arr2)
Image.fromarray(arr2.astype(np.uint8))


Out[757]:

In [139]:
plt.matshow(y)


Out[139]:
<matplotlib.image.AxesImage at 0x1104a64a8>

In [106]:
y.max


Out[106]:
<function ndarray.max>

In [107]:
y.max()


Out[107]:
7632739.0

In [137]:
z=np.zeros((100, 100))
z[30,30] = 255
z[30, 43] = 255
t = transform(z, 100, 100)


1000000

In [136]:
%%cython --cplus

cimport numpy as cnp
cimport cython

@cython.overflowcheck(False)
@cython.boundscheck(False)plt.matshow(t)


Out[136]:
<matplotlib.image.AxesImage at 0x1139c7320>

In [151]:
arr2 = np.zeros((600, 400))
transform_c(arr, arr2)


Out[151]:
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.]])

In [53]:
%%cython --cplus

cimport numpy as cnp
cimport cython
from libcpp.vector cimport vector

@cython.overflowcheck(False)
@cython.boundscheck(False)



def siema(float a, float b):
    return int(a) + <int>(b)

In [134]:
plt.matshow(arr2)


Out[134]:
<matplotlib.image.AxesImage at 0x115883208>

In [106]:
y = (arr2*255 / arr2.max()).astype(np.uint8)
Image.fromarray(y)


Out[106]:

In [37]:
x[:,np.newaxis,np.newaxis].repeat(3, axis=1).repeat(4, axis=2)


Out[37]:
array([[[0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]],

       [[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]]])

In [56]:
t=np.ones(12).reshape(3,4)

In [57]:
t


Out[57]:
array([[ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.]])

In [60]:
t[1, np.array([2,2,3,1])]  += [3,3,3,3]

In [61]:
t


Out[61]:
array([[ 1.,  1.,  1.,  1.],
       [ 1.,  4.,  4.,  4.],
       [ 1.,  1.,  1.,  1.]])

In [50]:
np.array([2,2,3,1])


Out[50]:
array([2, 2, 3, 1])

In [1]:
import numpy as np

In [2]:
%load_ext cython

In [23]:
def array_sum_py(x):
    res = 0
    for n1 in x:
        for n2 in x:
            for n3 in x:
                res += n1 + n2 + n3
                res %= 100000
    return res

In [27]:
%%cython --cplus

cimport numpy as cnp
cimport cython

@cython.overflowcheck(False)
@cython.boundscheck(False)
def array_sum(cnp.ndarray x):
    cdef int res = 0
    cdef int n1
    cdef int n2
    cdef int n3
    cdef int size = x.shape[0]
    cdef int [:] buff = x
    for n1 in range(size):
        for n2 in range(size):
            for n3 in range(size):
                res += buff[n1] + buff[n2] + buff[n3]
                res %= 100000
    return res % 100000

In [28]:
import cProfile

cProfile.run("array_sum(example)")


         4 function calls in 0.755 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.755    0.755 <string>:1(<module>)
        1    0.755    0.755    0.755    0.755 {_cython_magic_5c5d3620f9b6431a06c79d42cffed839.array_sum}
        1    0.000    0.000    0.755    0.755 {built-in method exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}



In [24]:
cProfile.run("array_sum_py(example)")


         4 function calls in 41.883 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1   41.883   41.883   41.883   41.883 <ipython-input-23-7035b73b9459>:1(array_sum_py)
        1    0.000    0.000   41.883   41.883 <string>:1(<module>)
        1    0.000    0.000   41.883   41.883 {built-in method exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}



In [ ]: