[go: up one dir, main page]

Menu

[r862]: / Segtool / Section.py  Maximize  Restore  History

Download this file

166 lines (129 with data), 4.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#
# Copyright (c) Madrid 2008
# BIT, ETSI Telecomunicacion, UPM
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
#
import wx
from lxml import etree
from string import atof
GlobalColors = ["white", "red", "blue", "green", "yellow", "cyan" ]
class Section:
def init_from_node(self, node):
if node.tag != "section":
raise ValueError("expected 'section' got '%s'" % (node.tag))
self.points = []
for n in node:
if (n.tag == 'color'):
self.color = n.get("value")
if (n.tag == 'point'):
x = atof(n.get("x"))
y = atof(n.get("y"))
self.points.append( (x,y) )
def __init__(self, color=None, points=None, node=None):
self.thickness = 1
if node:
self.init_from_node(node)
else:
self.init_from_color_points(color,points)
def init_from_color_points(self, color, points):
if color:
self.color = GlobalColors[color]
else:
self.color = GlobalColors[0]
if points:
self.points = points
else:
self.points = []
def Draw(self, dc):
pen = wx.Pen(self.color, self.thickness, wx.SOLID)
brush = wx.Brush(self.color, wx.CROSS_HATCH)
dc.SetPen(pen)
dc.SetBrush(brush)
dc.DrawPolygon(self.points)
def Invert(self):
self.points.reverse()
def Save(self, frame):
section = etree.Element("section")
frame.append(section)
color = etree.Element("color", value = self.color)
section.append(color)
for p in self.points:
cx = "%f" % (p[0])
cy = "%f" % (p[1])
section.append(etree.Element("point", x=cx, y=cy))
def SetPoints(self, points, scale):
self.points = []
for p in points:
self.points.append( ( p[0] * scale[0], p[1] * scale[1]) )
def GetPoints(self, scale):
points = []
for p in self.points:
points.append( ( p[0] * scale[0], p[1] * scale[1]) )
return points
def __eq__(self, other):
if (self.color != other.color):
return False
if len(self.points) != len(other.points):
return False
return self.points == other.points
class ImageSegmentation:
def __init__(self, image=None, Sections=None, node=None):
if node:
self.init_from_node(node)
else:
self.image_file = image
self.Sections = Sections
self.image = wx.Image(image, type=wx.BITMAP_TYPE_ANY, index = -1)
def init_from_node(self, node):
if node.tag != "frame":
raise ValueError("Expected 'frame' got '%s' instead" % node.tag)
self.Sections = []
for n in node:
if n.tag == "image":
self.image_file = n.get("name")
if n.tag == "section":
self.Sections.append(Section(node=n))
def GetImage(self):
return self.image
def GetSection(self):
return self.Sections
def SetSection(self, Sections):
self.Sections = Sections
def Save(self, root):
frame = etree.Element("frame")
image_name = etree.Element("image", name=self.image_file)
frame.append( image_name )
for sections in self.Sections:
section = etree.Element("section")
sections.Save( section )
frame.append( section )
root.append(frame)
def GetSections(self, out_size):
scale = ( (1.0 * out_size[0]) / self.image.GetWidth(),
(1.0 * out_size[1]) / self.image.GetHeight());
sections = []
for s in self.Sections:
sections.append(s.GetPoints(scale))
return sections
def __eq__(self, other):
if len(self.Sections) != len(other.Sections):
return False
for s in self.Sections:
if not (s in other.Sections):
return False
return self.image_file == other.image_file