def apply_features(self): self._features_calculated = True for region_name, container in self.containers.iteritems(): object_holder = ObjectHolder(region_name) if container is not None: for group, params in self.feature_groups.iteritems(): # XXX special casing for feature parameters. # call pattern like this: cnt.applyFeature(name, params) if group.startswith('haralick'): container.resetHaralick() for dist in params['dist']: container.addHaralickValue(dist) elif group == "spotfeatures": container.spot_diameter = params['diameter'] container.spot_threshold = params['thresh'] elif group == "granulometry": container.resetGranulometry() for val in params['se']: container.addGranulometryValue(val) container.applyFeature(group) for obj_id, c_obj in container.getObjects().iteritems(): dctFeatures = c_obj.getFeatures() # build a new ImageObject obj = ImageObject(c_obj) obj.iId = obj_id ul = obj.oRoi.upperLeft crack = [(pos[0] + ul[0], pos[1] + ul[1]) for pos in container.getCrackCoordinates(obj_id)] obj.crack_contour = crack # ORIENTATION TEST: orientation of objects (for tracking) # # at the moment a bit of a hack # # The problem is that orientation cannot be a feature # # but moments need to be chosen to calculate the orientation. # if self.feature_groups.has_key('moments'): obj.orientation = Orientation( angle=c_obj.orientation, eccentricity=dctFeatures['eccentricity']) # why do wo sort the features according to their names?? # does it matter? if self.lstFeatureNames is None: self.lstFeatureNames = sorted(dctFeatures.keys()) # assign feature values in sorted order as NumPy array features = (dctFeatures[f] for f in self.lstFeatureNames) obj.aFeatures = numpy.fromiter(features, dtype=float) object_holder[obj_id] = obj if self.lstFeatureNames is not None: object_holder.feature_names = self.lstFeatureNames self._regions[region_name] = object_holder
def apply_features(self): self._features_calculated = True for region_name, container in self.containers.iteritems(): object_holder = ObjectHolder(region_name) if not container is None: for strFeatureCategory in self.lstFeatureCategories: container.applyFeature(strFeatureCategory) # calculate set of haralick features # (with differnt distances) if 'haralick_categories' in self.dctFeatureParameters: for strHaralickCategory in self.dctFeatureParameters[ 'haralick_categories']: for iHaralickDistance in self.dctFeatureParameters[ 'haralick_distances']: container.haralick_distance = iHaralickDistance container.applyFeature(strHaralickCategory) for obj_id, c_obj in container.getObjects().iteritems(): dctFeatures = c_obj.getFeatures() # build a new ImageObject obj = ImageObject(c_obj) obj.iId = obj_id ul = obj.oRoi.upperLeft crack = [(pos[0] + ul[0], pos[1] + ul[1]) for pos in container.getCrackCoordinates(obj_id)] obj.crack_contour = crack # ORIENTATION TEST: orientation of objects (for tracking) # # at the moment a bit of a hack # # The problem is that orientation cannot be a feature # # but moments need to be chosen to calculate the orientation. # if 'moments' in self.lstFeatureCategories: obj.orientation = Orientation( angle=c_obj.orientation, eccentricity=dctFeatures['eccentricity']) # why do wo sort the features according to their names?? # does it matter? if self.lstFeatureNames is None: self.lstFeatureNames = sorted(dctFeatures.keys()) # assign feature values in sorted order as NumPy array features = (dctFeatures[f] for f in self.lstFeatureNames) obj.aFeatures = numpy.fromiter(features, dtype=float) object_holder[obj_id] = obj # print 'orientation %s (%i, %i): %f (%f deg)' % (obj_id, # obj.oRoi.upperLeft[0], # obj.oRoi.upperLeft[1], # obj.orientation, # 180.0 * obj.orientation / numpy.pi) if self.lstFeatureNames is not None: object_holder.feature_names = self.lstFeatureNames self._regions[region_name] = object_holder
def apply_features(self): self._features_calculated = True for region_name, container in self.containers.iteritems(): object_holder = ObjectHolder(region_name) if not container is None: for strFeatureCategory in self.lstFeatureCategories: container.applyFeature(strFeatureCategory) # calculate set of haralick features # (with differnt distances) if 'haralick_categories' in self.dctFeatureParameters: for strHaralickCategory in self.dctFeatureParameters['haralick_categories']: for iHaralickDistance in self.dctFeatureParameters['haralick_distances']: container.haralick_distance = iHaralickDistance container.applyFeature(strHaralickCategory) for obj_id, c_obj in container.getObjects().iteritems(): dctFeatures = c_obj.getFeatures() # build a new ImageObject obj = ImageObject(c_obj) obj.iId = obj_id ul = obj.oRoi.upperLeft crack = [(pos[0] + ul[0], pos[1] + ul[1]) for pos in container.getCrackCoordinates(obj_id)] obj.crack_contour = crack # ORIENTATION TEST: orientation of objects (for tracking) # # at the moment a bit of a hack # # The problem is that orientation cannot be a feature # # but moments need to be chosen to calculate the orientation. # if 'moments' in self.lstFeatureCategories: obj.orientation = Orientation(angle = c_obj.orientation, eccentricity = dctFeatures['eccentricity']) # why do wo sort the features according to their names?? # does it matter? if self.lstFeatureNames is None: self.lstFeatureNames = sorted(dctFeatures.keys()) # assign feature values in sorted order as NumPy array features = (dctFeatures[f] for f in self.lstFeatureNames) obj.aFeatures = numpy.fromiter(features, dtype=float) object_holder[obj_id] = obj # print 'orientation %s (%i, %i): %f (%f deg)' % (obj_id, # obj.oRoi.upperLeft[0], # obj.oRoi.upperLeft[1], # obj.orientation, # 180.0 * obj.orientation / numpy.pi) if self.lstFeatureNames is not None: object_holder.feature_names = self.lstFeatureNames self._regions[region_name] = object_holder
def apply_features(self): self._features_calculated = True for region_name, container in self.containers.iteritems(): object_holder = ObjectHolder(region_name) if container is not None: for group, params in self.feature_groups.iteritems(): if params is None: container.applyFeature(group) else: # special case for haralick features for param in params: container.haralick_distance = param container.applyFeature(group) for obj_id, c_obj in container.getObjects().iteritems(): dctFeatures = c_obj.getFeatures() # build a new ImageObject obj = ImageObject(c_obj) obj.iId = obj_id ul = obj.oRoi.upperLeft crack = [(pos[0] + ul[0], pos[1] + ul[1]) for pos in container.getCrackCoordinates(obj_id)] obj.crack_contour = crack # ORIENTATION TEST: orientation of objects (for tracking) # # at the moment a bit of a hack # # The problem is that orientation cannot be a feature # # but moments need to be chosen to calculate the orientation. # if self.feature_groups.has_key('moments'): obj.orientation = Orientation( angle = c_obj.orientation, eccentricity = dctFeatures['eccentricity'] ) # why do wo sort the features according to their names?? # does it matter? if self.lstFeatureNames is None: self.lstFeatureNames = sorted(dctFeatures.keys()) # assign feature values in sorted order as NumPy array features = (dctFeatures[f] for f in self.lstFeatureNames) obj.aFeatures = numpy.fromiter(features, dtype=float) object_holder[obj_id] = obj if self.lstFeatureNames is not None: object_holder.feature_names = self.lstFeatureNames self._regions[region_name] = object_holder
def apply_features(self, *args, **kw): """Concatenate features of images objects of different channels""" holder = ObjectHolder("-".join(self._merge_regions.values())) for cname, region_name in self._merge_regions.iteritems(): channel = self._channels[cname] holder0 = channel.get_region(region_name) pfx = "%s_%s" %(cname, region_name) feature_names = ["_".join((pfx, f)) for f in holder0.feature_names] holder.cat_samples(holder0, feature_names) removed = holder.remove_incomplete() if len(removed) > 0: self.logger.info("Found incomplete samples in merged channel") self.logger.info("removed samples: %s" %",".join([str(r) for r in removed])) self._regions[self.regkey] = holder self.lstFeatureNames = holder.feature_names
def apply_features(self, *args, **kw): """Concatenate features of images objects of different channels""" holder = ObjectHolder("-".join(self._merge_regions.values())) for cname, region_name in self._merge_regions.iteritems(): channel = self._channels[cname] holder0 = channel.get_region(region_name) pfx = "%s_%s" % (cname, region_name) feature_names = ["_".join((pfx, f)) for f in holder0.feature_names] holder.cat_samples(holder0, feature_names) removed = holder.remove_incomplete() if len(removed) > 0: self.logger.info("Found incomplete samples in merged channel") self.logger.info("removed samples: %s" % ",".join([str(r) for r in removed])) self._regions[self.regkey] = holder self.lstFeatureNames = holder.feature_names
def annotate(self, sample_objects, learner, container, region): """Annotate predefined class labels to picked samples.""" training_set = ObjectHolder(region.name) training_set.feature_names = region.feature_names for class_label, object_ids in sample_objects.iteritems(): class_name = learner.classdef.names[class_label] hex_color = learner.classdef.colors[class_name] for obj_id in object_ids: obj = region[obj_id] obj.iLabel = class_label obj.strClassName = class_name obj.strHexColor = hex_color training_set[obj_id] = obj return training_set
def apply_features(self): self._features_calculated = True for region_name, container in self.containers.iteritems(): object_holder = ObjectHolder(region_name) if not container is None: container.debug = False if container.debug: container.debug_folder = os.path.join("/Users/twalter/temp/spotfeatures", self.meta_image.coordinate.position) if not os.path.exists(container.debug_folder): os.makedirs(container.debug_folder) print 'generated ', container.debug_folder container.debug_prefix = 'img' # pass parameters to container for feature in self.dctFeatureParameters: if feature in ['featurecategory_haralick', 'featurecategory_haralick2']: container.resetHaralick() for haralick_size in self.dctFeatureParameters[feature]['dist']: container.addHaralickValue(haralick_size) elif feature == 'featurecategory_granulometry': container.resetGranulometry() for val in self.dctFeatureParameters[feature]['se']: container.addGranulometryValue(val) elif feature == 'featurecategory_spotfeatures': container.spot_diameter = self.dctFeatureParameters[feature]['diameter'] container.spot_threshold = self.dctFeatureParameters[feature]['thresh'] # apply feature for strFeatureCategory in self.lstFeatureCategories: container.applyFeature(strFeatureCategory) # --- # to replace # calculate set of haralick features # (with differnt distances) # if 'haralick_categories' in self.dctFeatureParameters: # for strHaralickCategory in self.dctFeatureParameters['haralick_categories']: # for iHaralickDistance in self.dctFeatureParameters['haralick_distances']: # container.haralick_distance = iHaralickDistance # container.applyFeature(strHaralickCategory) # --- for obj_id, c_obj in container.getObjects().iteritems(): dctFeatures = c_obj.getFeatures() # build a new ImageObject obj = ImageObject(c_obj) obj.iId = obj_id ul = obj.oRoi.upperLeft crack = [(pos[0] + ul[0], pos[1] + ul[1]) for pos in container.getCrackCoordinates(obj_id)] obj.crack_contour = crack # ORIENTATION TEST: orientation of objects (for tracking) # # at the moment a bit of a hack # # The problem is that orientation cannot be a feature # # but moments need to be chosen to calculate the orientation. # if 'moments' in self.lstFeatureCategories: obj.orientation = Orientation(angle = c_obj.orientation, eccentricity = dctFeatures['eccentricity']) # why do wo sort the features according to their names?? # does it matter? if self.lstFeatureNames is None: self.lstFeatureNames = sorted(dctFeatures.keys()) # assign feature values in sorted order as NumPy array features = (dctFeatures[f] for f in self.lstFeatureNames) obj.aFeatures = numpy.fromiter(features, dtype=float) object_holder[obj_id] = obj if self.lstFeatureNames is not None: object_holder.feature_names = self.lstFeatureNames self._regions[region_name] = object_holder
def apply_features(self): self._features_calculated = True for region_name, container in self.containers.iteritems(): object_holder = ObjectHolder(region_name) if container is not None: for group, params in self.feature_groups.iteritems(): # XXX special casing for feature parameters. # call pattern like this: cnt.applyFeature(name, params) if group.startswith('haralick'): container.resetHaralick() for dist in params['dist']: container.addHaralickValue(dist) elif group == "spotfeatures": container.spot_diameter = params['diameter'] container.spot_threshold = params['thresh'] elif group == "granulometry": container.resetGranulometry() for val in params['se']: container.addGranulometryValue(val) container.applyFeature(group) for obj_id, c_obj in container.getObjects().iteritems(): dctFeatures = c_obj.getFeatures() # build a new ImageObject obj = ImageObject(c_obj) obj.iId = obj_id ul = obj.oRoi.upperLeft crack = [(pos[0] + ul[0], pos[1] + ul[1]) for pos in container.getCrackCoordinates(obj_id)] obj.crack_contour = crack # ORIENTATION TEST: orientation of objects (for tracking) # # at the moment a bit of a hack # # The problem is that orientation cannot be a feature # # but moments need to be chosen to calculate the orientation. # if self.feature_groups.has_key('moments'): obj.orientation = Orientation( angle = c_obj.orientation, eccentricity = dctFeatures['eccentricity'] ) # why do wo sort the features according to their names?? # does it matter? if self.lstFeatureNames is None: self.lstFeatureNames = sorted(dctFeatures.keys()) # assign feature values in sorted order as NumPy array features = (dctFeatures[f] for f in self.lstFeatureNames) obj.aFeatures = numpy.fromiter(features, dtype=float) object_holder[obj_id] = obj if self.lstFeatureNames is not None: object_holder.feature_names = self.lstFeatureNames self._regions[region_name] = object_holder
def apply_features(self): self._features_calculated = True for region_name, container in self.containers.iteritems(): object_holder = ObjectHolder(region_name) if not container is None: container.debug = False if container.debug: container.debug_folder = os.path.join( "/Users/twalter/temp/spotfeatures", self.meta_image.coordinate.position) if not os.path.exists(container.debug_folder): os.makedirs(container.debug_folder) print 'generated ', container.debug_folder container.debug_prefix = 'img' # pass parameters to container for feature in self.dctFeatureParameters: if feature in [ 'featurecategory_haralick', 'featurecategory_haralick2' ]: container.resetHaralick() for haralick_size in self.dctFeatureParameters[ feature]['dist']: container.addHaralickValue(haralick_size) elif feature == 'featurecategory_granulometry': container.resetGranulometry() for val in self.dctFeatureParameters[feature]['se']: container.addGranulometryValue(val) elif feature == 'featurecategory_spotfeatures': container.spot_diameter = self.dctFeatureParameters[ feature]['diameter'] container.spot_threshold = self.dctFeatureParameters[ feature]['thresh'] # apply feature for strFeatureCategory in self.lstFeatureCategories: container.applyFeature(strFeatureCategory) # --- # to replace # calculate set of haralick features # (with differnt distances) # if 'haralick_categories' in self.dctFeatureParameters: # for strHaralickCategory in self.dctFeatureParameters['haralick_categories']: # for iHaralickDistance in self.dctFeatureParameters['haralick_distances']: # container.haralick_distance = iHaralickDistance # container.applyFeature(strHaralickCategory) # --- for obj_id, c_obj in container.getObjects().iteritems(): dctFeatures = c_obj.getFeatures() # build a new ImageObject obj = ImageObject(c_obj) obj.iId = obj_id ul = obj.oRoi.upperLeft crack = [(pos[0] + ul[0], pos[1] + ul[1]) for pos in container.getCrackCoordinates(obj_id)] obj.crack_contour = crack # ORIENTATION TEST: orientation of objects (for tracking) # # at the moment a bit of a hack # # The problem is that orientation cannot be a feature # # but moments need to be chosen to calculate the orientation. # if 'moments' in self.lstFeatureCategories: obj.orientation = Orientation( angle=c_obj.orientation, eccentricity=dctFeatures['eccentricity']) # why do wo sort the features according to their names?? # does it matter? if self.lstFeatureNames is None: self.lstFeatureNames = sorted(dctFeatures.keys()) # assign feature values in sorted order as NumPy array features = (dctFeatures[f] for f in self.lstFeatureNames) obj.aFeatures = numpy.fromiter(features, dtype=float) object_holder[obj_id] = obj if self.lstFeatureNames is not None: object_holder.feature_names = self.lstFeatureNames self._regions[region_name] = object_holder
def apply_features(self): for strKey, oContainer in self.dctContainers.iteritems(): oObjectHolder = ObjectHolder(strKey) if not oContainer is None: for strFeatureCategory in self.lstFeatureCategories: #print strFeatureCategory sys.stdout.flush() oContainer.applyFeature(strFeatureCategory) # calculate set of haralick features # (with differnt distances) if 'haralick_categories' in self.dctFeatureParameters: for strHaralickCategory in self.dctFeatureParameters['haralick_categories']: for iHaralickDistance in self.dctFeatureParameters['haralick_distances']: oContainer.haralick_distance = iHaralickDistance oContainer.applyFeature(strHaralickCategory) lstValidObjectIds = [] lstRejectedObjectIds = [] for iObjectId, oObject in oContainer.getObjects().iteritems(): dctFeatures = oObject.getFeatures() bAcceptObject = True # # post-processing # if self.bPostProcessing: # # if not eval(self.strPostprocessingConditions, dctFeatures): # if self.bPostProcessDeleteObjects: # #del dctObjects[iObjectId] # oContainer.delObject(iObjectId) # bAcceptObject = False # lstRejectedObjectIds.append(iObjectId) # else: # lstValidObjectIds.append(iObjectId) # else: # lstValidObjectIds.append(iObjectId) # # oContainer.lstValidObjectIds = lstValidObjectIds # oContainer.lstRejectedObjectIds = lstRejectedObjectIds if bAcceptObject: # build a new ImageObject oImageObject = ImageObject(oObject) oImageObject.iId = iObjectId if self.lstFeatureNames is None: self.lstFeatureNames = sorted(dctFeatures.keys()) # assign feature values in sorted order as NumPy array oImageObject.aFeatures = \ numpy.asarray(dict_values(dctFeatures, self.lstFeatureNames)) oObjectHolder[iObjectId] = oImageObject if not self.lstFeatureNames is None: oObjectHolder.setFeatureNames(self.lstFeatureNames) self._dctRegions[strKey] = oObjectHolder