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/>.
26 self
.x_boundry
= [None, None]
27 self
.y_boundry
= [None, None]
29 '''return the location of the circle, in flaot format'''
36 return x
/ len(self
.x_list
), y
/ len(self
.y_list
)
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
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
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.
66 im_in
= im_in
.convert('L')
68 for i
in range(width
):
70 for k
in range(height
):
71 if pix
[i
, k
] > threshold
:
76 li_range
= range(-scope
, scope
+ 1)
80 if status
[i
+ n
][k
+ m
] == 0:
83 for i
in range(scope
, width
- scope
):
84 for k
in range(scope
, height
- scope
):
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
99 while len(in_list
) != 0:
100 (x
, y
) = in_list
.pop()
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.'''
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.
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:
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'