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

Source Code for Module omero.util.uploadMask

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