Replace RGB channel of color imgae.

Replaces specified RGB channel of color image with specified grayscale image.

The filter is quite useful in conjunction with ExtractChannel filter (however may be used alone in some cases). Using the ExtractChannel filter it is possible to extract one of RGB channel, perform some image processing with it and then put it back into the original color image.

The filter accepts 24, 32, 48 and 64 bpp color images for processing.

Sample usage:

// extract red channel ExtractChannel extractFilter = new ExtractChannel( RGB.R ); Bitmap channel = extractFilter.Apply( image ); // threshold channel Threshold thresholdFilter = new Threshold( 230 ); thresholdFilter.ApplyInPlace( channel ); // put the channel back ReplaceChannel replaceFilter = new ReplaceChannel( RGB.R, channel ); replaceFilter.ApplyInPlace( image );

Initial image:

Result image:

Inheritance: BaseInPlacePartialFilter
Example #1
0
 private void SetFilter()
 {
     if ((int)mode > 3)
     {
         ImageType = ImageTypes.ARgb32bpp;
         Af.YCbCrReplaceChannel newFilter = new Af.YCbCrReplaceChannel((short)(mode - 4), channelImage);
         imageFilter = newFilter;
     }
     else
     {
         ImageType = ImageTypes.ARgb32bpp;
         Af.ReplaceChannel newFilter = new Af.ReplaceChannel((short)mode, channelImage);
         imageFilter = newFilter;
     }
 }
