def draw_annotation_images(self, plate, training_set, container, learner, rid=""): cldir = dict([(cname, join(learner.samples_dir, cname)) \ for cname in learner.class_names.values()]) # create dir per class name for dir_ in cldir.values(): makedirs(dir_) for obj in training_set.itervalues(): rgb_value = ccore.RGBValue(*hex2rgb(obj.strHexColor)) file_ = 'PL%s___P%s___T%05d___X%04d___Y%04d' \ %(plate, self.P, self._iT, obj.oCenterAbs[0], obj.oCenterAbs[1]) obj.file = file_ file_ = join(cldir[obj.strClassName], '%s___%s.png' % (file_, rid + "_%s")) container.exportObject(obj.iId, file_ % "img", file_ % "msk") container.markObjects([obj.iId], rgb_value, False, True) ccore.drawFilledCircle(ccore.Diff2D(*obj.oCenterAbs), 3, container.img_rgb, rgb_value)
def findObjects(self, classifier): """ runs the classifier """ objects = list() probs = list() channel = self._channels[classifier.name.title()] for region in channel.region_names(): holder = channel.get_region(region) container = channel.containers[region] for l, obj in holder.iteritems(): obj.iLabel, prob = classifier.predict(obj.aFeatures, holder.feature_names) obj.strClassNames = classifier.class_names[obj.iLabel] objects.append(obj) probs.append(prob) # for classification images hexcolor = classifier.hexcolors[ \ classifier.class_names[obj.iLabel]] rgb = ccore.RGBValue(*hexToRgb(hexcolor)) container.markObjects([l], rgb, False, True) self.objects = np.array(objects) self.probs = np.array(probs)
def render(self, dctRenderInfo=None, 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) 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]) return imgRgb