/// <summary>
        /// Adds a bitmap to extend the pattern on the left hand side. This makes it easy to build the shortest
        /// repeating pattern.
        /// </summary>
        /// <param name="pattern"></param>
        /// <param name="toConcat"></param>
        /// <returns></returns>
        public static Bitmap Append(Bitmap pattern, Bitmap toConcat)
        {
            Bitmap newPat;
            if (pattern == null)
            {
                newPat = Bitmap.DeepCopy(toConcat);
            }
            else if (toConcat == null)
            {
                newPat = Bitmap.DeepCopy(pattern);
            }
            else if (Bitmap.ExactlyMatches(pattern, toConcat))
            {
                newPat = Bitmap.DeepCopy(pattern);
            }
            else
            {
				newPat = Bitmap.FromDimensions(pattern.Width + toConcat.Width, pattern.Height);

                for (int row = 0; row < toConcat.Height; row++)
                {
                    for (int col = 0; col < pattern.Width; col++)
                    {
						((Bitmap)newPat)[row, col] = pattern[row, col];
                    }
                    for (int col = pattern.Width; col < newPat.Width; col++)
                    {
						((Bitmap)newPat)[row,col] = toConcat[row, col - pattern.Width];
                    }
                }
            }

            return newPat;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Collects all of the features that match the bitmap at the given probeOffset into the
        /// given feature bucket. Returns true if any matches were found.
        /// </summary>
        /// <param name="bitmap">The bitmap to find features in.</param>
        /// <param name="probeOffset">The offset that was the initial point to check in the bitmap.</param>
        /// <param name="bucket">The bucket where any found features will be added</param>
        /// <returns>True if there were any matches.</returns>
		public void GetMatches(Bitmap bitmap, int probeOffsetX, int probeOffsetY, ICollection<Tree> bucket)
        {
            
            foreach (Point offset in OffsetsToTest)
            {
                int imagex, imagey;

				int featureOffsetX = offset.X + FeatureWithHotspot.HotspotX;
				int featureOffsetY = offset.Y + FeatureWithHotspot.HotspotY;

				imagex = offset.X + probeOffsetX;
				imagey = offset.Y + probeOffsetY;

				if (imagey < 0 || imagex < 0
					|| imagey >= bitmap.Height || imagex >= bitmap.Width)
					return;


				int featurePixel = FeatureWithHotspot.Feature.Bitmap[featureOffsetY, featureOffsetX];
				if (featurePixel != bitmap[imagey, imagex])
					return;


            }

			AddMatch(probeOffsetX, probeOffsetY, bucket);
        }
Exemplo n.º 3
0
		public void FindContent(Bitmap image, Bitmap foregroundImage, Tree currentNode, List<Tree> foundContent)
		{
			Utils.RectData[] found = ConnectedComponents.TwoPass(ccLabelingBitmap, foregroundImage, currentNode, Utils.BACKGROUND);

			foreach (Utils.RectData rect in found)
			{
				if(rect != null){
					int hash = 17;
					for (int row = rect.Top; row <= rect.Bottom; row++)
					{
						for (int col = rect.Left; col <= rect.Right; col++)
						{
							hash = 31 * hash + image[row, col];
						}
					}

					BoundingBox bb = new BoundingBox(rect.Left, rect.Top, (rect.Right - rect.Left) + 1, (rect.Bottom - rect.Top) + 1);

					Dictionary<string,object> tags = new Dictionary<string, object>();
					tags.Add("type", "content");
					tags.Add("pixel_hash", hash);

					Tree node = Tree.FromBoundingBox(bb, tags);

					foundContent.Add(node);
				}
			}
		}
Exemplo n.º 4
0
        public Tree GetScreenshotAndCreateTree(int width, int height, System.Drawing.Bitmap bitmap,  Dictionary<string, object> tags)
        {

            CopyPixelsFromDrawingBitmap(bitmap, _currentBuffer);
            _currentFrame = Bitmap.FromPixels(bitmap.Width, bitmap.Height, _currentBuffer);

            // Perform diff
            //if (_currentFrame.Pixels.Equals(_previousFrame.Pixels))
            if (_currentFrame.Equals(_previousFrame))
            {
                return null;
            }

            IBoundingBox invalidated = GetDirtyRect(_previousFrame, _currentFrame);
            tags.Add("invalidated", invalidated);

            Tree root = Tree.FromPixels(_currentFrame, tags);

            //Swap the buffers we're writting to
            int[] tmp = _currentBuffer;
            _currentBuffer = _previousBuffer;
            _previousBuffer = tmp;


            //Set the previous bitmap
            _previousFrame = Bitmap.FromPixels(_currentFrame.Width, _currentFrame.Height, _previousBuffer);


            return root;
        }
Exemplo n.º 5
0
        public ViewablePrototypeItem(Ptype ptype, string library, IEnumerable<ImageAnnotation> positives, IEnumerable<ImageAnnotation> negatives, Bitmap bitmap = null)
        {


            if (bitmap != null)
            {
                IsContent = true;
                Image img = ToImage(bitmap);
                CapturedImage = img;
            }
            else
            {
                Guid = ptype.Id;

                PrototypeVisual = ptype;

            }

            Dictionary<string, Bitmap> screenshots = new Dictionary<string, Bitmap>();
            if (positives != null)
            {
                PositiveExamples = new ObservableCollection<Example>();
                foreach (ImageAnnotation img in positives)
                {
                    
                    if (!screenshots.ContainsKey(img.ImageId))
                        screenshots[img.ImageId] = AnnotationLibrary.GetImage(library, img.ImageId);
                     
                    Bitmap screenshot = screenshots[img.ImageId];


                    Image exampleImage = GetImageFromExample(screenshot, img.Region);
                    Example example = new Example(exampleImage, library, img);
                    PositiveExamples.Add(example);

                }
            }

            if (negatives != null)
            {

                NegativeExamples = new ObservableCollection<Example>();
                foreach (ImageAnnotation img in negatives)
                {
                    if (!screenshots.ContainsKey(img.ImageId))
                        screenshots[img.ImageId] = AnnotationLibrary.GetImage(library, img.ImageId);

                    Bitmap screenshot = screenshots[img.ImageId];


                    Image exampleImage = GetImageFromExample(screenshot, img.Region);
                    Example example = new Example(exampleImage, library, img);
                    NegativeExamples.Add(example);
                }
            }

            
        }
Exemplo n.º 6
0
		/// <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);
							}
						}
					}
				}
			}
		}
