Example #1
0
 public ColorFilter()
 {
     this.applyFilters = false;
     this.colorFilter = new YCbCrFiltering();
     this.grayFilter = new GrayscaleRMY();
     this.binaryFilter = new Threshold(1);
     this.erosionFilter = new BinaryErosion3x3();
     this.rgbFilter = new GrayscaleToRGB();
 }
        // Apply filter
        public Bitmap Apply(Bitmap srcImg)
        {
            // get source image size
            int width  = srcImg.Width;
            int height = srcImg.Height;

            // convert input to grayscale if it's not yet
            bool disposeSource = false;

            if (srcImg.PixelFormat != PixelFormat.Format8bppIndexed)
            {
                disposeSource = true;
                // create grayscale image
                IFilter filter = new GrayscaleRMY();
                srcImg = filter.Apply(srcImg);
            }

            // lock source bitmap data
            BitmapData srcData = srcImg.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);

            // create new image
            Bitmap dstImg = AForge.Imaging.Image.CreateGrayscaleImage(width, height);

            // lock destination bitmap data
            BitmapData dstData = dstImg.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            int stride = srcData.Stride;
            int offset = stride - width;
            int widthM1 = width - 1;
            int heightM1 = height - 1;
            int d, max;

            // do the job
            unsafe
            {
                byte *src = (byte *)srcData.Scan0.ToPointer();
                byte *dst = (byte *)dstData.Scan0.ToPointer();

                // skip one stride
                src += stride;
                dst += stride;

                // for each line
                for (int y = 1; y < heightM1; y++)
                {
                    src++;
                    dst++;

                    // for each pixel
                    for (int x = 1; x < widthM1; x++, src++, dst++)
                    {
                        max = 0;

                        // left diagonal
                        d = (int)src[-stride - 1] - src[stride + 1];
                        if (d < 0)
                        {
                            d = -d;
                        }
                        if (d > max)
                        {
                            max = d;
                        }
                        // right diagonal
                        d = (int)src[-stride + 1] - src[stride - 1];
                        if (d < 0)
                        {
                            d = -d;
                        }
                        if (d > max)
                        {
                            max = d;
                        }
                        // vertical
                        d = (int)src[-stride] - src[stride];
                        if (d < 0)
                        {
                            d = -d;
                        }
                        if (d > max)
                        {
                            max = d;
                        }
                        // horizontal
                        d = (int)src[-1] - src[1];
                        if (d < 0)
                        {
                            d = -d;
                        }
                        if (d > max)
                        {
                            max = d;
                        }

                        *dst = (byte)max;
                    }
                    src += offset + 1;
                    dst += offset + 1;
                }
            }
            // unlock both images
            dstImg.UnlockBits(dstData);
            srcImg.UnlockBits(srcData);

            if (disposeSource == true)
            {
                srcImg.Dispose();
            }

            return(dstImg);
        }
        // Apply filter
        public Bitmap Apply(Bitmap srcImg)
        {
            // get source image size
            int width  = srcImg.Width;
            int height = srcImg.Height;

            // convert input to grayscale if it's not yet
            bool disposeSource = false;

            if (srcImg.PixelFormat != PixelFormat.Format8bppIndexed)
            {
                disposeSource = true;
                // create grayscale image
                IFilter filter = new GrayscaleRMY();
                srcImg = filter.Apply(srcImg);
            }

            // lock source bitmap data
            BitmapData srcData = srcImg.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);

            // create new image
            Bitmap dstImg = AForge.Imaging.Image.CreateGrayscaleImage(width, height);

            // lock destination bitmap data
            BitmapData dstData = dstImg.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            int    stride = srcData.Stride;
            int    offset = stride - width;
            int    widthM1 = width - 1;
            int    heightM1 = height - 1;
            int    i, j, ir;
            double v, gx, gy, g, max = 0;

            // do the job
            unsafe
            {
                byte *src = (byte *)srcData.Scan0.ToPointer() + stride;
                byte *dst = (byte *)dstData.Scan0.ToPointer() + stride;

                // for each line
                for (int y = 1; y < heightM1; y++)
                {
                    src++;
                    dst++;

                    // for each pixel
                    for (int x = 1; x < widthM1; x++, src++, dst++)
                    {
                        gx = gy = 0;
                        // for each kernel row
                        for (i = 0; i < 3; i++)
                        {
                            ir = i - 1;
                            // for each kernel column
                            for (j = 0; j < 3; j++)
                            {
                                // source value
                                v = src[ir * stride + j - 1];

                                gx += v * xKernel[i, j];
                                gy += v * yKernel[i, j];
                            }
                        }
                        // get gradient value
//						g = Math.Min(Math.Sqrt(gx * gx + gy * gy), 255);
                        g = Math.Min(Math.Abs(gx) + Math.Abs(gy), 255);                         // approximation
                        if (g > max)
                        {
                            max = g;
                        }
                        *dst = (byte)g;
                    }
                    src += (offset + 1);
                    dst += (offset + 1);
                }

                // do we need scaling
                if ((scaleIntensity) && (max != 255))
                {
                    // make the second pass for intensity scaling
                    double factor = 255.0 / (double)max;
                    dst = (byte *)dstData.Scan0.ToPointer() + stride;

                    // for each line
                    for (int y = 1; y < heightM1; y++)
                    {
                        dst++;
                        // for each pixel
                        for (int x = 1; x < widthM1; x++, dst++)
                        {
                            *dst = (byte)(factor * *dst);
                        }
                        dst += (offset + 1);
                    }
                }
            }
            // unlock both images
            dstImg.UnlockBits(dstData);
            srcImg.UnlockBits(srcData);

            if (disposeSource == true)
            {
                srcImg.Dispose();
            }

            return(dstImg);
        }
        protected int x, y; // current position

        #endregion Fields

        #region Methods

        // Apply filter
        public Bitmap Apply(Bitmap srcImg)
        {
            // get source image size
            width = srcImg.Width;
            height = srcImg.Height;

            // temp bitmap
            Bitmap tmpImg;

            if (srcImg.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                // clone bitmap
                tmpImg = AForge.Imaging.Image.Clone(srcImg);
            }
            else
            {
                // create grayscale image
                IFilter filter = new GrayscaleRMY();
                tmpImg = filter.Apply(srcImg);
            }

            // lock temp bitmap data
            BitmapData tmpData = tmpImg.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);

            // create new grayscale image
            Bitmap dstImg = AForge.Imaging.Image.CreateGrayscaleImage(width, height);

            // lock destination bitmap data
            BitmapData dstData = dstImg.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            stride		= dstData.Stride;
            widthM1		= width - 1;
            heightM1	= height - 1;

            int	offset = stride - width;
            int	v, e;

            // do the job
            unsafe
            {
                byte * src = (byte *) tmpData.Scan0.ToPointer();
                byte * dst = (byte *) dstData.Scan0.ToPointer();

                // for each line
                for (y = 0; y < height; y++)
                {
                    // for each pixels
                    for (x = 0; x < width; x++, src ++, dst ++)
                    {
                        v = *src;

                        // fill the next destination pixel
                        if (v < 128)
                        {
                            *dst = 0;
                            e = v;
                        }
                        else
                        {
                            *dst = 255;
                            e = v - 255;
                        }

                        Diffuse(e, src);
                    }
                    src += offset;
                    dst += offset;
                }
            }
            // unlock both images
            dstImg.UnlockBits(dstData);
            tmpImg.UnlockBits(tmpData);

            // dispose temp bitmap
            tmpImg.Dispose();

            return dstImg;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            IVideoSource videoSource1 = videoSourcePlayer1.VideoSource;
            Bitmap currentVideoFrame = videoSourcePlayer1.GetCurrentVideoFrame();
            pictureBox1.Image = currentVideoFrame;

            if (currentVideoFrame != null)
            {
                Crop filter1 = new Crop(new Rectangle(1, 1, 319, 479));
                Crop filter2 = new Crop(new Rectangle(321, 1, 639, 479));
                Bitmap leftimage = filter1.Apply(currentVideoFrame);
                Bitmap rightimage = filter2.Apply(currentVideoFrame);

                // get grayscale image
                IFilter grayscaleFilter = new GrayscaleRMY();
                leftimage = grayscaleFilter.Apply(leftimage);
                rightimage = grayscaleFilter.Apply(rightimage);

                // apply threshold filter
                Threshold th = new Threshold(trackBar1.Value);
                Bitmap filteredImage1 = th.Apply(leftimage);
                pictureBox2.Image = filteredImage1;
                Bitmap filteredImage2 = th.Apply(rightimage);
                pictureBox3.Image = filteredImage2;
                label6.Text = trackBar1.Value.ToString();

                ImageStatistics lftstat = new ImageStatistics(filteredImage1);
                int lftpxlcntwthoutblck = lftstat.PixelsCountWithoutBlack;
                ImageStatistics rghtstat = new ImageStatistics(filteredImage2);
                int rghtpxlcntwthoutblck = rghtstat.PixelsCountWithoutBlack;

                int val = trackBar1.Value;

                if (((lftpxlcntwthoutblck - rghtpxlcntwthoutblck) > val) || ((rghtpxlcntwthoutblck - lftpxlcntwthoutblck) > val))
                     {
                        if ((lftpxlcntwthoutblck-rghtpxlcntwthoutblck) >val)
                            {
                                //label4.Text = "left";
                                label4.Text = "right";
                             }
                         if ((rghtpxlcntwthoutblck-lftpxlcntwthoutblck) >val)
                            {
                                //label4.Text = "right";
                                label4.Text = "left";
                            }
                     }
                else if ((lftpxlcntwthoutblck == 0) && (rghtpxlcntwthoutblck == 0))
                {
                    label4.Text = "Stop!! No  space ahead";
                }
                else    {
                    label4.Text = "Forward";

                        }

               }
        }
        // Apply filter
        public Bitmap Apply(Bitmap srcImg)
        {
            // get source image size
            int width = srcImg.Width;
            int height = srcImg.Height;

            // convert input to grayscale if it's not yet
            bool disposeSource = false;
            if (srcImg.PixelFormat != PixelFormat.Format8bppIndexed)
            {
                disposeSource = true;
                // create grayscale image
                IFilter filter = new GrayscaleRMY();
                srcImg = filter.Apply(srcImg);
            }

            // lock source bitmap data
            BitmapData srcData = srcImg.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);

            // create new image
            Bitmap dstImg = AForge.Imaging.Image.CreateGrayscaleImage(width, height);

            // lock destination bitmap data
            BitmapData dstData = dstImg.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            int stride = srcData.Stride;
            int offset = stride - width;
            int	widthM1 = width - 1;
            int heightM1 = height - 1;
            int d, max, v;

            // do the job
            unsafe
            {
                byte * src = (byte *) srcData.Scan0.ToPointer();
                byte * dst = (byte *) dstData.Scan0.ToPointer();

                // skip one stride
                src += stride;
                dst += stride;

                // for each line
                for (int y = 1; y < heightM1; y++)
                {
                    src ++;
                    dst ++;

                    // for each pixel
                    for (int x = 1; x <	widthM1; x++, src ++, dst ++)
                    {
                        max = 0;
                        v = *src;

                        // top-left
                        d = v - src[-stride - 1];
                        if (d < 0)
                            d = -d;
                        if (d > max)
                            max = d;
                        // top
                        d = v - src[-stride];
                        if (d < 0)
                            d = -d;
                        if (d > max)
                            max = d;
                        // top-right
                        d = v - src[-stride + 1];
                        if (d < 0)
                            d = -d;
                        if (d > max)
                            max = d;
                        // left
                        d = v - src[-1];
                        if (d < 0)
                            d = -d;
                        if (d > max)
                            max = d;
                        // right
                        d = v - src[1];
                        if (d < 0)
                            d = -d;
                        if (d > max)
                            max = d;
                        // bottom-left
                        d = v - src[stride - 1];
                        if (d < 0)
                            d = -d;
                        if (d > max)
                            max = d;
                        // bottom
                        d = v - src[stride];
                        if (d < 0)
                            d = -d;
                        if (d > max)
                            max = d;
                        // bottom-right
                        d = v - src[stride + 1];
                        if (d < 0)
                            d = -d;
                        if (d > max)
                            max = d;

                        *dst = (byte) max;
                    }
                    src += offset + 1;
                    dst += offset + 1;
                }
            }
            // unlock both images
            dstImg.UnlockBits(dstData);
            srcImg.UnlockBits(srcData);

            if (disposeSource == true)
            {
                srcImg.Dispose();
            }

            return dstImg;
        }
        // Apply filter
        public Bitmap Apply(Bitmap srcImg)
        {
            // get source image size
            int width = srcImg.Width;
            int height = srcImg.Height;

            // convert input to grayscale if it's not yet
            bool disposeSource = false;
            if (srcImg.PixelFormat != PixelFormat.Format8bppIndexed)
            {
                disposeSource = true;
                // create grayscale image
                IFilter filter = new GrayscaleRMY();
                srcImg = filter.Apply(srcImg);
            }

            // lock source bitmap data
            BitmapData srcData = srcImg.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);

            // create new image
            Bitmap dstImg = AForge.Imaging.Image.CreateGrayscaleImage(width, height);

            // lock destination bitmap data
            BitmapData dstData = dstImg.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            int stride = srcData.Stride;
            int offset = stride - width;
            int	widthM1 = width - 1;
            int heightM1 = height - 1;
            int i, j, ir;
            double	v, gx, gy, g, max = 0;

            // do the job
            unsafe
            {
                byte * src = (byte *) srcData.Scan0.ToPointer() + stride;
                byte * dst = (byte *) dstData.Scan0.ToPointer() + stride;

                // for each line
                for (int y = 1; y < heightM1; y ++)
                {
                    src++;
                    dst++;

                    // for each pixel
                    for (int x = 1; x <	widthM1; x++, src ++, dst ++)
                    {
                        gx = gy = 0;
                        // for each kernel row
                        for (i = 0; i < 3; i++)
                        {
                            ir = i - 1;
                            // for each kernel column
                            for (j = 0; j < 3; j++)
                            {
                                // source value
                                v = src[ir * stride + j - 1];

                                gx += v * xKernel[i, j];
                                gy += v * yKernel[i, j];
                            }
                        }
                        // get gradient value
            //						g = Math.Min(Math.Sqrt(gx * gx + gy * gy), 255);
                        g = Math.Min(Math.Abs(gx) + Math.Abs(gy), 255);	// approximation
                        if (g > max)
                            max = g;
                        *dst = (byte) g;
                    }
                    src += (offset + 1);
                    dst += (offset + 1);
                }

                // do we need scaling
                if ((scaleIntensity) && (max != 255))
                {
                    // make the second pass for intensity scaling
                    double factor = 255.0 / (double) max;
                    dst = (byte *) dstData.Scan0.ToPointer() + stride;

                    // for each line
                    for (int y = 1; y < heightM1; y ++)
                    {
                        dst++;
                        // for each pixel
                        for (int x = 1; x <	widthM1; x++, dst ++)
                        {
                            *dst = (byte) (factor * *dst);
                        }
                        dst += (offset + 1);
                    }
                }
            }
            // unlock both images
            dstImg.UnlockBits(dstData);
            srcImg.UnlockBits(srcData);

            if (disposeSource == true)
            {
                srcImg.Dispose();
            }

            return dstImg;
        }
        // Apply filter
        public Bitmap Apply(Bitmap srcImg)
        {
            // get source image size
            width  = srcImg.Width;
            height = srcImg.Height;

            // temp bitmap
            Bitmap tmpImg;

            if (srcImg.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                // clone bitmap
                tmpImg = AForge.Imaging.Image.Clone(srcImg);
            }
            else
            {
                // create grayscale image
                IFilter filter = new GrayscaleRMY();
                tmpImg = filter.Apply(srcImg);
            }

            // lock temp bitmap data
            BitmapData tmpData = tmpImg.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);

            // create new grayscale image
            Bitmap dstImg = AForge.Imaging.Image.CreateGrayscaleImage(width, height);

            // lock destination bitmap data
            BitmapData dstData = dstImg.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            stride   = dstData.Stride;
            widthM1  = width - 1;
            heightM1 = height - 1;

            int offset = stride - width;
            int v, e;

            // do the job
            unsafe
            {
                byte *src = (byte *)tmpData.Scan0.ToPointer();
                byte *dst = (byte *)dstData.Scan0.ToPointer();

                // for each line
                for (y = 0; y < height; y++)
                {
                    // for each pixels
                    for (x = 0; x < width; x++, src++, dst++)
                    {
                        v = *src;

                        // fill the next destination pixel
                        if (v < 128)
                        {
                            *dst = 0;
                            e = v;
                        }
                        else
                        {
                            *dst = 255;
                            e = v - 255;
                        }

                        Diffuse(e, src);
                    }
                    src += offset;
                    dst += offset;
                }
            }
            // unlock both images
            dstImg.UnlockBits(dstData);
            tmpImg.UnlockBits(tmpData);

            // dispose temp bitmap
            tmpImg.Dispose();

            return(dstImg);
        }
