/// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            Bitmap sourceImage = null;

            DA.GetData(0, ref sourceImage);

            Bitmap RChannel = null;

            DA.GetData(1, ref RChannel);
            Bitmap GChannel = null;

            DA.GetData(2, ref GChannel);
            Bitmap BChannel = null;

            DA.GetData(3, ref BChannel);
            Bitmap AChannel = null;

            DA.GetData(4, ref AChannel);

            sourceImage = ImageUtil.convert(sourceImage, PixelFormat.Format32bppArgb);

            if (RChannel != null)
            {
                RChannel = ImageUtil.convert(RChannel, PixelFormat.Format32bppArgb);
                RChannel = Grayscale.CommonAlgorithms.RMY.Apply(RChannel);
                ReplaceChannel myFilter = new ReplaceChannel(RGB.R, RChannel);
                sourceImage = myFilter.Apply(sourceImage);
            }

            if (GChannel != null)
            {
                GChannel = ImageUtil.convert(GChannel, PixelFormat.Format32bppArgb);
                GChannel = Grayscale.CommonAlgorithms.RMY.Apply(GChannel);
                ReplaceChannel myFilter = new ReplaceChannel(RGB.G, GChannel);
                sourceImage = myFilter.Apply(sourceImage);
            }

            if (BChannel != null)
            {
                BChannel = ImageUtil.convert(BChannel, PixelFormat.Format32bppArgb);
                BChannel = Grayscale.CommonAlgorithms.RMY.Apply(BChannel);
                ReplaceChannel myFilter = new ReplaceChannel(RGB.B, BChannel);
                sourceImage = myFilter.Apply(sourceImage);
            }

            if (AChannel != null)
            {
                AChannel = ImageUtil.convert(AChannel, PixelFormat.Format32bppArgb);
                AChannel = Grayscale.CommonAlgorithms.RMY.Apply(AChannel);
                ReplaceChannel myFilter = new ReplaceChannel(RGB.A, AChannel);
                sourceImage = myFilter.Apply(sourceImage);
            }

            DA.SetData(0, sourceImage);
        }
        private void process_button_Click(object sender, EventArgs e)
        {
            //changes are made here

            //Doing classic image layer by layer
            //modifying image layer by layer
            var extractRGBchannelFilter = new ExtractChannel();

            extractRGBchannelFilter.Channel = AForge.Imaging.RGB.G;//RGB.R RGB.B
            var redChannel       = extractRGBchannelFilter.Apply(_inputImage);
            var threshold        = new Threshold(150);
            var thresholdedImage = threshold.Apply(redChannel);

            var replacedFilter = new ReplaceChannel(AForge.Imaging.RGB.G, thresholdedImage);
            var replacedImage  = replacedFilter.Apply(_inputImage);

            pictureBoxOutput.Image = replacedImage;
        }
Example #3
0
        public static Bitmap Sketch(Bitmap input, int redThreshold, int blueThreshold, int greenThreshold)
        {
            int[,] kernel = {
                { 1, 2, 1, },  
               { 2, 4, 2, },   
               { 1, 2, 1, },
            };
            Convolution convultionFilters = new Convolution(kernel);
            Threshold thresholdFilter = new Threshold(redThreshold);
            input = convultionFilters.Apply(input);
            // extract red channel
            ExtractChannel extractFilter = new ExtractChannel(RGB.R);
            Bitmap channel = extractFilter.Apply(input);
            // threshold channel
            thresholdFilter.ApplyInPlace(channel);
            // put the channel back
            ReplaceChannel replaceFilter = new ReplaceChannel(RGB.R, channel);
            replaceFilter.ApplyInPlace(input);

            // extract blue channel
            extractFilter = new ExtractChannel(RGB.B);
            channel = extractFilter.Apply(input);
            // threshold channel
            thresholdFilter.ThresholdValue = blueThreshold;
            thresholdFilter.ApplyInPlace(channel);
            // put the channel back
            replaceFilter = new ReplaceChannel(RGB.B, channel);
            replaceFilter.ApplyInPlace(input);
            
            // extract green channel
            extractFilter = new ExtractChannel(RGB.G);
            channel = extractFilter.Apply(input);
            // threshold channel
            thresholdFilter.ThresholdValue = greenThreshold;
            thresholdFilter.ApplyInPlace(channel);
            // put the channel back
            replaceFilter = new ReplaceChannel(RGB.G, channel);
            replaceFilter.ApplyInPlace(input);

            return input;
        }