Exemplo n.º 7
0
        public static Image ToImage(Bitmap bitmap)
        {
            Image img = new Image();
            img.Source = ToBitmapSource(bitmap);
            img.SnapsToDevicePixels = true;
            img.Stretch = System.Windows.Media.Stretch.Uniform;
            RenderOptions.SetBitmapScalingMode(img, BitmapScalingMode.NearestNeighbor);

            return img;
        }
Exemplo n.º 8
0
        /// <summary>
        /// Returns the location where the pattern no longer matches the bitmap.
        /// </summary>
        /// <param name="bitmap">The bitmap to match against.</param>
        /// <param name="startCol">The starting column to match in the bitmap.</param>
        /// <param name="start">The starting row in the bitmap to match.</param>
        /// <param name="end">The ending row to match in the bitmap.</param>
        /// <returns>The index of the row where the pattern no longer matches.</returns>
        public  int MatchesVerticalUntil(Bitmap pattern, Bitmap bitmap, int startCol, int start, int end)
        {
            for (int row = start; row <= end; row++)
            {
                if (!RowMatches(bitmap, pattern, startCol, startCol + pattern.Width - 1, 0, row, (row - start) % pattern.Height))
                    return row;
            }

            return end + 1;
        }
Exemplo n.º 9
0
 /// <summary>
 /// Returns all of the features found within a bitmap.
 /// </summary>
 /// <param name="bitmap">The bitmap to search through.</param>
 /// <returns>All of the features found within the bitmap.</returns>
 public void Match(Prefab.Bitmap bitmap, ICollection <Tree> found)
 {
     for (int row = 0; row < bitmap.Height; row++)
     {
         for (int col = 0; col < bitmap.Width; col++)
         {
             root.GetMatches(bitmap, col, row, found);
         }
     }
 }
