/// <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 IBoundingBox GetInteriorBox(IBoundingBox toProcess, Ptype ptype) { Region topregion = ptype.Region("top"); Region bottomregion = ptype.Region("bottom"); Region leftregion = ptype.Region("left"); Region rightregion = ptype.Region("right"); int top = toProcess.Top + topregion.Bitmap.Height; int left = toProcess.Left + leftregion.Bitmap.Width; int height = toProcess.Height - topregion.Bitmap.Height - bottomregion.Bitmap.Height; int width = toProcess.Width - leftregion.Bitmap.Width - rightregion.Bitmap.Width; return(new BoundingBox(left, top, width, height)); }
/// <summary> /// Returns true if every edge matches the given hypothesis. /// </summary> /// <param name="features">The features found in the bitmap.</param> /// <param name="bitmap">The bitmap containing the hypothesis.</param> /// <returns>Returns true if every edge matches the given hypothesis.</returns> private bool EdgesMatch(Ptype ptype, Bitmap bitmap, IBoundingBox topleft, IBoundingBox topright, IBoundingBox bottomleft, IBoundingBox bottomright) { Region right = ptype.Region("right"); Region top = ptype.Region("top"); Region bottom = ptype.Region("bottom"); Region left = ptype.Region("left"); return(HorizontalPatternMatcher.Instance.Matches(top.Bitmap, bitmap, topleft.Top, topleft.Left + topleft.Width, topright.Left - 1) && HorizontalPatternMatcher.Instance.Matches(bottom.Bitmap, bitmap, bottomleft.Top + bottomleft.Height - bottom.Bitmap.Height, bottomleft.Left + bottomleft.Width, bottomright.Left - 1) && VerticalPatternMatcher.Instance.Matches(left.Bitmap, bitmap, topleft.Left, topleft.Top + topleft.Height, bottomleft.Top - 1) && VerticalPatternMatcher.Instance.Matches(right.Bitmap, bitmap, topright.Left + topright.Width - right.Bitmap.Width, topright.Top + topright.Height, bottomright.Top - 1)); }
public void FindContent(Tree occurrence, Bitmap image, Bitmap foreground, List <Tree> found) { Ptype ptype = (Ptype)occurrence["ptype"]; Region interiorregion = ptype.Region("interior"); if (interiorregion != null) { ContentFinder.Instance.FindContent(image, foreground, occurrence, found); } }
public IBoundingBox WriteBackgroundOver(Tree node, IBoundingBox regionToFill, Bitmap destination, int destTop, int destLeft) { Ptype ptype = (Ptype)node["ptype"]; Region interiorregion = ptype.Region("interior"); if (interiorregion != null) { IBoundingBox interior = GetInteriorBox(node, ptype); int top = interior.Top; int left = interior.Left; int bottom = interior.Top + interior.Height; int right = interior.Left + interior.Width; int startRow = Math.Max(regionToFill.Top, top); int startCol = Math.Max(regionToFill.Left, left); int endRow = Math.Min(regionToFill.Top + regionToFill.Height, bottom); int endCol = Math.Min(regionToFill.Left + regionToFill.Width, right); if (startRow > regionToFill.Top) { destTop += startRow - regionToFill.Top; } if (startCol > regionToFill.Left) { destLeft += startCol - regionToFill.Left; } int interiorheight = interiorregion.Bitmap.Height; int interiorwidth = interiorregion.Bitmap.Width; Parallel.For(startRow, endRow, delegate(int row) { int bkgTop = destTop + row - startRow; for (int col = startCol, bkgLeft = destLeft; col < endCol; col++, bkgLeft++) { destination[bkgTop, bkgLeft] = interiorregion.Bitmap[(row - top) % interiorheight, (col - left) % interiorwidth]; //image[/*top +*/ row/*(row - startRow)*/, left];//Interior[row - top, col - left]; } }); return(regionToFill); } return(null); }
public void SetForeground(Tree node, Bitmap image, Bitmap foreground) { Ptype ptype = (Ptype)node["ptype"]; Region interiorregion = ptype.Region("interior"); foreground.WriteBlock(Utils.BACKGROUND, node); if (interiorregion != null) { IBoundingBox interior = GetInteriorBox(node, ptype); int top = interior.Top; int left = interior.Left; int leftdepth = ptype.Region("left").Bitmap.Width; int bottom = interior.Top + interior.Height; int right = interior.Left + interior.Width; for (int row = top; row < bottom; row++) { for (int col = left; col < right; col++) { int backgroundValue = interiorregion.Bitmap[(row - top) % interiorregion.Bitmap.Height, (col - node.Left - leftdepth) % interiorregion.Bitmap.Width]; if (backgroundValue != image[row, col]) { foreground[row, col] = node.GetHashCode(); } } } //We looked at the features and might have counted them as foreground. EraseFeaturesFromforeground(foreground, node, ptype); } }
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); }