public Mutable(Dictionary <string, Bitmap> features, Dictionary <string, Region> regions) { List <Bitmap> fcpy = new List <Bitmap>(); foreach (Bitmap f in features.Values) { Bitmap cpy = Bitmap.SetTransparentValues(f); if (Bitmap.AllTransparent(cpy)) { throw new Exception("Cannot create an all transparent Bitmap."); } fcpy.Add(cpy); } this.Features = new Dictionary <string, Bitmap>(features); this.Regions = new Dictionary <string, Region>(regions); }
/// <summary> /// Uses the assigned value to a part to parameterize that part from examples. /// It caches the parameterizations, and checks the cache to see if it has /// made the parameterization beforeache. /// </summary> /// <param name="name">Name of the part to parameterize.</param> /// <param name="value">The part's value.</param> /// <param name="assignment">The whole assignment.</param> /// <param name="positives">Positive examples</param> /// <param name="negatives">Negative examples</param> /// <param name="cache">The cache foreach storing parameterizations to check foreach subsequent calls.</param> /// <returns>The parameterized part (either a feature or region)</returns> public static object ExtractPart(string name, object value, Dictionary <string, Part> assignment, IEnumerable <Bitmap> positives, IEnumerable <Bitmap> negatives, Dictionary <object, object> cache) { //Look in our cache to see if we extracted the part beforeache object extracted = null; Size size = null; RegionParameters rps = null; BackgroundValue bv = null; if (value is Size) { size = (Size)value; } else if (value is RegionParameters) { rps = (RegionParameters)value; } else if (value is string) { bv = GetBackgroundValue((String)value, assignment); value = bv; } Tuple <string, object> key = new Tuple <string, object>(name, value); if (!cache.ContainsKey(key)) { switch (name) { case "topleft": extracted = CropFromManyRelativeTopLeft(0, 0, size.Width, size.Height, positives); break; case "bottomleft": extracted = CropFromManyRelativeBottomLeft(size.Height, 0, size.Width, size.Height, positives); break; case "topright": extracted = CropFromManyRelativeTopRight(0, size.Width, size.Width, size.Height, positives); break; case "bottomright": extracted = CropFromManyRelativeBottomRight(size.Height, size.Width, size.Width, size.Height, positives); break; case "top": extracted = GetHorizontal(true, rps.Start, rps.End, rps.Depth, positives); break; case "bottom": extracted = GetHorizontal(false, rps.Start, rps.End, rps.Depth, positives); break; case "left": extracted = GetVertical(true, rps.Start, rps.End, rps.Depth, positives); break; case "right": extracted = GetVertical(false, rps.Start, rps.End, rps.Depth, positives); break; case "interior": extracted = GetInterior(bv, positives); break; } //Features that have all one value are likely to be background. So we disallow it. //Features that are all transparent can't be found (obviously). So we disallow it. if (extracted != null && extracted is Bitmap && (Bitmap.AllOneValue((Bitmap)extracted) || Bitmap.AllTransparent((Bitmap)extracted))) { extracted = null; } cache.Add(key, extracted); } else { extracted = cache[key]; } return(extracted); }