Exemplo n.º 10
0
        /// <summary>
        /// Returns true if the given pattern matches the row specified by the parameters.
        /// </summary>
        /// <param name="bitmap">The bimap to match against.</param>
        /// <param name="pattern">The pattern to match in the bitmap.</param>
        /// <param name="startCol">The start column in the bitmap.</param>
        /// <param name="endCol">The end column in the bitmap.</param>
        /// <param name="startColInPattern">The start column in the pattern.</param>
        /// <param name="rowInBitmap">The start row in the bitmap.</param>
        /// <param name="rowInPattern">The start row in the pattern.</param>
        /// <returns></returns>
        private static bool RowMatches(Bitmap bitmap, Bitmap pattern, int startCol, int endCol, int startColInPattern, int rowInBitmap, int rowInPattern)
        {
            for (int col = startCol, colInPattern = startColInPattern; col <= endCol; col++, colInPattern++)
            {
				if (bitmap[rowInBitmap, col] != pattern[rowInPattern, colInPattern])
                    return false;
            }

            return true;
        }
Exemplo n.º 11
0
        /// <summary>
        /// Returns true if the pattern matches the bitmap.
        /// </summary>
        /// <param name="bitmap">The bitmap to check against.</param>
        /// <param name="startCol">The starting column in the bitmap where the pattern will be checked.</param>
        /// <param name="start">The starting row in the bitmap where the pattern will be checked.</param>
        /// <param name="end">The ending column in the bitmap where the pattern will be checked.</param>
        /// <returns></returns>
        public  bool Matches(Bitmap pattern, Bitmap bitmap, int startCol, int start, int end)
        {
            for (int row = start; row <= end; row++)
            {
                if (!RowMatches(bitmap, pattern, startCol, startCol + pattern.Width - 1, 0, row, (row - start) % pattern.Height))
                    return false;
            }

            return true;
        }
Exemplo n.º 12
0
        public HelloWorldOverlay()
        {
            InitializeComponent();
            DataContext = this;

            TextLocations = new ObservableCollection<TextBlockRect>();
            BackgroundOverlayImage = new WriteableBitmap((int)System.Windows.SystemParameters.VirtualScreenWidth * 2,
                            (int)System.Windows.SystemParameters.VirtualScreenHeight * 2, 96, 96, PixelFormats.Bgra32, null);
            _backgroundBitmap = Bitmap.FromDimensions((int)SystemParameters.VirtualScreenHeight, (int)SystemParameters.VirtualScreenWidth);
            _update = new UpdateDel(UIThreadUpdate);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Returns true if the bitmap matches the pattern in the bitmap.
        /// </summary>
        /// <param name="bitmap">The bitmap to try to match</param>
        /// <param name="startRow">The starting row in the bitmap.</param>
        /// <param name="start">The starting column in the bitmap.</param>
        /// <param name="end">The ending column in the bitmap.</param>
        /// <returns></returns>
        public bool Matches(Bitmap pattern, Bitmap bitmap, int startRow, int start, int end)
        {

            for (int column = start; column <= end; column++)
            {
                if (!ColumnMatches(bitmap, pattern, startRow, startRow + pattern.Height - 1, 0, column, (column - start) % pattern.Width))
                    return false;
            }

            return true;
        }
Exemplo n.º 14
0
		public static Tree FromPixels(Bitmap image, Dictionary<string, object> tags){
			BoundingBox frame = new BoundingBox(0,0, image.Width, image.Height);
			Tree tree = new Tree();
			tree.Occurrence = frame;
			tree._tags = new Dictionary<string, object>(tags);
			tree._children = new List<Tree>();
			tree._tags.Add("capturedpixels", image);
			tree._tags.Add("type", "frame");

			return tree;
		}
Exemplo n.º 15
0
		/// <summary>
		/// Finds the unpredictable content using the backround regions of elements.
		/// </summary>
		/// <param name="node"></param>
		/// <param name="image"></param>
		/// <param name="background"></param>
		/// <param name="isForeground"></param>
		private static List<Tree> FindAndAddContent(InterpretArgs args, Tree node, Bitmap image, Bitmap isForeground){
			Ptype ptype = (Ptype)node["ptype"];
			List<Tree> allFound = new List<Tree>();
			if(ptype != null){
				ptype.Model.Finder.SetForeground(node, image, isForeground);
			}
			//recurse. if no siblings are overlapping (the majority case) then we can run each child in parallel.
//        if (!anyOverlapping(node.children())){
//            List<Future<ICollection<Tree>>> asyncResults = new List<Future<ICollection<Tree>>>();
//            for(final Tree child : node.children()){
//                Callable<ICollection<Tree>> callable = new Callable<ICollection<Tree>>(){
//                   public ICollection<Tree> call(){
//                        return findAndAddContent(args, ptype, child, image, isForeground);
//                  }
//                };
//                Future<ICollection<Tree>> results =  Utils.service.submit(callable);
//                asyncResults.add(results);
//            }
//
//            for(Future<ICollection<Tree>> res : asyncResults){
//
//                try {
//                    allFound.addAll(res.get());
//                } catch (InterruptedException e) {
//                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//                } catch (ExecutionException e) {
//                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//                }
//            }
//        }
//        else //we can't run children in parallel if any nodes overlap.
//        //nodes only overlap when there's a false positive. so we might be able
//        //to trigger something here and automatically correct that.
//        {
			foreach (Tree child in node.GetChildren())
				FindAndAddContent(args, child, image, isForeground);


			if(ptype != null){
				ptype.Model.Finder.FindContent(node, image, isForeground, allFound);
				foreach(Tree found  in allFound)
					args.SetAncestor(found, node);

                allFound.AddRange(node.GetChildren());
                PrototypeDetectionLayer.SetAncestorsByContainment(allFound, args);
			}



			// }

			return allFound;
		}
Exemplo n.º 16
0
 public static Bitmap GradientDown(Bitmap bmp)
 {
     Bitmap cpy = Bitmap.Crop(bmp, 1, 1,  bmp.Width - 2, bmp.Height - 2);
     for (int row = 1; row < bmp.Height - 1; row++)
     {
         for (int col = 1; col < bmp.Width - 1; col++)
         {
             double ly = -0.5 * bmp[ row, col] + 0.5 * bmp[row + 1, col];
             cpy[row - 1, col - 1] = (int)Math.Max(0, Math.Round(ly));
         }
     }
     return cpy;
 }
Exemplo n.º 17
0
        private ICollection <Tree> Match(Prefab.Bitmap bitmap,
                                         int top, int left, int bottomExclusive, int rightExclusive)
        {
            List <Tree> found = new List <Tree>();

            for (int row = top; row < bottomExclusive; row++)
            {
                for (int col = left; col < rightExclusive; col++)
                {
                    root.GetMatches(bitmap, col, row, found);
                }
            }

            return(found);
        }
Exemplo n.º 18
0
			public Feature Create(Bitmap bitmap)
			{
				foreach (Feature feature in features)
				{
					if (Bitmap.ExactlyMatches(bitmap, feature.Bitmap))
					{
						return feature;
					}
				}

				Feature f = new Feature(features.Count, bitmap);
				features.Add(f);

				return f;
			}
Exemplo n.º 19
0
		public static Utils.RectData[] TwoPass(Bitmap labels, Bitmap srcBitmap, Tree root, int background)
		{
			DisjointSets linked = new DisjointSets(root.Width * root.Height);
			int bottom = root.Top + root.Height;
			int right = root.Left + root.Width;

			int numelements = IdentifyComponents(labels, srcBitmap, linked, root, root.GetHashCode(), bottom, right, background);

			//Run the second pass through the image to label each component
			//and create bounding rectangles
			LabelComponentsAndSetBoundingBoxes(labels, linked, root.Top, root.Left, bottom, right, background);


			return GetRectsFromLabeledImage(labels, srcBitmap, numelements, root.Top, root.Left, bottom, right, background);
		}
Exemplo n.º 20
0
		private static void LabelComponentsAndSetBoundingBoxes(Bitmap labels, DisjointSets linked, int top, int left, int bottom, int right, int background)
		{
			//Run the second pass through the image to label each component
			//and create bounding rectangles
			for (int row = top; row < bottom; row++)
			{
				for (int col = left; col < right; col++)
				{
					if (labels[row, col] != background)
					{
						int id = linked.Find(labels[row, col]);
						labels[row, col] = id;
					}
				}
			}
		}
Exemplo n.º 21
0
        private void ThreadRun(object arg)
        {
            if (!_running)
            {
                _running = true;

                while (!_exitEvent.WaitOne(0, false))
                {
                    IntPtr windowHandle = (IntPtr)arg;
                    //Get the active window so we can translate it.
                    Tree window = _windowCapture.CaptureWindowWithPixels(windowHandle, UsePrintWindow, false);

                    if (window != null)
                    {
                        Prefab.Bitmap         capture = window["capturedpixels"] as Prefab.Bitmap;
                        System.Drawing.Bitmap bmp     = _pool.GetInstance(capture.Width, capture.Height);
                        Bitmap.ToSystemDrawingBitmap(capture, bmp);

                        // Get Window features
                        Win32.WINDOWINFO windowinfo = new Win32.WINDOWINFO(true);
                        Win32.GetWindowInfo(windowHandle, ref windowinfo);

                        // Save as png image
                        String filename = string.Format("{0}_f{1:D4}.png", _saveLoc.Substring(0, _saveLoc.Length - 4), frame_num);
                        bmp.Save(@filename, ImageFormat.Png);
                        frame_num++;


                        if (_videoStream == null)
                        {
                            _videoStream = _aviManager.AddVideoStream(false, 20, bmp);
                        }
                        else
                        {
                            _videoStream.AddFrame(bmp);
                        }

                        _pool.ReturnInstance(bmp);
                    }

                    //Let's not melt our processor ;)
                    Thread.Sleep(50);
                }
                _running = false;
                _aviManager.Close();
            }
        }
Exemplo n.º 22
0
		public static Bitmap CombineBitmapsAndMakeDifferencesTransparent(Bitmap b1, Bitmap b2)
		{
			Bitmap combined = Bitmap.FromDimensions(b1.Width, b1.Height);

			for (int row = 0; row < b1.Height; row++)
			{
				for (int col = 0; col < b1.Width; col++)
				{
					if (b1[row, col] != b2[row, col])
						combined[row, col] =  Feature.TRANSPARENT_VALUE;
					else
						combined[row, col] =  b1[row, col];
				}
			}

			return combined;
		}
Exemplo n.º 23
0
		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);
				}
			}
		}
