update doc
[Laueye.git] / src / graphical_analysis.py
blobff8460fac131f7ea917cd37cec398512952b105f
1 #coding: utf-8
2 #file name: graphical_analysis_pyqt4.py
3 #Laueye - A program to analyze Laue diffraction diagrams.
4 #Copyright (C) <2008> <Grissiom>
6 #This program is free software: you can redistribute it and/or modify
7 #it under the terms of the GNU General Public License as published by
8 #the Free Software Foundation, either version 3 of the License, or
9 #(at your option) any later version.
11 #This program is distributed in the hope that it will be useful,
12 #but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 #GNU General Public License for more details.
16 #You should have received a copy of the GNU General Public License
17 #along with this program. If not, see <http://www.gnu.org/licenses/>.
19 import Image
21 class circle:
22 '''a circle's data'''
23 def __init__(self):
24 self.x_list = []
25 self.y_list = []
26 self.x_boundry = [None, None]
27 self.y_boundry = [None, None]
28 def center(self):
29 '''return the location of the circle, in flaot format'''
30 x = 0
31 for i in self.x_list:
32 x += i
33 y = 0
34 for i in self.y_list:
35 y += i
36 return x / len(self.x_list), y / len(self.y_list)
39 scope = 1
41 def figure_out_circles(im_in, threshold):
42 '''figure out the circles in it
44 @param im_in the img to analyz
45 @param threshold the threshold we use to determin the boundry
46 @return return a list of circles
47 '''
48 return _get_circles(_setup_status(im_in, threshold))
50 def _setup_status(im_in, threshold):
51 '''figure out the boundry of circles for the image im_in.
52 this function should run *BEFORE* get_boundry() and run only once
54 @param im_in the image to setup.
55 @param threshold the threshold of black and white
56 @return the setuped status table.
57 0 for black, 1 for white, 2 if the pixel is just stand on the
58 edge, 4 for the pixels which is ready calculated into a
59 circle.'''
60 #TODO:This function is the most time wasting function.
61 #waite for further optimize.
62 width, height = im_in.size
64 # setup a temp matrices list here on status, 0 for black and 1 for white.
65 status = []
66 im_in = im_in.convert('L')
67 pix = im_in.load()
68 for i in range(width):
69 status.append([])
70 for k in range(height):
71 if pix[i, k] > threshold:
72 status[i].append(1)
73 else:
74 status[i].append(0)
76 li_range = range(-scope, scope + 1)
77 def is_boundry(i, k):
78 for n in li_range:
79 for m in li_range:
80 if status[i + n][k + m] == 0:
81 return True
83 for i in range(scope, width - scope):
84 for k in range(scope, height - scope):
85 if status[i][k] == 1:
86 if is_boundry(i, k):
87 status[i][k] = 2
88 return status
90 def _get_boundry(in_list, status):
91 '''get the boundry of the whole circle based on the status table.
93 @param in_list a point on the circle.
94 @param status the status table which is the base to analyze
95 @return x_list and y_listto store the boundry's xs and ys
96 '''
97 x_list = []
98 y_list = []
99 while len(in_list) != 0:
100 (x, y) = in_list.pop()
101 x_list.append(x)
102 y_list.append(y)
103 status[x][y] = 4
104 for i in range(-scope, scope + 1):
105 for k in range(-scope, scope + 1):
106 if status[x + i][y + k] == 2:
107 in_list.append([x + i, y + k])
108 return (x_list, y_list)
110 def _get_circles(status):
111 '''get the circles in the whole image.
113 @param status the status table which is the base of analyze.
114 @return a list of circles.'''
115 width = len(status)
116 height = len(status[0])
117 #TODO:maybe we need to check whether im_status is a matrices here.
118 # i.e, all the len(im_status[:]) have the same value.
119 circles = []
120 for i in range(scope, width - scope):
121 for k in range(scope, height - scope):
122 if status[i][k] == 2:
123 circles.append(circle())
124 circles[-1].x_list, circles[-1].y_list \
125 = _get_boundry([[i, k]], status)
126 if len(circles[-1].x_list) < 4:
127 del circles[-1]
128 return circles
130 if __name__ == '__main__':
131 # im = Laue_diagram('../test/test.png')
132 circles = figure_out_circles(Image.open('../test/test.png'), 128)
133 print 'we have', len(circles),'circles'
134 else:
135 pass
136 pass