Package omero :: Package util :: Module uploadMask
[hide private]
[frames] | no frames]

Source Code for Module omero.util.uploadMask

  1  """ 
  2   components/tools/OmeroPy/omero/util/UploadMask.py 
  3   
  4  ----------------------------------------------------------------------------- 
  5    Copyright (C) 2006-2009 University of Dundee. All rights reserved. 
  6   
  7   
  8    This program 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. 
 12    This program is distributed in the hope that it will be useful, 
 13    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 15    GNU General Public License for more details. 
 16   
 17    You should have received a copy of the GNU General Public License along 
 18    with this program; if not, write to the Free Software Foundation, Inc., 
 19    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
 20   
 21  ------------------------------------------------------------------------------ 
 22   
 23   
 24  @author  Jean-Marie Burel      
 25  <a href="mailto:j.burel@dundee.ac.uk">j.burel@dundee.ac.uk</a> 
 26  @author Donald MacDonald &nbsp;&nbsp;&nbsp;&nbsp; 
 27  <a href="mailto:donald@lifesci.dundee.ac.uk">donald@lifesci.dundee.ac.uk</a> 
 28  @version 3.0 
 29  <small> 
 30  (<b>Internal version:</b> $Revision: $Date: $) 
 31  </small> 
 32  @since 3.0-Beta4.1 
 33   
 34  """ 
 35   
 36  from OmeroPopo import MaskData 
 37  from OmeroPopo import ROIData 
 38  from OmeroPopo import ImageData 
 39  import math 
 40   