Exemplo n.º 24
0
		public static bool MatchesIgnoringTransparentPixels(Bitmap b1, Bitmap b2){
			if (b1.Width != b2.Width || b1.Height != b2.Height)
				return false;

			for (int row = 0; row < b1.Height; row++)
			{
				for (int col = 0; col < b2.Width; col++){
					int b1pixel = b1[row,col];
					int b2pixel = b2[row,col];
					if (b1pixel != Feature.TRANSPARENT_VALUE
						&& b2pixel != Feature.TRANSPARENT_VALUE
						&& b1pixel != b2pixel)
						return false;
				}
			}

			return true;
		}
Exemplo n.º 25
0
		private static int IdentifyComponents(Bitmap labels, Bitmap srcBitmap, DisjointSets linked, Tree toProcess, int treeNodeIndex, int bottom, int right, int background)
		{
			int numElements = 0;

			//Run the first pass through the image to find any foreground content
			int[] neighbors = new int[8];
			for (int row = toProcess.Top; row < bottom; row++)
			{
				for (int col = toProcess.Left; col < right; col++)
				{
					int sourcepixel = srcBitmap[row, col];

					if (sourcepixel == treeNodeIndex)
					{
						int neighborCount = NeighborsConnected8(neighbors, toProcess, row, col, labels, background);
						if (neighborCount == 0)
						{

							labels[row, col] = numElements+1;
							numElements++;

						}
						else
						{
							int min = Min(neighbors, neighborCount);
							labels[row, col] =  min;
							for(int i = 0; i < neighborCount; i++)
							{
								linked.union(min, neighbors[i]);

							}
						}

					}
					else
						labels[row, col] = background;
				}
			}

			return numElements;

		}