Example #2
0
        /// <summary>
        /// Get a filled image by a color.
        /// </summary>
        /// <param name="width">Image width.</param>
        /// <param name="height">Image height.</param>
        /// <param name="pixelFormat">Image pixel format.</param>
        /// <param name="color">Image filled color.</param>
        /// <returns>An filled image with a color.</returns>
        public static UnmanagedImage GetFilledImage(int width, int height, PixelFormat pixelFormat, Color color)
        {
            UnmanagedImage filledImage;

            var grayImage = UnmanagedImage.Create(width, height, PixelFormat.Format8bppIndexed);

            if (pixelFormat == PixelFormat.Format8bppIndexed ||
                pixelFormat == PixelFormat.Format16bppGrayScale)
            {
                SystemTools.SetUnmanagedMemory(
                    grayImage.ImageData,
                    (color.R + color.G + color.B) / 3,
                    grayImage.Stride * grayImage.Height);

                filledImage = pixelFormat == PixelFormat.Format8bppIndexed ? grayImage.Clone() :
                    UnmanagedImage.FromManagedImage(
                        Accord.Imaging.Image.Convert8bppTo16bpp(grayImage.ToManagedImage(false)));
            }
            else
            {
                filledImage = UnmanagedImage.Create(width, height, pixelFormat);

                if (pixelFormat == PixelFormat.Format24bppRgb)
                {
                    SystemTools.SetUnmanagedMemory(grayImage.ImageData, color.R, grayImage.Stride * grayImage.Height);
                    var replaceChannel = new ReplaceChannel(RGB.R, grayImage);
                    replaceChannel.ApplyInPlace(filledImage);

                    SystemTools.SetUnmanagedMemory(grayImage.ImageData, color.G, grayImage.Stride * grayImage.Height);
                    replaceChannel.Channel = RGB.G;
                    grayImage.Copy(replaceChannel.UnmanagedChannelImage);
                    replaceChannel.ApplyInPlace(filledImage);

                    SystemTools.SetUnmanagedMemory(grayImage.ImageData, color.B, grayImage.Stride * grayImage.Height);
                    replaceChannel.Channel = RGB.B;
                    grayImage.Copy(replaceChannel.UnmanagedChannelImage);
                    replaceChannel.ApplyInPlace(filledImage);
                }
                else
                {
                    SystemTools.SetUnmanagedMemory(grayImage.ImageData, color.R, grayImage.Stride * grayImage.Height);

                    UnmanagedImage grayImage16 = UnmanagedImage.FromManagedImage(
                        Accord.Imaging.Image.Convert8bppTo16bpp(grayImage.ToManagedImage(false)));

                    ReplaceChannel replaceChannel = new ReplaceChannel(RGB.R, grayImage16);
                    replaceChannel.ApplyInPlace(filledImage);


                    SystemTools.SetUnmanagedMemory(grayImage.ImageData, color.G, grayImage.Stride * grayImage.Height);

                    replaceChannel.Channel = RGB.G;
                    replaceChannel.UnmanagedChannelImage.Dispose();

                    replaceChannel.UnmanagedChannelImage =
                        UnmanagedImage.FromManagedImage(Accord.Imaging.Image.Convert8bppTo16bpp(
                            grayImage.ToManagedImage(false)));

                    replaceChannel.ApplyInPlace(filledImage);


                    SystemTools.SetUnmanagedMemory(grayImage.ImageData, color.B, grayImage.Stride * grayImage.Height);

                    replaceChannel.Channel = RGB.B;
                    replaceChannel.UnmanagedChannelImage.Dispose();

                    replaceChannel.UnmanagedChannelImage =
                        UnmanagedImage.FromManagedImage(Accord.Imaging.Image.Convert8bppTo16bpp(
                            grayImage.ToManagedImage(false)));

                    replaceChannel.ApplyInPlace(filledImage);
                    replaceChannel.UnmanagedChannelImage.Dispose();

                    grayImage16.Dispose();
                }
            }

            grayImage.Dispose();

            return filledImage;
        }
        /// <summary>
        /// Get a filled image by a color.
        /// </summary>
        /// <param name="width">Image width.</param>
        /// <param name="height">Image height.</param>
        /// <param name="pixelFormat">Image pixel format.</param>
        /// <param name="color">Image filled color.</param>
        /// <returns>An filled image with a color.</returns>
        public static UnmanagedImage GetFilledImage(int width, int height, PixelFormat pixelFormat, Color color)
        {
            UnmanagedImage filledImage;

            var grayImage = UnmanagedImage.Create(width, height, PixelFormat.Format8bppIndexed);

            if (pixelFormat == PixelFormat.Format8bppIndexed ||
                pixelFormat == PixelFormat.Format16bppGrayScale)
            {
                SystemTools.SetUnmanagedMemory(
                    grayImage.ImageData,
                    (color.R + color.G + color.B) / 3,
                    grayImage.Stride * grayImage.Height);

                filledImage = pixelFormat == PixelFormat.Format8bppIndexed ? grayImage.Clone() :
                              UnmanagedImage.FromManagedImage(
                    Accord.Imaging.Image.Convert8bppTo16bpp(grayImage.ToManagedImage(false)));
            }
            else
            {
                filledImage = UnmanagedImage.Create(width, height, pixelFormat);

                if (pixelFormat == PixelFormat.Format24bppRgb || pixelFormat == PixelFormat.Format32bppArgb || pixelFormat == PixelFormat.Format32bppRgb)
                {
                    SystemTools.SetUnmanagedMemory(grayImage.ImageData, color.R, grayImage.Stride * grayImage.Height);
                    var replaceChannel = new ReplaceChannel(RGB.R, grayImage);
                    replaceChannel.ApplyInPlace(filledImage);

                    SystemTools.SetUnmanagedMemory(grayImage.ImageData, color.G, grayImage.Stride * grayImage.Height);
                    replaceChannel.Channel = RGB.G;
                    grayImage.Copy(replaceChannel.UnmanagedChannelImage);
                    replaceChannel.ApplyInPlace(filledImage);

                    SystemTools.SetUnmanagedMemory(grayImage.ImageData, color.B, grayImage.Stride * grayImage.Height);
                    replaceChannel.Channel = RGB.B;
                    grayImage.Copy(replaceChannel.UnmanagedChannelImage);
                    replaceChannel.ApplyInPlace(filledImage);

                    if (pixelFormat == PixelFormat.Format32bppArgb)
                    {
                        SystemTools.SetUnmanagedMemory(grayImage.ImageData, color.A, grayImage.Stride * grayImage.Height);
                        replaceChannel.Channel = RGB.A;
                        grayImage.Copy(replaceChannel.UnmanagedChannelImage);
                        replaceChannel.ApplyInPlace(filledImage);
                    }
                }
                else
                {
                    SystemTools.SetUnmanagedMemory(grayImage.ImageData, color.R, grayImage.Stride * grayImage.Height);

                    UnmanagedImage grayImage16 = UnmanagedImage.FromManagedImage(
                        Accord.Imaging.Image.Convert8bppTo16bpp(grayImage.ToManagedImage(false)));

                    ReplaceChannel replaceChannel = new ReplaceChannel(RGB.R, grayImage16);
                    replaceChannel.ApplyInPlace(filledImage);


                    SystemTools.SetUnmanagedMemory(grayImage.ImageData, color.G, grayImage.Stride * grayImage.Height);

                    replaceChannel.Channel = RGB.G;
                    replaceChannel.UnmanagedChannelImage.Dispose();

                    replaceChannel.UnmanagedChannelImage =
                        UnmanagedImage.FromManagedImage(Accord.Imaging.Image.Convert8bppTo16bpp(
                                                            grayImage.ToManagedImage(false)));

                    replaceChannel.ApplyInPlace(filledImage);


                    SystemTools.SetUnmanagedMemory(grayImage.ImageData, color.B, grayImage.Stride * grayImage.Height);

                    replaceChannel.Channel = RGB.B;
                    replaceChannel.UnmanagedChannelImage.Dispose();

                    replaceChannel.UnmanagedChannelImage =
                        UnmanagedImage.FromManagedImage(Accord.Imaging.Image.Convert8bppTo16bpp(
                                                            grayImage.ToManagedImage(false)));

                    replaceChannel.ApplyInPlace(filledImage);
                    replaceChannel.UnmanagedChannelImage.Dispose();

                    grayImage16.Dispose();
                }
            }

            grayImage.Dispose();

            return(filledImage);
        }