41 -class uploadMask():
42 43 ## 44 # Instantiate the uploadMask Object. 45 #
46 - def __init__(self):
47 ## Map of colour, roiclass this will store all the masks in the image, and 48 # all the roi with a particular colour. */ 49 self.roiMap = {};
50 51 ## 52 # Add a Mask Shape to the appropriate ROIClass, creating one if neccessary. 53 # @param image The Image containing the mask data. 54 # @param z The Z Section of the image. 55 # @param t The Time point of the image. 56 #
57 - def addMaskShape(self, image, z, t):
58 maskMap = self.createMasks(image, z, t); 59 60 for mask in maskMap: 61 roiClass = None; 62 if(self.roiMap.has_key(mask.getColour())): 63 roiClass = roiMap[mask.getColour()]; 64 else: 65 roiClass = ROIClass(); 66 self.roiMap[mask.getColour()] = roiClass; 67 roiClass.addMask(mask, z, t);
68 69 ## 70 # Get all the masks for the image. 71 # @param image The Image containing the mask data. 72 # @param z The Z Section of the image. 73 # @param t The Time point of the image. 74 # @return See above. 75 #
76 - def createMasks(self, inputImage, z, t):
77 value = None; 78 mask = None; 79 map = {}; 80 for x in range(inputImage.getWidth()): 81 for y in range(inputImage.getHeight()): 82 83 value = inputImage.getRGB(x, y); 84 if(value==Color.black.getRGB()): 85 continue; 86 if (not map.has_key(value)): 87 mask = MaskClass(value); 88 map[value] = mask; 89 else: 90 mask = map[value]; 91 mask.add(Point(x, y)); 92 return map;
93 94 ## 95 # Return all the roi for the image. 96 # @param image See above. 97 # @return See above. 98 #
99 - def getROIForImage(self, image):
100 roiList = [] 101 for roi in self.roiMap: 102 roiList.append(roi.getROI(image)); 103 return roiList;
104
105 -class MaskClass():
106 107 ## 108 # Instantiate a new mask object with colour value. 109 # @param value The colour of the mask as packedInt 110 #
111 - def __init__(self, value):
112 113 ## The points in the mask. These points are in the image coordinates. 114 self.points = {}; 115 116 ## The colour of the mask. 117 self.colour = value; 118 119 ## The min(x,y) and max(x,y) coordinates. */ 120 self.min = Point(); 121 self.max = Point(); 122 123 ## The width of the mask. 124 self.width = 0; 125 126 ## The height of the mask. 127 self.height = 0;
128 129 ## 130 # Get the colour of the mask. 131 # @return See above. 132 #
133 - def getColour(self):
134 return self.colour;
135 136 ## 137 # Get the Points in the mask as a bytestream that can be used to 138 # make an image. 139 # @return See above. 140 #
141 - def asBytes(self):
142 bytesArray = array.array('B'); 143 for cnt in range(int(math.ceil(self.width*self.height))): 144 bytesArray.append(0); 145 position = 0; 146 for y in range(self.max.y): 147 for x in range(self.max.x): 148 if self.points.has_key(Point(x,y)): 149 self.setBit(bytesArray, position, 1) 150 else: 151 self.setBit(bytesArray, position, 0) 152 position = position + 1; 153 154 byteSwappedArray = bytesArray.byteswap(); 155 bytesString = bytesArray.tostring(); 156 return bytesString;
157 158 159 160 ## 161 # Add Point p to the list of points in the mask. 162 # @param p See above. 163 #
164 - def add(self, p):
165 if(len(self.points) == 0): 166 self.min = Point(p); 167 self.max = Point(p); 168 else: 169 self.min.x = min(p.x, min.x); 170 self.min.y = min(p.y, min.y); 171 self.max.x = max(p.x, max.x); 172 self.max.y = max(p.y, max.y); 173 self.width = max.x-min.x+1; 174 self.height = max.y-min.y+1; 175 self.points.add(p);
176 177 ## 178 # Create a MaskData Object from the mask. 179 # @param z The Z section the mask data is on. 180 # @param t The T section the mask data is on. 181 # @return See above. 182 #
183 - def asMaskData(self, z, t):
184 mask = MaskData(); 185 mask.setX(self.min.x); 186 mask.setY(self.min.y); 187 mask.setWidth(self.width); 188 mask.setHeight(self.height); 189 mask.setFill(self.colour); 190 mask.setT(t); 191 mask.setZ(z); 192 mask.setMask(self.asBytes()); 193 return mask;
194
195 - def setBit(self, data, bit, val):
196 bytePosition = bit/8; 197 bitPosition = bit%8; 198 data[bytePosition] = data[bytePosition] & ~(0x1<<bitPosition) | (val<<bitPosition); 199
200 - def getBit(self, data, bit):
201 bytePosition = bit/8; 202 bitPosition = bit%8; 203 if ((data[bytePosition] & (0x1<<bitPosition))!=0): 204 return 1 205 else: 206 return 0
207
208 -class ROIClass():
209 210 211 ## 212 # Instantiate the ROIClass and create the maskMap. 213 #
214 - def __init__(self):
215 ## 216 # Map of the coordinates and mask objects, may have more than one 217 # mask on one plane. 218 # 219 self.maskMap = {};
220 221 ## 222 # Add a mask to the ROIMap, this will store the mask and it's z,t 223 # @param mask See above. 224 # @param z See above. 225 # @param t See above. 226 #
227 - def addMask(self, mask, z, t):
228 maskList = []; 229 coord = ROICoordinate(z, t); 230 if(self.maskMap.has_key(coord)): 231 maskList = self.maskMap[coord]; 232 else: 233 maskList = []; 234 maskMap.put(coord, maskList); 235 maskList.append(mask);
236 237 ## 238 # Create the roi for the 239 # @param image 240 # @return See above. 241 #
242 - def getROI(self, image):
243 roi = ROIData(); 244 roi.setId(image.getId()); 245 246 for coord in self.maskMap: 247 maskList = maskMap[coord]; 248 for mask in maskList: 249 toSaveMask = mask.asMaskData(coord.getZSection(), coord.getTimePoint()); 250 roi.addShapeData(toSaveMask); 251 return roi;
252 253 ## 254 # Point class with x, y values 255 #
256 -class Point():
257 258 ## 259 # Initialise point class 260 # @param p point class can be initialised from another point. 261 #
262 - def __init__(self, p = None):
263 if(p != None): 264 x = p.x; 265 y = p.y; 266 else: 267 x = 0; 268 y = 0;
269 270 ## 271 # Get the x value of the point 272 # @return See Above. 273 #
274 - def getX(self):
275 return self.x;
276 277 ## 278 # Set the x value of the point 279 # @param x See Above. 280 #
281 - def setX(self, x):
282 self.x = x;
283 284 ## 285 # Get the y value of the point 286 # @return See Above. 287 #
288 - def getY(self):
289 return self.y;
290 291 ## 292 # Set the y value of the point 293 # @param y See Above. 294 #
295 - def setY(self, x):
296 self.y = y;
297