Beispiel #1
0
        public static Gray <byte> ToGray(this Bgr <byte> bgr)
        {
            Gray <byte> gray = default(Gray <byte>);

            Bgr <byte> .Convert(bgr, ref gray);

            return(gray);
        }
 /// <summary>
 /// Converts an image to an bitmap.
 /// </summary>
 /// <param name="img">Input image.</param>
 /// <returns>Bitmap</returns>
 public static Bitmap ToBitmap(this Gray<short>[,] img)
 {
     Bitmap bmp = null;
     using (var uImg = img.Lock())
     {
         bmp = toBitmap(uImg, PixelFormat.Format16bppGrayScale);
     }
     return bmp;
 }
 /// <summary>
 /// Converts an image to an bitmap.
 /// </summary>
 /// <param name="img">Input image.</param>
 /// <returns>Bitmap</returns>
 public static Bitmap ToBitmap(this Gray<byte>[,] img)
 {
     Bitmap bmp = null;
     using (var uImg = img.Lock())
     {
         bmp = toBitmap(uImg, PixelFormat.Format8bppIndexed); 
     }
     return bmp;
 }
Beispiel #4
0
        public static void Convert(Bgr <byte> bgr, ref Gray <byte> gray)
        {
            int val = ((bgr.R << 1) +          //2 * red
                       (bgr.G << 2) + bgr.G +  //5 * green
                       bgr.B                   //1 * blue

                       ) >> 3;                 //divide by 8

            gray.Intensity = (byte)val;
        }
        /// <summary>
        /// Converts the specified managed array to the corresponding bitmap source.
        /// </summary>
        /// <param name="image">Managed array.</param>
        /// <returns>Bitmap source.</returns>
        public static BitmapSource ToBitmapSource(this Gray <short>[,] image)
        {
            BitmapSource bmpSource = null;

            using (var uImg = image.Lock())
            {
                bmpSource = BitmapSource.Create(uImg.Width, uImg.Height, 96, 96,
                                                PixelFormats.Gray16, BitmapPalettes.Gray256,
                                                uImg.ImageData, uImg.Stride * uImg.Height, uImg.Stride);
            }

            return(bmpSource);
        }
Beispiel #6
0
        /// <summary>
        /// Sets the specified value for only those element of the array where the mask is non-zero.
        /// </summary>
        /// <typeparam name="T">Element type.</typeparam>
        /// <param name="array">Array with value type elements.</param>
        /// <param name="value">Value to set.</param>
        /// <param name="mask">Mask.</param>
        public static void SetValue <T>(this T[,] array, T value, Gray <byte>[,] mask)
        {
            if (array.Size() != mask.Size())
            {
                throw new ArgumentException("Array and mask must have the same size.");
            }

            ParallelLauncher.Launch((thread) =>
            {
                if (mask[thread.Y, thread.X] != 0)
                {
                    array[thread.Y, thread.X] = value;
                }
            },
                                    array.Width(), array.Height());
        }
Beispiel #7
0
        /// <summary>
        /// Extracts the specified image channels.
        /// </summary>
        /// <typeparam name="TSrcColor">Source color type.</typeparam>
        /// <typeparam name="TDepth">Channel depth type.</typeparam>
        /// <param name="image">Image.</param>
        /// <param name="area">Working area.</param>
        /// <param name="channelIndices">Channel indicies to extract. If null, all channels are extracted.</param>
        /// <returns>Channel collection.</returns>
        public static unsafe Gray <TDepth>[][,] SplitChannels <TSrcColor, TDepth>(this TSrcColor[,] image, Rectangle area, params int[] channelIndices)
        where TSrcColor : struct, IColor <TDepth>
        where TDepth : struct
        {
            if (channelIndices == null || channelIndices.Length == 0)
            {
                channelIndices = Enumerable.Range(0, ColorInfo.GetInfo <TSrcColor>().ChannelCount).ToArray();
            }

            var channels = new Gray <TDepth> [channelIndices.Length][, ];
            for (int i = 0; i < channelIndices.Length; i++)
            {
                channels[i] = GetChannel <TSrcColor, TDepth>(image, area, channelIndices[i]);
            }

            return(channels);
        }
Beispiel #8
0
        /// <summary>
        /// Extracts a single image channel.
        /// </summary>
        /// <typeparam name="TSrcColor">Source color type.</typeparam>
        /// <typeparam name="TDepth">Channel depth type.</typeparam>
        /// <param name="image">Image.</param>
        /// <param name="area">Working area.</param>
        /// <param name="channelIndex">Channel index.</param>
        /// <returns>Extracted channel.</returns>
        public static unsafe Gray <TDepth>[,] GetChannel <TSrcColor, TDepth>(this TSrcColor[,] image, Rectangle area, int channelIndex)
        where TSrcColor : struct, IColor <TDepth>
        where TDepth : struct
        {
            int width  = area.Width;
            int height = area.Height;

            var dest = new Gray <TDepth> [area.Height, area.Width];

            using (var lockedImage = image.Lock())
                using (var dstImg = dest.Lock())
                {
                    var srcImg      = lockedImage.GetSubRect(area);
                    int channelSize = srcImg.ColorInfo.ChannelSize;
                    int colorSize   = srcImg.ColorInfo.Size;

                    byte *srcPtr = (byte *)srcImg.ImageData + channelIndex * srcImg.ColorInfo.ChannelSize;
                    byte *dstPtr = (byte *)dstImg.ImageData;

                    for (int row = 0; row < height; row++)
                    {
                        byte *srcColPtr = srcPtr;
                        byte *dstColPtr = dstPtr;
                        for (int col = 0; col < width; col++)
                        {
                            /********** copy channel byte-per-byte ************/
                            for (int partIdx = 0; partIdx < channelSize; partIdx++)
                            {
                                dstColPtr[partIdx] = srcColPtr[partIdx];
                            }

                            srcColPtr += colorSize; //move to the next column
                            dstColPtr += channelSize;
                            /********** copy channel byte-per-byte ************/
                        }

                        srcPtr += srcImg.Stride;
                        dstPtr += dstImg.Stride;
                    }
                }

            return(dest);
        }
Beispiel #9
0
 /// <summary>
 /// Saves the specified image.
 /// </summary>
 /// <param name="image">Image to save.</param>
 /// <param name="fileName">Image filename.</param>
 public static void Save(this Gray <double>[,] image, string fileName)
 {
     image.Save <Gray <double> >(fileName);
 }
Beispiel #10
0
        /// <summary>
        /// Copies values from source to destination image using mask. Destination values where mask == 0 are not erased!.
        /// </summary>
        /// <param name="source">Image.</param>
        /// <param name="destination">Destination image</param>
        /// <param name="mask">Mask. Color locations that need to be copied must be set to !=0 in mask.</param>
        public static void CopyTo <TColor>(this TColor[,] source, TColor[,] destination, Gray <byte>[,] mask)
            where TColor : struct
        {
            if (source.Size() != mask.Size() || source.Size() != destination.Size())
            {
                throw new Exception("Image, mask, destImg size must be the same!");
            }

            ParallelLauncher.Launch((thread) =>
            {
                if (mask[thread.Y, thread.X] != 0)
                {
                    destination[thread.Y, thread.X] = source[thread.Y, thread.X];
                }
            },
                                    source.Width(), source.Height());
        }
Beispiel #11
0
 /// <summary>
 /// Converts the source color to the destination color.
 /// </summary>
 /// <param name="grayIm">Source image.</param>
 /// <returns>image with converted color.</returns>
 public static Bgr <byte>[,] ToBgr(this Gray <byte>[,] grayIm)
 {
     return(grayIm.Convert <Gray <byte>, Bgr <byte> >(Gray <byte> .Convert));
 }
Beispiel #12
0
 /// <summary>
 /// Converts the source color to the destination color.
 /// </summary>
 /// <param name="grayIm">Source image.</param>
 /// <param name="area">Working area.</param>
 /// <returns>image with converted color.</returns>
 public static Bgr <byte>[,] ToBgr(this Gray <byte>[,] grayIm, Rectangle area)
 {
     return(grayIm.Convert <Gray <byte>, Bgr <byte> >(Gray <byte> .Convert, area));
 }
