Function iaellipse

Synopse

Create a binary ellipse image.

  • g = iaellipse(s, r, c, theta)
    • Output
      • g: Image.
    • Input
      • s: Image. [rows cols], output image dimensions.
      • r: Double. [rRows rCols], radius for y and x directions.
      • c: Image. [row0 col0], center of the circle.
      • theta: Double. Angle rotation in radians. (optional)

Description

The iaellipse function creates a binary image with dimensions given by s, radius given by r (r[0] = rRows; r[1] = rCols) and center given by c. The pixels inside the circle are one and outside zero.

Function Code

01. from numpy import *
02. import math
03. 
04. def iaellipse(s, r, c, theta = 0):
05.     rows, cols = s[0], s[1]
06.     rr0,  cc0  = c[0], c[1]
07.     rr, cc = meshgrid(range(rows), range(cols), indexing='ij')
08.     rr = rr - rr0
09.     cc = cc - cc0
10.     cos = math.cos(theta)
11.     sen = math.sin(theta)
12.     i = cos/r[1]
13.     j = sen/r[0]
14.     m = -sen/r[1]
15.     n = cos/r[0]
16.     g = ((i*cc + m*rr)**2 + (j*cc + n*rr)**2) <= 1
17.     return g

Examples

Numerical example:

1. from ia636 import iaellipse
2. 
3. g = iaellipse([16,16], [2,4], [8,8], math.pi * 0.25)
4. print 'g:\n', g.astype(int)
g:
[[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 1 1 0 0 0 0 0]
 [0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0]
 [0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0]
 [0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0]
 [0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0]
 [0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0]
 [0 0 0 0 0 0 1 1 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]]

Measuring time:

1. from time import time
2. t = time()
3. g = iaellipse([300,300], [90,140], [150,150], math.pi * 0.25)
4. tend = time()
5. print 'Computational time (10k,10k) is {0:.2f} seconds.'.format(tend - t)
6. adshow(g, "Ellipse")
Computational time (10k,10k) is 0.02 seconds.

Ellipse

Equation

Contributions

  • Rafael Berri, 23sep2013: initial function.