Exemplo n.º 26
0
        public static Bitmap Gradient(Bitmap bmp)
        {
            Bitmap cpy = Bitmap.DeepCopy(bmp);

            for (int row = 1; row < cpy.Height - 1; row++)
            {
                for (int col = 1; col < cpy.Width - 1; col++)
                {

                    double lx = -0.5 * bmp[ row, col - 1] + 0.5 * bmp[ row, col + 1];
                    double ly = -0.5 * bmp[row - 1, col] + 0.5 * bmp[row + 1, col];
                    
                    
                    double grad = Math.Sqrt(lx * lx + ly * ly);
                    cpy[ row , col + 1] = (int)Math.Round(grad);
                }
            }

            return Bitmap.Crop(cpy, 1, 1, cpy.Width - 2, cpy.Height - 2);
        }
Exemplo n.º 27
0
        public static IBoundingBox GetDirtyRect(Bitmap previous, Bitmap current)
        {

            if (previous == null || previous.Width != current.Width || previous.Height != current.Height)
                return new BoundingBox(0, 0, current.Width, current.Height);

            List<RectData> rects = new List<RectData>();

            Parallel.For<RectData>(0, current.Height,
                () => new RectData(int.MaxValue, int.MaxValue, 0, 0),
                (row, loop, bb) =>
                {
                    for (int col = 0; col < current.Width; col++)
                    {
                        if (current[row, col] != previous[row, col])
                        {
                            if (row < bb.Top)
                                bb.Top = row;
                            if (col < bb.Left)
                                bb.Left = col;
                            if (col > bb.Right)
                                bb.Right = col;
                            if (row > bb.Bottom)
                                bb.Bottom = row;
                        }
                    }
                    return bb;
                },

                (bb) =>
                {
                    lock (((ICollection)rects).SyncRoot)
                        rects.Add(bb);
                }
                );

            IBoundingBox dirty = Union(rects);

            return dirty;
        }
Exemplo n.º 28
0
        public static Int32Rect SnapRect(Bitmap bitmap)
        {
            Bitmap grayscale = GrayscaleEdgeDetector.ConvertToGrayscale(bitmap, false);
            Bitmap up = GrayscaleEdgeDetector.GradientUp(grayscale);
            Bitmap right = GrayscaleEdgeDetector.GradientRight(grayscale);
            Bitmap down = GrayscaleEdgeDetector.GradientDown(grayscale);
            Bitmap left = GrayscaleEdgeDetector.GradientLeft(grayscale);

            int width = right.Width - 1;
            int height = up.Height - 1;

            Bitmap[] bmps = new Bitmap[] { up, right, down, left };

            List<Int32Rect> rects = FindRectsAllThreshs(bmps, new List<Prefab.Point>() { new Prefab.Point(bitmap.Width / 2, bitmap.Height / 2) },
                GrayscaleEdgeDetector.CountOfEachGradient(up));

            //double closest = double.PositiveInfinity;
            Int32Rect biggest = Int32Rect.Empty;
            foreach (Int32Rect rect in rects)
            {
                Console.WriteLine(rect.X + ", " + rect.Y + ", " + rect.Width + ", " + rect.Height);

                double area = rect.Width * rect.Height;
                if(area > biggest.Width * biggest.Height)
                {
                    biggest = rect;
                    
                }
                //double dist = Math.Sqrt((rect.X * rect.X) + (rect.Y * rect.Y) + 
                //    (rect.Width - bitmap.Width) * (rect.Width - bitmap.Width) +
                //    (rect.Height - bitmap.Height) * (rect.Height - bitmap.Height));
                //if (dist < closest)
                //{
                //    biggest = rect;
                //    closest = dist;
                //}
            }

            return biggest;
        }
