/// <summary> /// Finds any occurrences given the found features. /// </summary> /// <param name="features">Feature occurrences that correspond to this model.</param> /// <param name="bitmap">Bitmap containing the feature occurrences.</param> /// <returns>A list of hypotheses</returns> public void FindOccurrences(Ptype ptype, Bitmap bitmap, IEnumerable <Tree> features, List <Tree> found) { Feature bottomleftFeature = ptype.Feature("bottomleft"); Feature toprightFeature = ptype.Feature("topright"); Feature bottomrightFeature = ptype.Feature("bottomright"); Feature topleftFeature = ptype.Feature("topleft"); Region topregion = ptype.Region("top"); Region leftregion = ptype.Region("left"); Region bottomregion = ptype.Region("bottom"); Region rightregion = ptype.Region("right"); Region interior = ptype.Region("interior"); //foreach each corresponding bottom left feature, find the corresponding bottom right and top right features foreach (Tree bottomleft in features) { if (bottomleft["feature"].Equals(bottomleftFeature)) { //Find each bottom right feature corresponding to the bottom left feature IEnumerable <Tree> bottomrights = GetBottomRightsFromBottomLeft(bottomrightFeature, bottomleft, features); //foreach each bottom right feature, get the corresponding top right features foreach (Tree bottomright in bottomrights) { //Get the top right feature corresponding to this bottom right feature IEnumerable <Tree> toprights = GetTopRightsFromBottomRight(toprightFeature, bottomright, features); foreach (Tree topright in toprights) { Tree topleft = GetTopLeft(topleftFeature, topright, bottomleft, features); //Validate the hypothesis by matching edges. If they match, then create occurrence. if (topleft != null && InteriorFits(interior, topregion, bottomregion, leftregion, rightregion, topleft, bottomleft, topright, bottomright) && EdgesMatch(ptype, bitmap, topleft, topright, bottomleft, bottomright)) { int top = topleft.Top; int left = topleft.Left; int height = bottomleft.Top + bottomleft.Height - topleft.Top; int width = topright.Left + topright.Width - topleft.Left; BoundingBox bb = new BoundingBox(left, top, width, height); Dictionary <String, Object> dict = new Dictionary <String, Object>(); dict.Add("type", "ptype"); dict.Add("ptype", ptype); dict.Add("ptype_id", ptype.Id); Tree prototypeOccurrence = Tree.FromBoundingBox(bb, dict); found.Add(prototypeOccurrence); } } } } } }
private static void EraseFeaturesFromforeground(Bitmap foreground, Tree occurrence, Ptype ptype) { //top left Feature topleft = ptype.Feature("topleft"); for (int row = occurrence.Top; row < occurrence.Top + topleft.Bitmap.Height; row++) { for (int col = occurrence.Left; col < occurrence.Left + topleft.Bitmap.Width; col++) { foreground[row, col] = Utils.BACKGROUND; } } //top right Feature topright = ptype.Feature("topright"); for (int row = occurrence.Top; row < occurrence.Top + topleft.Bitmap.Height; row++) { for (int col = occurrence.Left + occurrence.Width - topright.Bitmap.Width; col < occurrence.Left + occurrence.Width; col++) { foreground[row, col] = Utils.BACKGROUND; } } //bottom left Feature bottomleft = ptype.Feature("bottomleft"); for (int row = occurrence.Top + occurrence.Height - bottomleft.Bitmap.Height; row < occurrence.Top + occurrence.Height; row++) { for (int col = occurrence.Left; col < occurrence.Left + bottomleft.Bitmap.Width; col++) { foreground[row, col] = Utils.BACKGROUND; } } //bottom right Feature bottomright = ptype.Feature("bottomright"); for (int row = occurrence.Top + occurrence.Height - bottomright.Bitmap.Height; row < occurrence.Top + occurrence.Height; row++) { for (int col = occurrence.Left + occurrence.Width - bottomright.Bitmap.Width; col < occurrence.Left + occurrence.Width; col++) { foreground[row, col] = Utils.BACKGROUND; } } }
public void FindOccurrences(Ptype ptype, Bitmap bitmap, IEnumerable <Tree> features, List <Tree> found) { Feature part = ptype.Feature("part"); foreach (Tree feature in features) { if (part.Equals(feature["feature"])) { Dictionary <string, object> dict = new Dictionary <string, object>(); dict.Add("type", "ptype"); dict.Add("ptype", ptype); dict.Add("ptype_id", ptype.Id); Tree occurrence = Tree.FromBoundingBox(feature, dict); found.Add(occurrence); } } }
public Visual Visualize(Ptype ptype, object parameter = null) { if (!ptype.Model.Name.Equals("onepart")) { throw new Exception("One Part Model cannot render prototypes created by other models."); } Feature part = ptype.Feature("part"); Image img = new Image(); img.Source = ViewablePrototypeItem.ToBitmapSource(part.Bitmap); img.SnapsToDevicePixels = true; img.Stretch = System.Windows.Media.Stretch.Uniform; RenderOptions.SetBitmapScalingMode(img, BitmapScalingMode.NearestNeighbor); return(img); }
public Visual Visualize(Ptype ptype, object parameter = null) { if (!ptype.Model.Name.Equals("ninepart")) { throw new Exception("Nine Part Model cannot render prototypes created by other models."); } Feature bottomleftFeature = ptype.Feature("bottomleft"); Feature toprightFeature = ptype.Feature("topright"); Feature bottomrightFeature = ptype.Feature("bottomright"); Feature topleftFeature = ptype.Feature("topleft"); Region topregion = ptype.Region("top"); Region leftregion = ptype.Region("left"); Region bottomregion = ptype.Region("bottom"); Region rightregion = ptype.Region("right"); Region interiorRegion = ptype.Region("interior"); Grid grid = new Grid(); ////Find the biggest dimensions for each Part object so that ////we can figure out how many cells to add into the grid. int longestTopOrBottom = (int)Math.Max(topregion.Bitmap.Width, bottomregion.Bitmap.Width); int longestLeftOrRight = (int)Math.Max(leftregion.Bitmap.Height, rightregion.Bitmap.Height); int widestTLOrBL = (int)Math.Max(topleftFeature.Bitmap.Width, bottomleftFeature.Bitmap.Width); int widestTROrBR = (int)Math.Max(toprightFeature.Bitmap.Width, bottomrightFeature.Bitmap.Width); int tallestTLOrTR = (int)Math.Max(topleftFeature.Bitmap.Height, toprightFeature.Bitmap.Height); int tallestBLOrBR = (int)Math.Max(bottomleftFeature.Bitmap.Height, bottomrightFeature.Bitmap.Height); int interiorWidth = 0; int interiorHeight = 0; if (interiorRegion != null) { interiorHeight = interiorRegion.Bitmap.Height; interiorWidth = interiorRegion.Bitmap.Width; } //Assign the width and height of the grid. int width = Math.Max(widestTLOrBL + longestTopOrBottom + widestTROrBR + 2, interiorWidth + leftregion.Bitmap.Width + rightregion.Bitmap.Width + 2); int height = Math.Max(tallestTLOrTR + longestLeftOrRight + tallestBLOrBR + 2, interiorHeight + topregion.Bitmap.Height + bottomregion.Bitmap.Height + 2); //Set the rows and columns of the grid. for (int row = 0; row < height; row++) { RowDefinition rowdef = new RowDefinition(); grid.RowDefinitions.Add(rowdef); } for (int col = 0; col < width; col++) { ColumnDefinition coldef = new ColumnDefinition(); grid.ColumnDefinitions.Add(coldef); } //Add each Part to the grid (cells = pixels). PtypeVisualizerHelper.AddFeatureToGrid(topleftFeature, new Prefab.Point(0, 0), grid); PtypeVisualizerHelper.AddFeatureToGrid(toprightFeature, new Prefab.Point(width - toprightFeature.Bitmap.Width, 0), grid); PtypeVisualizerHelper.AddFeatureToGrid(bottomleftFeature, new Prefab.Point(0, height - bottomleftFeature.Bitmap.Height), grid); PtypeVisualizerHelper.AddFeatureToGrid(bottomrightFeature, new Prefab.Point(width - bottomrightFeature.Bitmap.Width, height - bottomrightFeature.Bitmap.Height), grid); PtypeVisualizerHelper.AddHorizontalRegionToGrid(topregion.Bitmap, new Prefab.Point(topleftFeature.Bitmap.Width + 1, 0), grid); PtypeVisualizerHelper.AddHorizontalRegionToGrid(bottomregion.Bitmap, new Prefab.Point(bottomleftFeature.Bitmap.Width + 1, height - bottomregion.Bitmap.Height), grid); PtypeVisualizerHelper.AddVerticalRegionToGrid(leftregion.Bitmap, new Prefab.Point(0, topleftFeature.Bitmap.Height + 1), grid); PtypeVisualizerHelper.AddVerticalRegionToGrid(rightregion.Bitmap, new Prefab.Point(width - rightregion.Bitmap.Width, toprightFeature.Bitmap.Height + 1), grid); if (interiorRegion != null) { Prefab.Point location; if (interiorRegion.MatchStrategy.Equals("horizontal")) { location = new Prefab.Point(topleftFeature.Bitmap.Width + 1, (height - interiorHeight) / 2); } else { location = new Prefab.Point((width - interiorWidth) / 2, topleftFeature.Bitmap.Height + 1); } PtypeVisualizerHelper.AddEachPixelOfBitmapToGrid(interiorRegion.Bitmap, location, grid); } return(grid); }