Example #4
0
        public mSwapRGB(Bitmap BaseBitmap, int A, int R, int G, int B)
        {
            ExtractChannel cR = new ExtractChannel(RGB.R);
            ExtractChannel cG = new ExtractChannel(RGB.G);
            ExtractChannel cB = new ExtractChannel(RGB.B);

            ExtractChannel cA = new ExtractChannel(RGB.A);

            YCbCrExtractChannel cL = new YCbCrExtractChannel(YCbCr.YIndex);

            List <Bitmap> maps = new List <Bitmap>();

            Bitmap BmpA = new Bitmap(BaseBitmap.Width, BaseBitmap.Height, PixelFormat.Format16bppGrayScale);
            Bitmap BmpR = new Bitmap(BaseBitmap.Width, BaseBitmap.Height, PixelFormat.Format16bppGrayScale);
            Bitmap BmpG = new Bitmap(BaseBitmap.Width, BaseBitmap.Height, PixelFormat.Format16bppGrayScale);
            Bitmap BmpB = new Bitmap(BaseBitmap.Width, BaseBitmap.Height, PixelFormat.Format16bppGrayScale);
            Bitmap BmpL = new Bitmap(BaseBitmap.Width, BaseBitmap.Height, PixelFormat.Format16bppGrayScale);

            BmpA = cA.Apply(new Bitmap(BaseBitmap));
            BmpR = cR.Apply(new Bitmap(BaseBitmap));
            BmpG = cG.Apply(new Bitmap(BaseBitmap));
            BmpB = cB.Apply(new Bitmap(BaseBitmap));
            BmpL = cL.Apply(new Bitmap(BaseBitmap));

            maps.Add(BmpA);
            maps.Add(BmpR);
            maps.Add(BmpG);
            maps.Add(BmpB);
            maps.Add(BmpL);

            Bitmap bmp = new Bitmap(BaseBitmap.Width, BaseBitmap.Height, PixelFormat.Format32bppArgb);

            bmp.MakeTransparent();

            bmp = new ReplaceChannel(RGB.A, maps[A]).Apply(bmp);
            bmp = new ReplaceChannel(RGB.R, maps[R]).Apply(bmp);
            bmp = new ReplaceChannel(RGB.G, maps[G]).Apply(bmp);
            bmp = new ReplaceChannel(RGB.B, maps[B]).Apply(bmp);

            ModifiedBitmap = new Bitmap(bmp);
        }
Example #5
0
        public static Bitmap RGBA_replaceChannel(Bitmap img, Bitmap RChannel, Bitmap GChannel, Bitmap BChannel, Bitmap AChannel)
        {
            Bitmap NewIMG = ImageUtil.convert(img, PixelFormat.Format32bppArgb);

            if (RChannel != null)
            {
                RChannel = ImageUtil.convert(RChannel, PixelFormat.Format32bppArgb);
                RChannel = Grayscale.CommonAlgorithms.RMY.Apply(RChannel);
                ReplaceChannel myFilter = new ReplaceChannel(RGB.R, RChannel);
                img = myFilter.Apply(img);
            }

            if (GChannel != null)
            {
                GChannel = ImageUtil.convert(GChannel, PixelFormat.Format32bppArgb);
                GChannel = Grayscale.CommonAlgorithms.RMY.Apply(GChannel);
                ReplaceChannel myFilter = new ReplaceChannel(RGB.G, GChannel);
                img = myFilter.Apply(img);
            }

            if (BChannel != null)
            {
                BChannel = ImageUtil.convert(BChannel, PixelFormat.Format32bppArgb);
                BChannel = Grayscale.CommonAlgorithms.RMY.Apply(BChannel);
                ReplaceChannel myFilter = new ReplaceChannel(RGB.B, BChannel);
                img = myFilter.Apply(img);
            }

            if (AChannel != null)
            {
                AChannel = ImageUtil.convert(AChannel, PixelFormat.Format32bppArgb);
                AChannel = Grayscale.CommonAlgorithms.RMY.Apply(AChannel);
                ReplaceChannel myFilter = new ReplaceChannel(RGB.A, AChannel);
                img = myFilter.Apply(img);
            }

            return(img);
        }
Example #6
0
        // Process new frame
        public void ProcessFrame(ref Bitmap image)
        {
            if (backgroundFrame == null)
            {
                // create initial backgroung image
                backgroundFrame = grayscaleFilter.Apply(image);

                // get image dimension
                width  = image.Width;
                height = image.Height;

                // just return for the first time
                return;
            }

            Bitmap tmpImage;

            // apply the grayscale file
            tmpImage = grayscaleFilter.Apply(image);

            // set backgroud frame as an overlay for difference filter
            differenceFilter.OverlayImage = backgroundFrame;

            // apply difference filter
            Bitmap tmpImage2 = differenceFilter.Apply(tmpImage);

            // lock the temporary image and apply some filters on the locked data
            bitmapData = tmpImage2.LockBits(new Rectangle(0, 0, width, height),
                                            ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            // threshold filter
            thresholdFilter.ApplyInPlace(bitmapData);
            // erosion filter
            Bitmap tmpImage3 = erosionFilter.Apply(bitmapData);

            // unlock temporary image
            tmpImage2.UnlockBits(bitmapData);
            tmpImage2.Dispose( );

            // calculate amount of changed pixels
            pixelsChanged = (calculateMotionLevel) ?
                            CalculateWhitePixels(tmpImage3) : 0;

            // dispose old background
            backgroundFrame.Dispose( );
            // set backgound to current
            backgroundFrame = tmpImage;

            // extract red channel from the original image
            Bitmap redChannel = extrachChannel.Apply(image);

            //  merge red channel with moving object
            mergeFilter.OverlayImage = tmpImage3;
            Bitmap tmpImage4 = mergeFilter.Apply(redChannel);

            redChannel.Dispose( );
            tmpImage3.Dispose( );

            // Updated from original example to support new AForge Libraries (Sumit)
            if (replaceChannel == null)
            {
                replaceChannel = new ReplaceChannel(RGB.R, tmpImage4);
            }
            else
            {
                replaceChannel.ChannelImage = tmpImage4;
            }

            Bitmap tmpImage5 = replaceChannel.Apply(image);

            tmpImage4.Dispose( );

            image.Dispose( );
            image = tmpImage5;
        }
        // Process new frame
        public void ProcessFrame(ref Bitmap image)
        {
            if (backgroundFrame == null)
            {
                // create initial backgroung image
                backgroundFrame = grayscaleFilter.Apply(image);

                // get image dimension
                width  = image.Width;
                height = image.Height;

                // just return for the first time
                return;
            }

            Bitmap tmpImage;

            // apply the the grayscale file
            tmpImage = grayscaleFilter.Apply(image);


            if (++counter == 2)
            {
                counter = 0;

                // move background towards current frame
                moveTowardsFilter.OverlayImage = tmpImage;
                moveTowardsFilter.ApplyInPlace(backgroundFrame);
            }

            // set backgroud frame as an overlay for difference filter
            differenceFilter.OverlayImage = backgroundFrame;

            // lock temporary image to apply several filters
            bitmapData = tmpImage.LockBits(new Rectangle(0, 0, width, height),
                                           ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

            // apply difference filter
            differenceFilter.ApplyInPlace(bitmapData);
            // apply threshold filter
            thresholdFilter.ApplyInPlace(bitmapData);

            // calculate amount of changed pixels
            pixelsChanged = (calculateMotionLevel) ?
                            CalculateWhitePixels(bitmapData) : 0;

            Bitmap tmpImage2 = openingFilter.Apply(bitmapData);

            // unlock temporary image
            tmpImage.UnlockBits(bitmapData);
            tmpImage.Dispose( );

            // apply edges filter
            Bitmap tmpImage2b = edgesFilter.Apply(tmpImage2);

            tmpImage2.Dispose( );

            // extract red channel from the original image
            Bitmap redChannel = extrachChannel.Apply(image);

            //  merge red channel with moving object borders
            mergeFilter.OverlayImage = tmpImage2b;
            Bitmap tmpImage3 = mergeFilter.Apply(redChannel);

            redChannel.Dispose( );
            tmpImage2b.Dispose( );

            // replace red channel in the original image
            // Updated from original example to support new AForge Libraries (Sumit)
            if (replaceChannel == null)
            {
                replaceChannel = new ReplaceChannel(RGB.R, tmpImage3);
            }
            else
            {
                replaceChannel.ChannelImage = tmpImage3;
            }

            Bitmap tmpImage4 = replaceChannel.Apply(image);

            tmpImage3.Dispose( );

            image.Dispose( );
            image = tmpImage4;
        }