Example #9
0
        // Apply filter
        public Bitmap Apply(Bitmap srcImg)
        {
            // get source image size
            int width  = srcImg.Width;
            int height = srcImg.Height;

            // convert input to grayscale if it's not yet
            bool disposeSource = false;

            if (srcImg.PixelFormat != PixelFormat.Format8bppIndexed)
            {
                disposeSource = true;
                // create grayscale image
                IFilter filter = new GrayscaleRMY();
                srcImg = filter.Apply(srcImg);
            }

            // lock source bitmap data
            BitmapData srcData = srcImg.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);

            // create new image
            Bitmap dstImg = AForge.Imaging.Image.CreateGrayscaleImage(width, height);

            // lock destination bitmap data
            BitmapData dstData = dstImg.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            int stride = srcData.Stride;
            int offset = stride - width;

            int    widthM1 = width - 1;
            int    heightM1 = height - 1;
            double ex, ey, weight, weightTotal = 0, total = 0;

            // do the job
            unsafe
            {
                byte *src = (byte *)srcData.Scan0.ToPointer() + stride;
                byte *dst = (byte *)dstData.Scan0.ToPointer();

                // --- 1st pass - collecting statistics

                // for each line
                for (int y = 1; y < heightM1; y++)
                {
                    src++;
                    // for each pixels
                    for (int x = 1; x < widthM1; x++, src++)
                    {
                        // the equations are:
                        // ex = I(x + 1, y) - I(x - 1, y)
                        // ey = I(x, y + 1) - I(x, y - 1)
                        // weight = max(ex, ey)
                        // weightTotal += weight
                        // total += weight * I(x, y)

                        ex           = src[1] - src[-1];
                        ey           = src[stride] - src[-stride];
                        weight       = (ex > ey) ? ex : ey;
                        weightTotal += weight;
                        total       += weight * (*src);
                    }
                    src += offset + 1;
                }

                // calculate threshold
                threshold = (weightTotal == 0) ? (byte)0 : (byte)(total / weightTotal);

                // --- 2nd pass - thresholding
                src = (byte *)srcData.Scan0.ToPointer();

                // for each line
                for (int y = 0; y < height; y++)
                {
                    // for all pixels
                    for (int x = 0; x < width; x++, src++, dst++)
                    {
                        *dst = (byte)((*src <= threshold) ? 0 : 255);
                    }
                    src += offset;
                    dst += offset;
                }
            }
            // unlock both images
            dstImg.UnlockBits(dstData);
            srcImg.UnlockBits(srcData);

            if (disposeSource == true)
            {
                srcImg.Dispose();
            }

            return(dstImg);
        }
Example #10
0
        // Apply filter
        public Bitmap Apply(Bitmap srcImg)
        {
            // get source image size
            int width = srcImg.Width;
            int height = srcImg.Height;

            // convert input to grayscale if it's not yet
            bool disposeSource = false;
            if (srcImg.PixelFormat != PixelFormat.Format8bppIndexed)
            {
                disposeSource = true;
                // create grayscale image
                IFilter filter = new GrayscaleRMY();
                srcImg = filter.Apply(srcImg);
            }

            // lock source bitmap data
            BitmapData srcData = srcImg.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);

            // create new image
            Bitmap dstImg = AForge.Imaging.Image.CreateGrayscaleImage(width, height);

            // lock destination bitmap data
            BitmapData dstData = dstImg.LockBits(
                new Rectangle(0, 0, width, height),
                ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            int stride = srcData.Stride;
            int offset = stride - width;

            int widthM1 = width - 1;
            int heightM1 = height - 1;
            double ex, ey, weight, weightTotal = 0, total = 0;

            // do the job
            unsafe
            {
                byte * src = (byte *) srcData.Scan0.ToPointer() + stride;
                byte * dst = (byte *) dstData.Scan0.ToPointer();

                // --- 1st pass - collecting statistics

                // for each line
                for (int y = 1; y < heightM1; y++)
                {
                    src++;
                    // for each pixels
                    for (int x = 1; x < widthM1; x++, src++)
                    {
                        // the equations are:
                        // ex = I(x + 1, y) - I(x - 1, y)
                        // ey = I(x, y + 1) - I(x, y - 1)
                        // weight = max(ex, ey)
                        // weightTotal += weight
                        // total += weight * I(x, y)

                        ex = src[1] - src[-1];
                        ey = src[stride] - src[-stride];
                        weight = (ex > ey) ? ex : ey;
                        weightTotal += weight;
                        total += weight * (*src);
                    }
                    src += offset + 1;
                }

                // calculate threshold
                threshold = (weightTotal == 0) ? (byte) 0 : (byte) (total / weightTotal);

                // --- 2nd pass - thresholding
                src = (byte *) srcData.Scan0.ToPointer();

                // for each line
                for (int y = 0; y < height; y++)
                {
                    // for all pixels
                    for (int x = 0; x < width; x++, src++, dst++)
                    {
                        *dst = (byte) ((*src <= threshold) ? 0 : 255);
                    }
                    src += offset;
                    dst += offset;
                }
            }
            // unlock both images
            dstImg.UnlockBits(dstData);
            srcImg.UnlockBits(srcData);

            if (disposeSource == true)
            {
                srcImg.Dispose();
            }

            return dstImg;
        }