Create a binary ellipse image.
g = ellipse(s, r, c, theta)
In [6]:
import numpy as np
def ellipse(s, r, c, theta=0):
rows, cols = s[0], s[1]
rr0, cc0 = c[0], c[1]
rr, cc = np.meshgrid(range(rows), range(cols), indexing='ij')
rr = rr - rr0
cc = cc - cc0
cos = np.cos(theta)
sen = np.sin(theta)
i = cos/r[1]
j = sen/r[0]
m = -sen/r[1]
n = cos/r[0]
g = ((i*cc + m*rr)**2 + (j*cc + n*rr)**2) <= 1
return g
In [1]:
testing = (__name__ == "__main__")
if testing:
! jupyter nbconvert --to python ellipse.ipynb
import numpy as np
import sys,os
import matplotlib.image as mpimg
ia898path = os.path.abspath('../../')
if ia898path not in sys.path:
sys.path.append(ia898path)
import ia898.src as ia
In [3]:
if testing:
g = ia.ellipse([16,16], [2,4], [8,8], np.pi * 0.25)
print('g:\n', g.astype(int))
In [4]:
if testing:
from time import time
t = time()
g = ia.ellipse([300,300], [90,140], [150,150], np.pi * 0.25)
tend = time()
print('Computational time (10k, 10k) is {0:.2f} seconds.'.format(tend - t))
ia.adshow(g, "Ellipse")
In [6]:
if testing:
print('Computational time (10k, 10k) is:')
%timeit ia.ellipse([300,300], [90,140], [150,150], np.pi * 0.25)
In [ ]: