Function iasecrop

Synopse

Extract the minimum centered non-background rectangle with odd dimensions.

  • g = iasecrop(f)
    • Output
      • g: ndarray, minimum rectangle with odd-size dimensions not contained background pixels
    • Input
      • f: ndarray, 2D.

Description

Adopting the convention that the origin (0,0) of the structuring element is at the center of the matrix, extract the minimum centered odd-size rectangle containing all non background pixels.

Function Code

01. def iasecrop(f):
02.     import numpy as np
03.     from ia870 import ialimits, iagray
04.     k1,k2 = ialimits(f)
05.     if not f.shape[0] & 1: f = np.vstack([k1 * np.ones((1,f.shape[1]),f.dtype),f])
06.     if not f.shape[1] & 1: f = np.hstack([k1 * np.ones((f.shape[0],1),f.dtype),f])
07.     h,w = f.shape
08.     h1 = (f != k1).max(1).nonzero()[0]
09.     dh = min([h1[0],h-h1[-1]-1])
10.     fh = f[dh:h-dh,:]
11.     w1 = (fh != k1).max(0).nonzero()[0]
12.     dw = min([w1[0],w-w1[-1]-1])
13.     return fh[:,dw:w-dw]

Examples

Numerical binary example:

01. f = array( [[0,1,1,0,0,0],
02.             [1,0,0,0,0,0],
03.             [1,0,0,1,0,0],
04.             [0,0,0,0,0,0]], 'bool')
05. 
06. from iasecrop import iasecrop
07. from ia870 import iaseshow, iabshow, iaunion
08. 
09. g = iasecrop(f)
10. print 'f:\n',f
11. print 'g:\n',g
12. adshow(iabshow(iaunion(f,True),f),'input')
13. adshow(iaseshow(g,'EXPAND'),'centered crop')
f:
[[False  True  True False False False]
 [ True False False False False False]
 [ True False False  True False False]
 [False False False False False False]]
g:
[[False  True  True False False]
 [ True False False False False]
 [ True False False  True False]]

input

centered crop

Numerical grayscale int32 example:

01. f = array( [[0,1,1,0,0,0],
02.             [1,0,0,0,0,0],
03.             [1,0,0,1,0,0],
04.             [0,0,0,0,0,0]], 'bool')
05. 
06. from iasecrop import iasecrop
07. from ia870 import iaseshow, iabshow, iagray, iamaxleveltype
08. from ia636 import ianshow
09. 
10. f1 = iagray(f,'int32',0)
11. g1 = iasecrop(f1)
12. print 'f1:\n',f1
13. print 'g1:\n',g1
14. #adshow(ianshow(f1),'input')
15. #adshow(iaseshow(g1,'EXPAND'),'centered crop')
f1:
[[-2147483647           0           0 -2147483647 -2147483647 -2147483647]
 [          0 -2147483647 -2147483647 -2147483647 -2147483647 -2147483647]
 [          0 -2147483647 -2147483647           0 -2147483647 -2147483647]
 [-2147483647 -2147483647 -2147483647 -2147483647 -2147483647 -2147483647]]
g1:
[[-2147483647           0           0 -2147483647 -2147483647]
 [          0 -2147483647 -2147483647 -2147483647 -2147483647]
 [          0 -2147483647 -2147483647           0 -2147483647]]

Numerical grayscale float64 example:

01. f = array( [[0,1,1,0,0,0],
02.             [1,0,0,0,0,0],
03.             [1,0,0,1,0,0],
04.             [0,0,0,0,0,0]], 'bool')
05. 
06. from iasecrop import iasecrop
07. from ia870 import iaseshow, iabshow, iagray, iamaxleveltype
08. from ia636 import ianshow
09. 
10. f1 = iagray(f,'float64',iamaxleveltype('float64'))
11. g1 = iasecrop(f1)
12. print 'f1:\n',f1
13. print 'g1:\n',g1
14. #adshow(ianshow(f1),'input')
15. #adshow(iaseshow(g1,'EXPAND'),'centered crop')
f1:
[[-inf  inf  inf -inf -inf -inf]
 [ inf -inf -inf -inf -inf -inf]
 [ inf -inf -inf  inf -inf -inf]
 [-inf -inf -inf -inf -inf -inf]]
g1:
[[-inf  inf  inf -inf -inf]
 [ inf -inf -inf -inf -inf]
 [ inf -inf -inf  inf -inf]]

See Also