Function iamat2set

Synopse

Converts image representation from matrix to set

  • CV = iamat2set(A)
    • CV: Image
    • A: Image Image in matrix format, where the origin (0,0) is at the center of the matrix.
01. from numpy import *
02. 
03. def iamat2set(A):
04.     from ialimits import ialimits
05. 
06. 
07.     if len(A.shape) == 1: A = A[newaxis,:]
08.     offsets = nonzero(ravel(A) - ialimits(A)[0])
09.     if type(offsets) == type(()):
10.         offsets = offsets[0]        # for compatibility with numarray
11.     if len(offsets) == 0: return ([],[])
12.     (h,w) = A.shape
13.     x = range(2)
14.     x[0] = offsets/w - (h-1)/2
15.     x[1] = offsets%w - (w-1)/2
16.     x = transpose(x)
17.     CV = x,ravel(A)[offsets]
18.     return CV

Description

Return tuple with array of pixel coordinates and array of corresponding pixel values. The input image is in the matrix format, like the structuring element, where the origin (0,0) is at the center of the matrix.

Examples

Example 1

01. from ia870 import iamat2set
02. 
03. f=array([[1,2,3],
04.          [4,5,6],
05.          [7,8,9]])
06. i,v=iamat2set(f)
07. print 'f=\n',f
08. print 'f.dtype=',f.dtype
09. print 'coordinates=\n',i
10. print 'v=\n',v
11. print 'v.dtype=',v.dtype
f=
[[1 2 3]
 [4 5 6]
 [7 8 9]]
f.dtype= int64
coordinates=
[[-1 -1]
 [-1  0]
 [-1  1]
 [ 0 -1]
 [ 0  0]
 [ 0  1]
 [ 1 -1]
 [ 1  0]
 [ 1  1]]
v=
[1 2 3 4 5 6 7 8 9]
v.dtype= int64

Example 1.1

01. from ia870 import iamat2set
02. 
03. f=array([[1,2,3],
04.          [4,5,6],
05.          [7,8,9]],'int32')
06. i,v=iamat2set(f)
07. print 'f=\n',f
08. print 'f.dtype=',f.dtype
09. print 'coordinates=\n',i
10. print 'v=\n',v
11. print 'v.dtype=',v.dtype
f=
[[1 2 3]
 [4 5 6]
 [7 8 9]]
f.dtype= int32
coordinates=
[[-1 -1]
 [-1  0]
 [-1  1]
 [ 0 -1]
 [ 0  0]
 [ 0  1]
 [ 1 -1]
 [ 1  0]
 [ 1  1]]
v=
[1 2 3 4 5 6 7 8 9]
v.dtype= int32

When image size is even, the origin is at the nearest top-left neighbor pixel of the center of the matrix

Example 2 - Even dimension

1. f=array([[1,2,3,4],
2.          [5,6,7,8]],'uint8')
3. i,v=iamat2set(f)
4. print 'coordinates',i
5. print 'values',v
coordinates [[ 0 -1]
 [ 0  0]
 [ 0  1]
 [ 0  2]
 [ 1 -1]
 [ 1  0]
 [ 1  1]
 [ 1  2]]
values [1 2 3 4 5 6 7 8]

Example 3 - Binary case

1. f=array([[0,0,1,1],
2.          [0,1,0,1],
3.          [0,0,0,1]],'bool')
4. i,v=iamat2set(f)
5. print 'coordinates',i
6. print 'values',v
coordinates [[-1  1]
 [-1  2]
 [ 0  0]
 [ 0  2]
 [ 1  2]]
values [ True  True  True  True  True]

See Also: