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  from OmeroPopo import ROICoordinate 
 40  import math 
 41   
42 -class uploadMask():
43 44 ## 45 # Instantiate the uploadMask Object. 46 #
47 - def __init__(self):
48 ## Map of colour, roiclass this will store all the masks in the image, and 49 # all the roi with a particular colour. */ 50 self.roiMap = {};
51 52 ## 53 # Add a Mask Shape to the appropriate ROIClass, creating one if neccessary. 54 # @param image The Image containing the mask data. 55 # @param z The Z Section of the image. 56 # @param t The Time point of the image. 57 #
58 - def addMaskShape(self, image, z, t):
59 maskMap = self.createMasks(image, z, t); 60 61 for mask in maskMap: 62 roiClass = None; 63 if(self.roiMap.has_key(mask.getColour())): 64 roiClass = self.roiMap[mask.getColour()]; 65 else: 66 roiClass = ROIClass(); 67 self.roiMap[mask.getColour()] = roiClass; 68 roiClass.addMask(mask, z, t);
69 70 ## 71 # Get all the masks for the image. 72 # @param image The Image containing the mask data. 73 # @param z The Z Section of the image. 74 # @param t The Time point of the image. 75 # @return See above. 76 #
77 - def createMasks(self, inputImage, z, t):
78 value = None; 79 mask = None; 80 map = {}; 81 for x in range(inputImage.getWidth()): 82 for y in range(inputImage.getHeight()): 83 84 value = inputImage.getRGB(x, y); 85 if(value==Color.black.getRGB()): 86 continue; 87 if (not map.has_key(value)): 88 mask = MaskClass(value); 89 map[value] = mask; 90 else: 91 mask = map[value]; 92 mask.add(Point(x, y)); 93 return map;
94 95 ## 96 # Return all the roi for the image. 97 # @param image See above. 98 # @return See above. 99 #
100 - def getROIForImage(self, image):
101 roiList = [] 102 for roi in self.roiMap: 103 roiList.append(roi.getROI(image)); 104 return roiList;
105
106 -class MaskClass():
107 108 ## 109 # Instantiate a new mask object with colour value. 110 # @param value The colour of the mask as packedInt 111 #
112 - def __init__(self, value):
113 114 ## The points in the mask. These points are in the image coordinates. 115 self.points = {}; 116 117 ## The colour of the mask. 118 self.colour = value; 119 120 ## The min(x,y) and max(x,y) coordinates. */ 121 self.min = Point(); 122 self.max = Point(); 123 124 ## The width of the mask. 125 self.width = 0; 126 127 ## The height of the mask. 128 self.height = 0;
129 130 ## 131 # Get the colour of the mask. 132 # @return See above. 133 #
134 - def getColour(self):
135 return self.colour;
136 137 ## 138 # Get the Points in the mask as a bytestream that can be used to 139 # make an image. 140 # @return See above. 141 #
142 - def asBytes(self):
143 import array 144 bytesArray = array.array('B'); 145 for cnt in range(int(math.ceil(self.width*self.height))): 146 bytesArray.append(0); 147 position = 0; 148 for y in range(self.max.y): 149 for x in range(self.max.x): 150 if self.points.has_key(Point(x,y)): 151 self.setBit(bytesArray, position, 1) 152 else: 153 self.setBit(bytesArray, position, 0) 154 position = position + 1; 155 156 byteSwappedArray = bytesArray.byteswap(); 157 bytesString = bytesArray.tostring(); 158 return bytesString;
159 160 161 162 ## 163 # Add Point p to the list of points in the mask. 164 # @param p See above. 165 #
166 - def add(self, p):
167 if(len(self.points) == 0): 168 self.min = Point(p); 169 self.max = Point(p); 170 else: 171 self.min.x = min(p.x, min.x); 172 self.min.y = min(p.y, min.y); 173 self.max.x = max(p.x, max.x); 174 self.max.y = max(p.y, max.y); 175 self.width = max.x-min.x+1; 176 self.height = max.y-min.y+1; 177 self.points.add(p);
178 179 ## 180 # Create a MaskData Object from the mask. 181 # @param z The Z section the mask data is on. 182 # @param t The T section the mask data is on. 183 # @return See above. 184 #
185 - def asMaskData(self, z, t):
186 mask = MaskData(); 187 mask.setX(self.min.x); 188 mask.setY(self.min.y); 189 mask.setWidth(self.width); 190 mask.setHeight(self.height); 191 mask.setFill(self.colour); 192 mask.setT(t); 193 mask.setZ(z); 194 mask.setMask(self.asBytes()); 195 return mask;
196
197 - def setBit(self, data, bit, val):
198 bytePosition = bit/8; 199 bitPosition = bit%8; 200 data[bytePosition] = data[bytePosition] & ~(0x1<<bitPosition) | (val<<bitPosition); 201
202 - def getBit(self, data, bit):
203 bytePosition = bit/8; 204 bitPosition = bit%8; 205 if ((data[bytePosition] & (0x1<<bitPosition))!=0): 206 return 1 207 else: 208 return 0
209
210 -class ROIClass():
211 212 213 ## 214 # Instantiate the ROIClass and create the maskMap. 215 #
216 - def __init__(self):
217 ## 218 # Map of the coordinates and mask objects, may have more than one 219 # mask on one plane. 220 # 221 self.maskMap = {};
222 223 ## 224 # Add a mask to the ROIMap, this will store the mask and it's z,t 225 # @param mask See above. 226 # @param z See above. 227 # @param t See above. 228 #
229 - def addMask(self, mask, z, t):
230 maskList = []; 231 coord = ROICoordinate(z, t); 232 if(self.maskMap.has_key(coord)): 233 maskList = self.maskMap[coord]; 234 else: 235 maskList = []; 236 self.maskMap.put(coord, maskList); 237 maskList.append(mask);
238 239 ## 240 # Create the roi for the 241 # @param image 242 # @return See above. 243 #
244 - def getROI(self, image):
245 roi = ROIData(); 246 roi.setId(image.getId()); 247 248 for coord in self.maskMap: 249 maskList = self.maskMap[coord]; 250 for mask in maskList: 251 toSaveMask = mask.asMaskData(coord.getZSection(), coord.getTimePoint()); 252 roi.addShapeData(toSaveMask); 253 return roi;
254 255 ## 256 # Point class with x, y values 257 #
258 -class Point():
259 260 ## 261 # Initialise point class 262 # @param p point class can be initialised from another point. 263 #
264 - def __init__(self, p = None):
265 if(p != None): 266 x = p.x; 267 y = p.y; 268 else: 269 x = 0; 270 y = 0;
271 272 ## 273 # Get the x value of the point 274 # @return See Above. 275 #
276 - def getX(self):
277 return self.x;
278 279 ## 280 # Set the x value of the point 281 # @param x See Above. 282 #
283 - def setX(self, x):
284 self.x = x;
285 286 ## 287 # Get the y value of the point 288 # @return See Above. 289 #
290 - def getY(self):
291 return self.y;
292 293 ## 294 # Set the y value of the point 295 # @param y See Above. 296 #
297 - def setY(self, y):
298 self.y = y;
299