Beispiel #13
0
 /// <summary>
 /// Gets System.Drawing.Color from Bgr8 color.
 /// </summary>
 /// <param name="color">Color.</param>
 /// <param name="opacity">Opacity. If color has 4 channels opacity is discarded.</param>
 /// <returns>System.Drawing.Color</returns>
 public static System.Drawing.Color ToColor(this Gray <byte> color, byte opacity = Byte.MaxValue)
 {
     return(Color.FromArgb(opacity, color.Intensity, color.Intensity, color.Intensity));
 }
Beispiel #14
0
 /// <summary>
 /// Encodes the specified image into the specified image type byte array.
 /// </summary>
 /// <param name="image">Image to encode.</param>
 /// <param name="extension">Image type extension (.bmp, .png, .jpg)</param>
 /// <returns>Image type byte array.</returns>
 public static byte[] Encode(this Gray <ushort>[,] image, string extension)
 {
     return(encode(image, extension, null));
 }
Beispiel #15
0
 /// <summary>
 /// Converts 8-bit gray intensity to the 8-bit Bgr color.
 /// </summary>
 /// <param name="gray">Source color.</param>
 /// <param name="bgr">Destination color.</param>
 public static void Convert(Gray <T> gray, ref Bgr <T> bgr)
 {
     bgr.B = gray.Intensity;
     bgr.G = gray.Intensity;
     bgr.R = gray.Intensity;
 }
Beispiel #16
0
 /// <summary>
 /// Encodes the specified image into the PNG byte array.
 /// </summary>
 /// <param name="image">Image to encode.</param>
 /// <param name="pngCompression">PNG compression level [0..9] where 9 is the highest compression.</param>
 /// <returns>PNG byte array.</returns>
 public static byte[] EncodeAsPng(this Gray <ushort>[,] image, int pngCompression = 3)
 {
     return(encodeAsPng(image, pngCompression));
 }
Beispiel #17
0
 /// <summary>
 /// Encodes the specified image into the Jpeg byte array.
 /// </summary>
 /// <param name="image">Image to encode.</param>
 /// <param name="jpegQuality">Jpeg quality [0..100] where 100 is the highest quality.</param>
 /// <returns>Jpeg byte array.</returns>
 public static byte[] EncodeAsJpeg(this Gray <ushort>[,] image, int jpegQuality = 95)
 {
     return(encodeAsJpeg(image, jpegQuality));
 }
Beispiel #18
0
 /// <summary>
 /// Converts the source channel depth to the destination channel depth.
 /// </summary>
 /// <typeparam name="TDepth">Destination channel depth.</typeparam>
 /// <param name="image">Image.</param>
 /// <returns>Image with converted element depth.</returns>
 public static Gray <TDepth>[,] Cast <TDepth>(this Gray <double>[,] image)
 where TDepth : struct
 {
     return(image.ConvertChannelDepth <Gray <double>, Gray <TDepth> >());
 }
Beispiel #19
0
 /// <summary>
 /// Saves the specified image.
 /// </summary>
 /// <param name="image">Image to save.</param>
 /// <param name="fileName">Image filename.</param>
 public static void Save(this Gray <ushort>[,] image, string fileName)
 {
     image.Save <Gray <ushort> >(fileName);
 }
Beispiel #20
0
 /// <summary>
 /// Replaces the selected image channel with the specified channel.
 /// </summary>
 /// <typeparam name="TSrcColor">Source color type.</typeparam>
 /// <typeparam name="TDepth">Channel depth type.</typeparam>
 /// <param name="image">Image.</param>
 /// <param name="channel">Channel.</param>
 /// <param name="channelIndex">Index of a channel to replace.</param>
 public static void ReplaceChannel <TSrcColor, TDepth>(this TSrcColor[,] image, Gray <TDepth>[,] channel, int channelIndex)
     where TSrcColor : unmanaged, IColor <TDepth>
     where TDepth : unmanaged
 {
     using (var im = image.Lock())
         using (var ch = channel.Lock())
         {
             replaceChannel <TSrcColor, TDepth>(im, ch, channelIndex);
         }
 }