Function iarectangle

Synopse

Create a binary rectangle image.

  • g = iarectangle(s, r, c)
    • g: Image.
    • s: Image. [rows cols], output image dimensions.
    • r: Double. [rrows ccols], rectangle image dimensions.
    • c: Image. [row0 col0], center of the rectangle.
01. from numpy import *
02. 
03. def iarectangle(s, r, c):
04. 
05.     rows,  cols  = s[0], s[1]
06.     rrows, rcols = r[0], r[1]
07.     rr0,   cc0   = c[0], c[1]
08.     rr, cc = meshgrid(range(rows), range(cols), indexing='ij')
09. 
10.     min_row, max_row = rr0-rrows/2.0, rr0+rrows/2.0
11.     min_col, max_col = cc0-rcols/2.0, cc0+rcols/2.0
12. 
13.     g1 = (min_row <= rr) & (max_row > rr)
14.     g2 = (min_col <= cc) & (max_col > cc)
15. 
16.     g = g1 & g2
17.     return g

Description

Creates a binary image with dimensions given by s, rectangle dimensions given by r and center given by c. The pixels inside the rectangle are one and outside zero.

Examples

Example 1

1. from ia636 import iarectangle
2. 
3. F = iarectangle([7,9], [3,2], [3,4])
4. print F.astype(int)
[[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 1 1 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]]

Example 2

In the following example, an image with 200 lines and 300 columns is created with a rectangle of height 90 and width 120 pixels placed with its center at coordinates (h,w)=(70,120):

1. F = iarectangle([200,300], [90,120], [70,120])
2. adshow(F)

Equation

[–] Comments
Roberto Lotufo at 2015-03-12 05:45h
Likes:
Artur Bernardo Mallmann
Miria Bernardino
 2
Olá Miria, sim aqui não existe nenhum fatiamento. São todas operações matriciais normais. Veja que são todas operações booleanas: <=, &.

Miria Bernardino at 2015-03-11 23:04h
 0
Na linha 13. g1 = (min_row <= rr) & (max_row > rr), g1 é cópia profunda de rr? E na linha 16. g = g1 & g2, g é cópia profunda de g1 e g2?