File pruning.
[pyplotsuite.git] / imageanalyzer / section.py
blobb246add4a506df683bbf0d025ccbf0d94d1a8745
1 #!/usr/bin/env python
2 # -*- coding: UTF8 -*-
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
16 def rounda(array):
17 """Round the array passed to the nearest integer."""
18 return floor(array + 0.5).astype('int32')
20 class ArraySection:
21 """
22 Class representing an array section. See the __init__ documetation.
23 """
24 def __init__(self, arr, x1, x2, y1, y2, xPixDim, yPixDim, debug=False):
25 """
26 Parameters
27 arr: arrray to section
28 x1, x2,
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.
32 xPixDim,
33 yPixDim: X and Y dimention of one pixel (element) of the array
34 debug: optionally enable debug prints
36 Class Attributes:
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
41 section slope.
42 self.color: Default to None, you can fill this to associate a color
43 to the section
44 """
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
51 self.color = None
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):
62 """
63 Returns the pixel-precise section of the array between the two points (x1,
64 y1) and (x2, y2) (extremezes included).
66 """
67 dx = x2 - x1
68 dy = y2 - y1
69 n = max(abs(dx),abs(dy))
70 if debug: print 'dx:', dx, 'dy:', dy, 'n:', n
72 if dx == 0 and dy == 0:
73 # Single point
74 if debug: print 'point segment'
75 section = arr[x1, y1]
76 elif dx == 0:
77 # Vertical segment
78 if debug: print 'vertical segment'
79 y = arange(y1, y2+sign(dy), sign(dy))
80 section = arr[y, x1]
81 elif dy == 0:
82 # Horizzontal segment
83 if debug: print 'orizzontale segment'
84 x = arange(x1, x2+sign(dx), sign(dx))
85 section = arr[y1, x]
86 else:
87 # Skew segment
88 if debug: print 'skew segment'
89 m = float(dy) / float(dx)
90 c = y1 - m*x1
91 step = float(dx) / float(n)
92 if debug: print 'm:',m,'c:',c,'step:',step
93 X = arange(x1, x2+step, step)
94 Y = m * X + c
96 arr = array(arr)
97 section = arr[Y.round().astype('uint'), X.round().astype('uint')]
99 return section
101 if __name__ == "__main__":
102 from scipy import lena
103 from pylab import plot, imshow, title, grid, gcf, gca, show, draw
104 l = lena()
105 imshow(l)
106 title('Click on two points the close the window')
108 points = []
109 def button_press_callback(event):
110 points.append([event.xdata, event.ydata])
111 gca().plot((event.xdata,), (event.ydata,), 'k', marker='.', ms=10)
112 draw()
114 gcf().canvas.mpl_connect('button_press_event', button_press_callback)
115 show()
117 c = ArraySection(l,
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')
123 grid(True)
124 show()