Esempio n. 1
0
        /// <summary>
        /// Recieves a black and white image to start
        /// extracting the blobs from it
        /// </summary>
        /// <param name="bitmap">Black and white image</param>
        /// <returns>Image with colored blobs</returns>
        public static Bitmap Execute(Bitmap bitmap)
        {
            int   x, y, blobIndex;
            Blob  tmpBlob;
            Color blobColor;

            MyRender.SetImage(bitmap);                      // Instatiates image pointers
            Point pixelPt;

            random = new Random();
            blobs  = new List <Blob>();
            names  = (KnownColor[])Enum.GetValues(typeof(KnownColor));

            bmData = MyRender.Image.LockBits(new Rectangle(0, 0, MyRender.Width, MyRender.Height), // Locks image memory
                                             ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);

            pBase = (byte *)bmData.Scan0.ToPointer();       // Pointer of image in memory
            pixel = PixelAt(0, 0);                          // Retrieves pointer pixel X,Y (0,0)

            mu      = 0;
            pixelPt = new Point();
            for (y = 1; y < bmData.Height - 1; y++)
            {
                pixel = PixelAt(0, y);
                for (x = 1; x < bmData.Width - 1; x++)
                {
                    if (pixel->R == 0 && pixel->G == 0 && pixel->B == 0)    // If pixel is black, starts a blob
                    {
                        pointList = new List <Point>();
                        blobColor = RandomColor();

                        minX = int.MaxValue;
                        minY = int.MaxValue;

                        maxX = int.MinValue;
                        maxY = int.MinValue;

                        //Grabs blob
                        pixelPt.X = x;
                        pixelPt.Y = y;

                        pointList.Add(pixelPt);

                        blobIndex = 0;
                        while (blobIndex < pointList.Count)
                        {
                            TrackBlob(pointList[blobIndex], blobColor);
                            blobIndex++;
                        }

                        if (pointList.Count > 3)
                        {
                            tmpBlob = new Blob(pointList, blobColor, new Point(minX, minY), new Point(maxX, maxY));
                            if (!tmpBlob.Undefined)
                            {
                                blobs.Add(tmpBlob);
                                mu += tmpBlob.Points.Count;
                            }
                        }
                    }
                    pixel++;
                }
            }
            mu = mu / blobs.Count;

            MyRender.Image.UnlockBits(bmData);

            return(MyRender.Image);
        }
Esempio n. 2
0
        /// <summary>
        /// It computes the matrix with the values of
        /// the integral image from a given bitmap
        /// </summary>
        /// <param name="bitmap">the bitmap wich it computes its integral values</param>
        /// <returns></returns>
        public static Bitmap Execute(Bitmap bitmap)
        {
            if (bitmap == null)
            {
                return(bitmap);
            }

            float  value;
            double total;

            MyRender.SetImage(bitmap);
            CanvasIMG.SetImage(bitmap);

            Init(bitmap);

            iimg = new float[bitmap.Width, bitmap.Height];

            value = (((pixel->R * VAL_R) + (pixel->G * VAL_G) + (pixel->B * VAL_B)));
            total = value / MY_BYTE;

            iimg[0, 0] = (float)total;

            for (Xi = 1; Xi < bitmap.Width; Xi++)
            {
                value = (((pixel->R * VAL_R) + (pixel->G * VAL_G) + (pixel->B * VAL_B)));
                total = value / MY_BYTE;

                if (total > 0)
                {
                    iimg[Xi, 0] += (float)total + iimg[Xi - 1, 0];
                }
                pixel++;
            }

            pixel = PixelAt(0, 0, bitmap.Width);

            value = (((pixel->R * VAL_R) + (pixel->G * VAL_G) + (pixel->B * VAL_B)));
            total = value / MY_BYTE;

            for (Yi = 1; Yi < bitmap.Height; Yi++)
            {
                value = (((pixel->R * VAL_R) + (pixel->G * VAL_G) + (pixel->B * VAL_B)));
                total = value / MY_BYTE;

                if (total > 0)
                {
                    iimg[0, Yi] = (float)total + iimg[0, Yi - 1];
                }
                pixel += bitmap.Width;
            }


            for (Yi = 1; Yi < bitmap.Height; Yi++)
            {
                pixel = PixelAt(1, Yi, bitmap.Width);
                for (Xi = 1; Xi < bitmap.Width; Xi++)
                {
                    value = (((pixel->R * VAL_R) + (pixel->G * VAL_G) + (pixel->B * VAL_B)));
                    total = value / MY_BYTE;

                    iimg[Xi, Yi] += (float)total + (iimg[Xi - 1, Yi] - iimg[Xi - 1, Yi - 1]) + iimg[Xi, Yi - 1];

                    pixel++;
                }
            }

            bitmap.UnlockBits(bmData);

            return(bitmap);
        }