def getMosaicImage(self, maxdimension=None, numtype=numpy.uint16): if not self.tiles: return None bbox = self.getMosaicImageBoundaries() imageshape = [bbox['max'][0] - bbox['min'][0], bbox['max'][1] - bbox['min'][1]] scale = 1.0 scaleoffset = [0, 0] if maxdimension is not None: for value in imageshape: newscale = float(maxdimension)/float(value) if newscale < scale: scale = newscale for i, value in enumerate(imageshape): imageshape[i] = int(numpy.ceil(scale*value)) # scaleoffset[i] = int(numpy.floor((maxdimension - imageshape[i])/2.0)) if self.tiles: numtype = self.tiles[0].image.dtype mosaicimage = numpy.zeros(imageshape, numtype) for tile in self.tiles: position = self.getTilePosition(tile) shape = self.getTileShape(tile) offset = (position[0] - bbox['min'][0], position[1] - bbox['min'][1]) offset = (int(round(offset[0] * scale + scaleoffset[0])), int(round(offset[1] * scale + scaleoffset[1]))) image = imagefun.scale(tile.image, scale) image = numpy.asarray(image, numtype) mosaicimage[offset[0]:offset[0] + image.shape[0], offset[1]:offset[1] + image.shape[1]] = image self.scale = scale return mosaicimage
def getMosaicImage(self, maxdimension=None): self.calculateMosaicImage() ### scale the mosaic shape if maxdimension is None: scale = 1.0 else: maxdim = max(self.mosaicshape) self.scale = float(maxdimension) / float(maxdim) ## this is stupid, but avoids rounding problems ## and misaligned matrices errors scale = float(maxdimension-1) / float(maxdim) self.scale = scale numtype = self.tiles[0].image.dtype ### find shape of final mosaic ### optimize me maxrow = maxcol = 0 for tile in self.tiles: scaled_tile = imagefun.scale(tile.image, scale) scaled_tile = numpy.asarray(scaled_tile, numtype) scaled_shape = scaled_tile.shape scaled_pos = self.scaled(tile.corner_pos) rowslice1 = scaled_pos[0],scaled_pos[0]+scaled_shape[0] colslice1 = scaled_pos[1],scaled_pos[1]+scaled_shape[1] rowslice = slice(scaled_pos[0],scaled_pos[0]+scaled_shape[0]) colslice = slice(scaled_pos[1],scaled_pos[1]+scaled_shape[1]) if rowslice1[1] > maxrow: maxrow = rowslice1[1] if colslice1[1] > maxcol: maxcol = colslice1[1] mshape = (maxrow,maxcol) ### create mosaic image mosaicimage = numpy.zeros(mshape, numtype) ### scale and insert tiles for tile in self.tiles: scaled_tile = imagefun.scale(tile.image, scale) scaled_tile = numpy.asarray(scaled_tile, numtype) scaled_shape = scaled_tile.shape scaled_pos = self.scaled(tile.corner_pos) rowslice = slice(scaled_pos[0],scaled_pos[0]+scaled_shape[0]) colslice = slice(scaled_pos[1],scaled_pos[1]+scaled_shape[1]) mosaicimage[rowslice, colslice] = scaled_tile return mosaicimage
def reshapeMask( image, mask ): # make sure the images have the same shape imgshape = numpy.asarray(image.shape) print "MRC Image Shape:" print imgshape imgsize = imgshape[0]*imgshape[1] print "MRC Image Size:" print imgsize maskshape = numpy.shape(mask) print "Mask Image Shape:" print maskshape print "resizing mask image." scaleFactor = float(imgshape[0])/float(maskshape[0]) print scaleFactor outimg = imagefun.scale( mask, scaleFactor ) maskshape = numpy.shape(outimg) print "Mask Image Shape:" print maskshape return outimg
def commitToDatabase(self, imgdata): sessiondata = imgdata['session'] rundir = self.params['rundir'] maskname = self.params['runname'] assessname = self.params['runname'] bin = self.params['bin'] maskdir = os.path.join(rundir, "masks") maskrundata, maskparamsdata = apMask.getMaskParamsByRunName( maskname, sessiondata) if not maskrundata: apMask.insertManualMaskRun(sessiondata, rundir, maskname, bin) maskrundata, maskparamsdata = apMask.getMaskParamsByRunName( maskname, sessiondata) try: apParam.createDirectory(maskdir) except: apDisplay.printWarning('can not create mask directory') massessrundata, exist = apMask.insertMaskAssessmentRun( sessiondata, maskrundata, assessname) # The mask file should only exist if the em_hole_finder found a region to mask. # If it does not exist, do not insert anything to the DB. if (os.path.exists(self.outfile)): apDisplay.printMsg("Writing results to database: " + time.asctime()) # Set black pixels to white and anything else to 0 img1 = Image.open(self.outfile) img2 = Image.eval(img1, lambda px: 255 if px == 0 else 0) # make sure the images have the same shape imgshape = numpy.asarray(imgdata['image'].shape) apDisplay.printMsg("MRC Image Shape:") print imgshape imgsize = imgshape[0] * imgshape[1] apDisplay.printMsg("MRC Image Size:") print imgsize maskshape = numpy.shape(img2) apDisplay.printMsg("Mask Image Shape:") print maskshape apDisplay.printMsg("resizing mask image with scale:") scaleFactorx = float(imgshape[0]) / float(maskshape[0]) scaleFactory = float(imgshape[1]) / float(maskshape[1]) scale = scaleFactorx, scaleFactory print scale img3 = imagefun.scale(img2, scale) maskshape = numpy.shape(img3) apDisplay.printMsg("Mask Image Shape:") print maskshape #img3 = numpy.resize(img2, imgshape) # not working img3path = self.outfile + "_tmp.jpg" scipy.misc.imsave(img3path, img3) labeled_regions, clabels = ndimage.label(img3) testlog = [False, 0, ""] infos = {} apDisplay.printMsg("getting mask region info.") infos, testlog = apCrud.getLabeledInfo(imgdata['image'], img3, labeled_regions, range(1, clabels + 1), False, infos, testlog) apDisplay.printMsg("inserting mask regions to DB.") print len(infos) area_max = imgsize * .9 offset = 1 for l1 in range(0, len(infos)): l = l1 + offset info = infos[l] area = info[0] print area if (area > 400 and area < area_max): apDisplay.printMsg("saving a region of size:") print area info.append(l) regiondata = apMask.insertMaskRegion( maskrundata, imgdata, info) # Insert mask assessment regions. This keeps track of the mask regions that the user wants to reject. allregiondata = apMask.getMaskRegions(maskrundata, imgdata) for regiondata in allregiondata: apMask.insertMaskAssessment(massessrundata, regiondata, True) # if self.assess != self.assessold and self.assess is not None: # #imageaccessor run is always named run1 # apDatabase.insertImgAssessmentStatus(imgdata, 'run1', self.assess) return
def commitToDatabase(self,imgdata): sessiondata = imgdata['session'] rundir = self.params['rundir'] maskname = self.params['runname'] assessname = self.params['runname'] bin = self.params['bin'] maskdir = os.path.join(rundir,"masks") maskrundata,maskparamsdata = apMask.getMaskParamsByRunName(maskname,sessiondata) if not maskrundata: apMask.insertManualMaskRun(sessiondata,rundir,maskname,bin) maskrundata,maskparamsdata = apMask.getMaskParamsByRunName(maskname,sessiondata) try: apParam.createDirectory(maskdir) except: apDisplay.printWarning('can not create mask directory') massessrundata,exist = apMask.insertMaskAssessmentRun(sessiondata,maskrundata,assessname) # The mask file should only exist if the em_hole_finder found a region to mask. # If it does not exist, do not insert anything to the DB. if ( os.path.exists(self.outfile) ): apDisplay.printMsg("Writing results to database: " + time.asctime()) # Set black pixels to white and anything else to 0 img1 = Image.open(self.outfile) img2 = Image.eval(img1, lambda px: 255 if px==0 else 0) # make sure the images have the same shape imgshape = numpy.asarray(imgdata['image'].shape) apDisplay.printMsg("MRC Image Shape:") print imgshape imgsize = imgshape[0]*imgshape[1] apDisplay.printMsg("MRC Image Size:") print imgsize maskshape = numpy.shape(img2) apDisplay.printMsg("Mask Image Shape:") print maskshape apDisplay.printMsg("resizing mask image with scale:") scaleFactorx = float(imgshape[0])/float(maskshape[0]) scaleFactory = float(imgshape[1])/float(maskshape[1]) scale = scaleFactorx, scaleFactory print scale img3 = imagefun.scale( img2, scale ) maskshape = numpy.shape(img3) apDisplay.printMsg("Mask Image Shape:") print maskshape #img3 = numpy.resize(img2, imgshape) # not working img3path = self.outfile + "_tmp.jpg" scipy.misc.imsave(img3path, img3) labeled_regions,clabels = ndimage.label(img3) testlog = [False,0,""] infos={} apDisplay.printMsg("getting mask region info.") infos,testlog = apCrud.getLabeledInfo( imgdata['image'], img3, labeled_regions, range(1,clabels+1), False, infos, testlog) apDisplay.printMsg("inserting mask regions to DB.") print len(infos) area_max = imgsize*.9 offset = 1 for l1 in range(0,len(infos)): l = l1 + offset info = infos[l] area=info[0] print area if (area > 400 and area < area_max): apDisplay.printMsg("saving a region of size:") print area info.append(l) regiondata = apMask.insertMaskRegion( maskrundata, imgdata, info ) # Insert mask assessment regions. This keeps track of the mask regions that the user wants to reject. allregiondata = apMask.getMaskRegions(maskrundata,imgdata) for regiondata in allregiondata: apMask.insertMaskAssessment(massessrundata,regiondata,True) # if self.assess != self.assessold and self.assess is not None: # #imageaccessor run is always named run1 # apDatabase.insertImgAssessmentStatus(imgdata, 'run1', self.assess) return