def compose_galleries(path, path_hmm, quality="90", one_daughter=True, sample=30): logger = logging.getLogger('compose_galleries') column_name = 'Trajectory' path_index = os.path.join(path_hmm, '_index') if not os.path.isdir(path_index): logger.warning("Index path '%s' does not exist. Make sure the error correction was executed successfully." % path_index) return for filename in os.listdir(path_index): logger.info('Creating gallery overview for %s' % filename) group_name = os.path.splitext(filename)[0] t = read_table(os.path.join(path_index, filename))[1] t.reverse() if one_daughter: for record in t[:]: if record[column_name].split('__')[4] != 'B01': t.remove(record) n = len(t) if not sample is None and sample <= n: idx = random.sample(xrange(n), sample) idx.sort() d = [t[i] for i in idx] else: d = t n = len(d) results = {} for idx, record in enumerate(d): #print idx, record traj = record[column_name] items = traj.split('__') pos = items[1][1:] key = '__'.join(items[1:5]) gallery_path = os.path.join(path, 'analyzed', pos, 'gallery') if os.path.isdir(gallery_path): for gallery_name in os.listdir(gallery_path): img = ccore.readImageRGB(os.path.join(gallery_path, gallery_name, '%s.jpg' % key)) if gallery_name not in results: results[gallery_name] = ccore.RGBImage(img.width, img.height*n) img_out = results[gallery_name] ccore.copySubImage(img, ccore.Diff2D(0, 0), ccore.Diff2D(img.width, img.height), img_out, ccore.Diff2D(0, img.height*idx)) for gallery_name in results: path_out = os.path.join(path_hmm, '_gallery', gallery_name) safe_mkdirs(path_out) image_name = os.path.join(path_out, '%s.jpg' % group_name) ccore.writeImage(results[gallery_name], image_name, quality) logger.debug("Gallery image '%s' successfully written." % image_name) yield group_name
def getBackgroundForImage(self, imgIn, imgMask, outFilenameBase=None): mask_histo = [ x * imgMask.width * imgMask.height for x in imgMask.getHistogram(256) ] area_objects = mask_histo[0] area_background = sum(mask_histo[1:]) # pos, t, channel, path + filename imgConv = ccore.conversionTo8Bit(imgIn, 2**15, 2**15 + 4096, 0, 255) imgInf = ccore.infimum(imgMask, imgConv) inf_histo = [ x * (imgInf.width * imgInf.height) / area_background for x in imgInf.getHistogram(256) ] if self.oSettings.write_images and not outFilenameBase is None: outDir = os.path.dirname(outFilenameBase) filename = os.path.basename(outFilenameBase) if not os.path.isdir(outDir): os.makedirs(outDir) ccore.writeImage( imgConv, os.path.join(outDir, 'convert_%s' % filename.replace('tif', 'png'))) ccore.writeImage( imgInf, os.path.join(outDir, 'inf_%s' % filename.replace('tif', 'png'))) # we exclude the object pixels from the analysis inf_histo[0] = 0 # get statistical values meanval_background = sum([inf_histo[i] * i for i in range(256)]) sumval = 0.0 medianval_background = -1 quantile25 = -1 for i in range(256): sumval += inf_histo[i] if sumval >= 0.25 and quantile25 < 0: quantile25 = i if sumval >= 0.5 and medianval_background < 0: medianval_background = i break res = { 'mean': meanval_background, 'median': medianval_background, '25quantile': quantile25 } return res
def convertToOneFilePerTrack(cls, path_out, image_compression=''): for event_id in os.listdir(path_out): event_path = os.path.join(path_out, event_id) #print event_path if os.path.isdir(event_path): # get all image cutter files filenames = collect_files(event_path, extensions=['.jpg', '.png', '.tif'], absolute=True) if len(filenames) > 0: img_out = None # determine file extension ext = os.path.splitext(filenames[0])[1] # stitch image horizontally for idx, filename in enumerate(filenames): img = cls.read_image(filename) if img_out is None: size = img.width, img.height img_out = cls.IMAGE_CLASS(size[0] * len(filenames), size[1]) ccore.copySubImage(img, ccore.Diff2D(0, 0), ccore.Diff2D(size[0], size[1]), img_out, ccore.Diff2D(size[0]*idx, 0)) # save a one file with event_id (P,T,O) + extension filename_out = os.path.join(path_out, event_id) + ext #print filename_out ccore.writeImage(img_out, filename_out) path_out_info = os.path.join(path_out, '_info') makedirs(path_out_info) shutil.copy2(os.path.join(event_path, '_%s.txt' % event_id), os.path.join(path_out_info, '_%s.txt' % event_id)) shutil.rmtree(event_path, ignore_errors=True)
def render(self, strPathOut, dctRenderInfo=None, strFileSuffix='.jpg', strCompression='98', writeToDisc=True, images=None): lstImages = [] if not images is None: lstImages += images if dctRenderInfo is None: for name, oChannel in self._channel_registry.iteritems(): for strRegion, oContainer in oChannel.containers.iteritems(): strHexColor, fAlpha = oChannel.dctAreaRendering[strRegion] imgRaw = oChannel.meta_image.image imgCon = ccore.Image(imgRaw.width, imgRaw.height) ccore.drawContour(oContainer.getBinary(), imgCon, 255, False) lstImages.append((imgRaw, strHexColor, 1.0)) lstImages.append((imgCon, strHexColor, fAlpha)) else: for channel_name, dctChannelInfo in dctRenderInfo.iteritems(): if channel_name in self._channel_registry: oChannel = self._channel_registry[channel_name] if 'raw' in dctChannelInfo: strHexColor, fAlpha = dctChannelInfo['raw'] # special casing for virtual channel to mix # raw images together if oChannel.is_virtual(): lstImages.extend(oChannel.meta_images(fAlpha)) else: lstImages.append( (oChannel.meta_image.image, strHexColor, 1.0)) if 'contours' in dctChannelInfo: # transform the old dict-style to the new tuple-style, # which allows multiple definitions for one region if isinstance(dctChannelInfo['contours'], dict): lstContourInfos = [ (k, ) + v for k, v in dctChannelInfo['contours'].iteritems() ] else: lstContourInfos = dctChannelInfo['contours'] for tplData in lstContourInfos: strRegion, strNameOrColor, fAlpha, bShowLabels = tplData[: 4] # draw contours only if region is present if oChannel.has_region(strRegion): if len(tplData) > 4: bThickContours = tplData[4] else: bThickContours = False imgRaw = oChannel.meta_image.image if strNameOrColor == 'class_label': oContainer = oChannel.containers[strRegion] oRegion = oChannel.get_region(strRegion) dctLabels = {} dctColors = {} for iObjId, oObj in oRegion.iteritems(): iLabel = oObj.iLabel if not iLabel is None: if not iLabel in dctLabels: dctLabels[iLabel] = [] dctLabels[iLabel].append(iObjId) dctColors[ iLabel] = oObj.strHexColor imgCon2 = ccore.Image( imgRaw.width, imgRaw.height) for iLabel, lstObjIds in dctLabels.iteritems( ): imgCon = ccore.Image( imgRaw.width, imgRaw.height) # Flip this and use drawContours with fill option enables to get black background oContainer.drawContoursByIds( lstObjIds, 255, imgCon, bThickContours, False) # oContainer.drawContoursByIds(lstObjIds, 255, imgCon, bThickContours, True) lstImages.append( (imgCon, dctColors[iLabel], fAlpha)) if isinstance(bShowLabels, bool) and bShowLabels: oContainer.drawTextsByIds( lstObjIds, [str(iLabel)] * len(lstObjIds), imgCon2) lstImages.append((imgCon2, '#FFFFFF', 1.0)) else: oContainer = oChannel.containers[strRegion] oRegion = oChannel.get_region(strRegion) lstObjIds = oRegion.keys() imgCon = ccore.Image( imgRaw.width, imgRaw.height) if not strNameOrColor is None: oContainer.drawContoursByIds( lstObjIds, 255, imgCon, bThickContours, False) else: strNameOrColor = '#FFFFFF' lstImages.append( (imgCon, strNameOrColor, fAlpha)) if bShowLabels: imgCon2 = ccore.Image( imgRaw.width, imgRaw.height) oContainer.drawLabelsByIds( lstObjIds, imgCon2) lstImages.append( (imgCon2, '#FFFFFF', 1.0)) if len(lstImages) > 0: imgRgb = ccore.makeRGBImage( [x[0].getView() for x in lstImages], [ccore.RGBValue(*hex2rgb(x[1])) for x in lstImages], [x[2] for x in lstImages]) if writeToDisc: strFilePath = join( strPathOut, "P%s_T%05d%s" % (self.P, self._iT, strFileSuffix)) makedirs(strPathOut) ccore.writeImage(imgRgb, strFilePath, strCompression) self.logger.debug("* rendered image written '%s'" % strFilePath) else: strFilePath = '' return imgRgb, strFilePath
def __init__(self, eventselector, strPathIn, oP, strPathOut, imageCompression="85", imageSuffix=".jpg", border=0, writeSubdirs=True, writeDescription=True, size=None, oneFilePerTrack=False): self._bHasImages = False dctTimePoints = {} for strStartId, lstTimeData in eventselector.bboxes( \ size=size, border=border).iteritems(): items = Tracker.split_nodeid(strStartId) iStartT, iObjId = items[:2] if len(items) == 3: branch_id = items[2] else: branch_id = 1 if writeSubdirs: strPathOutEvent = os.path.join(strPathOut, self._format_name(oP, iStartT, iObjId, branch_id)) else: strPathOutEvent = strPathOut makedirs(strPathOutEvent) if writeDescription: oFile = file(os.path.join(strPathOutEvent, "_%s.txt" % self._format_name(oP, iStartT, iObjId, branch_id)), "w") lstData = ["Frame", "ObjId", "x1", "y1", "x2", "y2"] oFile.write("%s\n" % "\t".join(map(str, lstData))) for iCnt, (iT, tplBoundingBox, lstObjIds) in enumerate(lstTimeData): if writeDescription: lstData = [iT, ';'.join(map(str, lstObjIds))] + list(tplBoundingBox) oFile.write("%s\n" % "\t".join(map(str, lstData))) if not iT in dctTimePoints: dctTimePoints[iT] = [] dctTimePoints[iT].append((strStartId, lstObjIds, iCnt, strPathOutEvent, tplBoundingBox)) if writeDescription: oFile.close() for idx, (iT, lstItems) in enumerate(dctTimePoints.iteritems()): #print iT, lstItems imgXY = self._getImage(strPathIn, iT) for strStartId, lstObjIds, iCnt, strPathOutEvent, tplBoundingBox in lstItems: x1, y1, x2, y2 = tplBoundingBox x1Corr = 0 if x1 < 0 else x1 y1Corr = 0 if y1 < 0 else y1 x2Corr = imgXY.width-1 if x2 >= imgXY.width else x2 y2Corr = imgXY.height-1 if y2 >= imgXY.height else y2 imgSub = ccore.subImage(imgXY, ccore.Diff2D(x1Corr, y1Corr), ccore.Diff2D(x2Corr-x1Corr+1, y2Corr-y1Corr+1)) if (x1 < 0 or y1 < 0 or x2 >= imgXY.width or y2 >= imgXY.height): imgSub2 = self.IMAGE_CLASS(size[0], size[1]) ccore.copySubImage(imgSub, imgSub2, ccore.Diff2D(x1Corr-x1, y1Corr-y1)) imgSub = imgSub2 assert imgSub.width == size[0] assert imgSub.width == x2-x1+1 assert imgSub.height == size[1] assert imgSub.height == y2-y1+1 if self.PROCESS_LABEL: lstImages = [] for iObjId in lstObjIds: lstImages.append(ccore.copyImageIfLabel(imgSub, imgSub, iObjId)) imgSub = ccore.projectImage(lstImages, ccore.ProjectionType.MaxProjection) strFilenameImage = os.path.join(strPathOutEvent, "P%s__T%05d%s" % (oP, iT, imageSuffix)) ccore.writeImage(imgSub, strFilenameImage) if oneFilePerTrack and os.path.isdir(strPathOut): self.convertToOneFilePerTrack(strPathOut, imageCompression)
def compose_galleries(path, path_hmm, quality="90", one_daughter=True, sample=30): logger = logging.getLogger('compose_galleries') column_name = 'Trajectory' path_index = os.path.join(path_hmm, '_index') if not os.path.isdir(path_index): logger.warning(("Index path '%s' does not exist. Make sure the error" " correction was executed successfully." %path_index)) return for filename in os.listdir(path_index): logger.info('Creating gallery overview for %s' % filename) group_name = os.path.splitext(filename)[0] t = read_table(os.path.join(path_index, filename))[1] t.reverse() if one_daughter: for record in t[:]: if record[column_name].split('__')[4] != 'B01': t.remove(record) n = len(t) if not sample is None and sample <= n: idx = random.sample(xrange(n), sample) idx.sort() d = [t[i] for i in idx] else: d = t n = len(d) results = {} for idx, record in enumerate(d): #print idx, record traj = record[column_name] items = traj.split('__') pos = items[1][1:] key = '__'.join(items[1:5]) gallery_path = os.path.join(path, 'analyzed', pos, 'gallery') if os.path.isdir(gallery_path): for gallery_name in os.listdir(gallery_path): img = ccore.readImageRGB(os.path.join(gallery_path, gallery_name, '%s.jpg' % key)) if gallery_name not in results: results[gallery_name] = ccore.RGBImage(img.width, img.height*n) img_out = results[gallery_name] ccore.copySubImage(img, ccore.Diff2D(0, 0), ccore.Diff2D(img.width, img.height), img_out, ccore.Diff2D(0, img.height*idx)) for gallery_name in results: path_out = os.path.join(path_hmm, '_gallery', gallery_name) makedirs(path_out) image_name = os.path.join(path_out, '%s.jpg' % group_name) ccore.writeImage(results[gallery_name], image_name, quality) logger.debug("Gallery image '%s' successfully written." % image_name) yield group_name
def render(self, strPathOut, dctRenderInfo=None, strFileSuffix='.jpg', strCompression='98', writeToDisc=True, images=None): lstImages = [] if not images is None: lstImages += images if dctRenderInfo is None: for name, oChannel in self._channel_registry.iteritems(): for strRegion, oContainer in oChannel.containers.iteritems(): strHexColor, fAlpha = oChannel.dctAreaRendering[strRegion] imgRaw = oChannel.meta_image.image imgCon = ccore.Image(imgRaw.width, imgRaw.height) ccore.drawContour(oContainer.getBinary(), imgCon, 255, False) lstImages.append((imgRaw, strHexColor, 1.0)) lstImages.append((imgCon, strHexColor, fAlpha)) else: for channel_name, dctChannelInfo in dctRenderInfo.iteritems(): if channel_name in self._channel_registry: oChannel = self._channel_registry[channel_name] if 'raw' in dctChannelInfo: strHexColor, fAlpha = dctChannelInfo['raw'] # special casing for virtual channel to mix # raw images together if oChannel.is_virtual(): lstImages.extend(oChannel.meta_images(fAlpha)) else: lstImages.append((oChannel.meta_image.image, strHexColor, 1.0)) if 'contours' in dctChannelInfo: # transform the old dict-style to the new tuple-style, # which allows multiple definitions for one region if isinstance(dctChannelInfo['contours'], dict): lstContourInfos = [(k,)+v for k,v in dctChannelInfo['contours'].iteritems()] else: lstContourInfos = dctChannelInfo['contours'] for tplData in lstContourInfos: strRegion, strNameOrColor, fAlpha, bShowLabels = tplData[:4] # draw contours only if region is present if oChannel.has_region(strRegion): if len(tplData) > 4: bThickContours = tplData[4] else: bThickContours = False imgRaw = oChannel.meta_image.image if strNameOrColor == 'class_label': oContainer = oChannel.containers[strRegion] oRegion = oChannel.get_region(strRegion) dctLabels = {} dctColors = {} for iObjId, oObj in oRegion.iteritems(): iLabel = oObj.iLabel if not iLabel is None: if not iLabel in dctLabels: dctLabels[iLabel] = [] dctLabels[iLabel].append(iObjId) dctColors[iLabel] = oObj.strHexColor imgCon2 = ccore.Image(imgRaw.width, imgRaw.height) for iLabel, lstObjIds in dctLabels.iteritems(): imgCon = ccore.Image(imgRaw.width, imgRaw.height) # Flip this and use drawContours with fill option enables to get black background oContainer.drawContoursByIds(lstObjIds, 255, imgCon, bThickContours, False) # oContainer.drawContoursByIds(lstObjIds, 255, imgCon, bThickContours, True) lstImages.append((imgCon, dctColors[iLabel], fAlpha)) if isinstance(bShowLabels, bool) and bShowLabels: oContainer.drawTextsByIds(lstObjIds, [str(iLabel)]*len(lstObjIds), imgCon2) lstImages.append((imgCon2, '#FFFFFF', 1.0)) else: oContainer = oChannel.containers[strRegion] oRegion = oChannel.get_region(strRegion) lstObjIds = oRegion.keys() imgCon = ccore.Image(imgRaw.width, imgRaw.height) if not strNameOrColor is None: oContainer.drawContoursByIds(lstObjIds, 255, imgCon, bThickContours, False) else: strNameOrColor = '#FFFFFF' lstImages.append((imgCon, strNameOrColor, fAlpha)) if bShowLabels: imgCon2 = ccore.Image(imgRaw.width, imgRaw.height) oContainer.drawLabelsByIds(lstObjIds, imgCon2) lstImages.append((imgCon2, '#FFFFFF', 1.0)) if len(lstImages) > 0: imgRgb = ccore.makeRGBImage([x[0].getView() for x in lstImages], [ccore.RGBValue(*hex2rgb(x[1])) for x in lstImages], [x[2] for x in lstImages]) if writeToDisc: strFilePath = join(strPathOut, "P%s_T%05d%s" %(self.P, self._iT, strFileSuffix)) makedirs(strPathOut) ccore.writeImage(imgRgb, strFilePath, strCompression) self.logger.debug("* rendered image written '%s'" % strFilePath) else: strFilePath = '' return imgRgb, strFilePath
def cutTracks(self, full_track_data, img_container, plate, pos, lstTracks=None, channels=None, skip_done=False): #imoutDir = self.oSettings.galleryDir #inDir = os.path.join(self.oSettings.rawImgDir, plate) #lstTracks.sort() #imgContainer = self.imageImporter(inDir) #channels = self.oSettings.plateChannelDict[plate].values() #filter(lambda x: x.split('__')[0] != 'feature', impdata['plate1_1_013']['00008']['T00181__O0031']['primary']['primary'].keys()) #filter(lambda x: x.split('__')[0] != 'feature', impdata['plate1_1_013']['00008']['T00181__O0031']['primary']['primary'].keys()) #['tracking__upperleft_x', 'tracking__center_x', 'tracking__lowerright_y', 'class__label', 'class__name', 'tracking__lowerright_x', 'tracking__center_y', 'class__probability', 'tracking__upperleft_y'] if channels is None: channels = img_container.meta_data.channels imoutDir = os.path.join(self.baseOutDir, plate, pos) if not os.path.exists(imoutDir): print 'generating the folder %s' % imoutDir os.makedirs(imoutDir) if lstTracks is None: lstTracks = sorted(full_track_data[plate][pos].keys()) for trackId in lstTracks: center_values = zip(full_track_data[plate][pos][trackId][self.track_channel][self.track_region]['tracking__center_x'], full_track_data[plate][pos][trackId][self.track_channel][self.track_region]['tracking__center_y'], full_track_data[plate][pos][trackId][self.track_channel][self.track_region]['Frame']) print 'cutting ', trackId imout_filename = os.path.join(imoutDir, 'Gallery--%s.png' % (trackId)) if skip_done and os.path.isfile(imout_filename): continue # allocate output image imout = ccore.Image(len(center_values) * (2*self.width + 1), len(channels) * (2*self.width + 1)) images = {} x = 0 for cx, cy, timepoint in center_values: y = 0 for channel in channels : #image_filename = os.path.join(inDir, imageInfo[pos][timepoint][channel]['path'], # imageInfo[pos][timepoint][channel]['filename']) image_filename = os.path.join(img_container.path, img_container.dimension_lookup[pos][timepoint][channel][0]) #print image_filename imin = ccore.readImageMito(image_filename) x_ul = cx - self.width if cx >= self.width else 0 # max(cx - width, 0) #x_ul = max(cx - width, 0) y_ul = cy - self.width if cy >= self.width else 0 # max(cy - width, 0) #y_ul = max(cy - width, 0) x_lr = cx + self.width if cx + self.width < imin.width else imin.width - 1 #x_lr = min(cx + width, imin.width-1) y_lr = cy + self.width if cy + self.width < imin.height else imin.height - 1 #y_lr = min(cx + width, imin.height-1) w_x = x_lr - x_ul w_y = y_lr - y_ul #print x_ul, y_ul, x_lr, y_lr, w_x, w_y imsub = ccore.subImage(imin, ccore.Diff2D(int(x_ul), int(y_ul)), ccore.Diff2D(int(w_x), int(w_y))) ccore.copySubImage(imsub, imout, ccore.Diff2D(x, y)) y += (2 * self.width + 1) x += (2 * self.width + 1) ccore.writeImage(imout, imout_filename) return