4 # Copyright (C) 2006-2007 Antonio Ingargiola <tritemio@gmail.com>
6 # This file is part of Image Analyzer.
8 # Image Analyzer is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
14 from numpy
import arange
, sign
, array
, sqrt
17 """Round the array passed to the nearest integer."""
18 return floor(array
+ 0.5).astype('int32')
22 Class representing an array section. See the __init__ documetation.
24 def __init__(self
, arr
, x1
, x2
, y1
, y2
, xPixDim
, yPixDim
, debug
=False):
27 arr: arrray to section
29 y1, y2: coordinates of the twop point indentifing the segment
30 along which to calculate the array values (the section).
31 This params are in array-index coordinates.
33 yPixDim: X and Y dimention of one pixel (element) of the array
34 debug: optionally enable debug prints
37 self.y: the array values along the section
38 self.x: the X axis for self.y calculated considering the effective
39 section length (considering pixel dimetion and the slope).
40 self.dim: real section length calculated based on pixel dimention and
42 self.color: Default to None, you can fill this to associate a color
45 # Calculate section values
46 self
.y
= sectionarray(arr
, x1
, x2
, y1
, y2
, debug
=debug
)
47 self
.dim
= sqrt( ((x2
-x1
)*xPixDim
)**2 + ((y2
-y1
)*yPixDim
)**2 )
48 self
.x
= arange(len(self
.y
))/float(len(self
.y
)-1) * self
.dim
50 # The caller can use this
53 ## Save some data about the segment
54 #self.x1, self.x2, self.y1, self.y2, self.xPixDim, self.yPixDim =\
55 # x1, x2, y1, y2, xPixDim, yPixDim
57 # Provided for further extensions (currently unused)
58 #self.plotkwargs = dict()
61 def sectionarray(arr
, x1
, x2
, y1
, y2
, debug
=False):
63 Returns the pixel-precise section of the array between the two points (x1,
64 y1) and (x2, y2) (extremezes included).
69 n
= max(abs(dx
),abs(dy
))
70 if debug
: print 'dx:', dx
, 'dy:', dy
, 'n:', n
72 if dx
== 0 and dy
== 0:
74 if debug
: print 'point segment'
78 if debug
: print 'vertical segment'
79 y
= arange(y1
, y2
+sign(dy
), sign(dy
))
83 if debug
: print 'orizzontale segment'
84 x
= arange(x1
, x2
+sign(dx
), sign(dx
))
88 if debug
: print 'skew segment'
89 m
= float(dy
) / float(dx
)
91 step
= float(dx
) / float(n
)
92 if debug
: print 'm:',m
,'c:',c
,'step:',step
93 X
= arange(x1
, x2
+step
, step
)
97 section
= arr
[Y
.round().astype('uint'), X
.round().astype('uint')]
101 if __name__
== "__main__":
102 from scipy
import lena
103 from pylab
import plot
, imshow
, title
, grid
, gcf
, gca
, show
, draw
106 title('Click on two points the close the window')
109 def button_press_callback(event
):
110 points
.append([event
.xdata
, event
.ydata
])
111 gca().plot((event
.xdata
,), (event
.ydata
,), 'k', marker
='.', ms
=10)
114 gcf().canvas
.mpl_connect('button_press_event', button_press_callback
)
118 points
[-2][0], points
[-1][0],
119 points
[-2][1], points
[-1][1], 1, 1)
121 plot(c
.x
, c
.y
, '-b.', linewidth
=1, ms
=10)
122 title('Image section')