Exemplo n.º 29
0
		public void GetMatches(Bitmap bitmap, int probeOffsetX, int probeOffsetY, ICollection<Tree> bucket)
		{
			int imageOffsetX, imageOffsetY;
			imageOffsetX = probeOffsetX + m_offsetToTest.X;
			imageOffsetY = probeOffsetY + m_offsetToTest.Y;
			//Point.Add(probeOffsetX, m_offsetToTest.X, probeOffsetY, m_offsetToTest.Y, out imageOffsetX, out imageOffsetY);

			if (imageOffsetX >= 0 && imageOffsetY >= 0
				&& imageOffsetY < bitmap.Height && imageOffsetX < bitmap.Width)
			{
				FeatureTreeNode child = null;
				bool has = m_childrenByColor.TryGetValue(bitmap[imageOffsetY, imageOffsetX], out child);
				if (has)
				{
					child.GetMatches(bitmap, probeOffsetX, probeOffsetY, bucket);
					if(m_transparentChild != null)
						m_transparentChild.GetMatches(bitmap, probeOffsetX, probeOffsetY, bucket);
					return;
				}
			}
			if(m_transparentChild != null)
				m_transparentChild.GetMatches(bitmap, probeOffsetX, probeOffsetY, bucket);
		}
Exemplo n.º 30
0
 void MultiThreadedMatch(Prefab.Bitmap bitmap, ICollection <Tree> found, int top, int left, int bottom, int right)
 {
     Parallel.For <List <Tree> >(top, bottom - 1, () => new List <Tree>(),
                                 (row, loop, list) =>
     {
         for (int col = left; col <= right; col++)
         {
             root.GetMatches(bitmap, col, row, list);
         }
         return(list);
     }
                                 ,
                                 (elmnts) =>
     {
         lock (((ICollection)found).SyncRoot)
         {
             foreach (Tree o in elmnts)
             {
                 found.Add(o);
             }
         }
     }
                                 );
 }
		private static void BitmapToJson(JObject dest, Bitmap bitmap){
			dest.Add("width", bitmap.Width );
			dest.Add("height", bitmap.Height);
			int[] pixels = bitmap.Pixels;

			byte[] bytes = new byte[pixels.Length * sizeof(int)];
			Buffer.BlockCopy (pixels, 0, bytes, 0, bytes.Length);

			string pixelstring = Convert.ToBase64String (bytes);


			dest.Add("pixels", pixelstring);

		}
Exemplo n.º 32
0
 private bool BitmapContainsOffset(Bitmap bmp, Prefab.Point location) {
     return location.Y < bmp.Height && location.X < bmp.Width && location.X >= 0 &&
         location.Y >= 0;
 }
Exemplo n.º 33
0
        private Bitmap[] CopyBitmaps(Bitmap[] bitmaps)
        {
            Bitmap[] cpy = new Bitmap[bitmaps.Length];
            for (int i = 0; i < cpy.Length; i++)
                cpy[i] = Prefab.Bitmap.DeepCopy(bitmaps[i]);

            return cpy;
        }
Exemplo n.º 34
0
        public static List<Int32Rect> FindRectsAllThreshs(Bitmap[] bitmaps, List<Prefab.Point> points, int[] histogram) {
            RectangleFinder r = new RectangleFinder();
            r.FoundRects = new List<Int32Rect>();
            r.Points = points;
            r.SetupBoundaryPoints();
            List<Int32Rect> foundRects = new List<Int32Rect>();
            for (int i = 0; i < 255; i += 1)
            {
                if (histogram[i] > 0)
                {
                    r.Bitmaps = r.CopyBitmaps(bitmaps);
                    r.Thresh = i;

                    int x, y;
                    x = r.FarthestLeftClick - 1;
                    y = points[0].Y;
                    r.Start = new Prefab.Point(x, y);
                    AddUnique(r.FindRects(), foundRects);
                }
            }
            return foundRects;
        }