Page 1 of 1

how to control mouse cursor with color tracking using Aforge

PostPosted: Sun Jan 28, 2018 12:48 pm
by mrvturan96
Hi,
I want to move the mouse cursor according to the color of the object. I could object tracking but I could not control the Mouse. How can I do this?
Code: Select all
public void nesnebul(Bitmap image)
        {

            BlobCounter blobCounter = new BlobCounter();
            blobCounter.MinWidth = 5;
            blobCounter.MinHeight = 5;
            blobCounter.FilterBlobs = true;
            blobCounter.ObjectsOrder = ObjectsOrder.Size;

            BitmapData objectsData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat);
            Grayscale grayscaleFilter = new Grayscale(0.2125, 0.7154, 0.0721);
            UnmanagedImage grayImage = grayscaleFilter.Apply(new UnmanagedImage(objectsData));
            image.UnlockBits(objectsData);
            blobCounter.ProcessImage(image);
            Rectangle[] rects = blobCounter.GetObjectsRectangles();
            Blob[] blobs = blobCounter.GetObjectsInformation();
            islemBox.Image = image;



            foreach (Rectangle recs in rects)
            {

                if (rects.Length > 0)
                {
                    Rectangle objectRect = rects[0];

                    Graphics g = kaynakBox.CreateGraphics();
                    using (Pen pen = new Pen(Color.FromArgb(250, 0, 0), 2))
                    {
                        g.DrawRectangle(pen, objectRect);
                    }

                    int objectX = objectRect.X + (objectRect.Width / 2);
                    int objectY = objectRect.Y + (objectRect.Height / 2);


                    this.Cursor  = new Cursor(Cursor.Current.Handle);
                    Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);

                    int xX = cu.posX(kaynakBox.Width, objectRect.X, objectRect.Width);
                    int yY = cu.posY(kaynakBox.Height, objectRect.Y, objectRect.Height);


                    String area = "";

                    if (objectX < 108 && objectY < 90)
                    {
                        area = "1.Bölge";
                       

                    }
                    else if ((objectX > 108 && objectX < 212) && (objectY < 90))
                    {
                        area = "2.Bölge";
                       
                    }
                    else if ((objectX > 212 && objectX < 360) && (objectY < 90))
                    {
                        area = "3.Bölge";
                       

                    }

Re: how to control mouse cursor with color tracking using Af

PostPosted: Sun Jan 28, 2018 9:57 pm
by andrew.kirillov
Hello,

AForge.NET Framework provides image processing tools to detect objects of certain colors. This is what you already did. However, AForge.NET Framework was never targeted to controlling mouse cursors - it is out of scope for the framework. So you may need to google for the solution.

Ask an image processing question, if you have one. This was the aim of the discussion board.