当前位置: 首页>>代码示例>>C#>>正文


C# Image.SetPixels方法代码示例

本文整理汇总了C#中System.Image.SetPixels方法的典型用法代码示例。如果您正苦于以下问题:C# Image.SetPixels方法的具体用法?C# Image.SetPixels怎么用?C# Image.SetPixels使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Image的用法示例。


在下文中一共展示了Image.SetPixels方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: g

        /*

            "For image enhancement purposes, it is often useful to apply a remapping g(x) that
            makes the histogram “flat”, i.e., remaps the grey values so that each value occurs an
            equal number of times in the image. This is called histogram equalization. The idea behind
            it is that the available image contrast is used optimally."

                - Digital and medical Imgae Processing by Twan maintz, update 11/2005, page 59

        */
        /// <summary>
        /// Apply the histogram equalization
        /// </summary>
        /// <param name="image"></param>
        public override void Apply(Image image)
        {
            //h(v) = default pixel distribution
            int[] h = new int[Image.TotalGrayValues];
            for (int x = 0; x < image.Size.Width; x++)
            {
                for (int y = 0; y < image.Size.Height; y++)
                {
                    int pixelColor = image.GetPixelColor(x, y);
                    h[pixelColor] = h[pixelColor] + 1; //Add one to this bin
                }
            }

            //c(v) = added pixel distribution
            int[] c = new int[Image.TotalGrayValues];
            int counter = 0;
            for (int i = 0; i < Image.TotalGrayValues; i++)
            {
                //We are checking the amount of pixels of the current gray value, and add those to the total counter.
                counter += h[i];
                //We set the new value to the c(v) list
                c[i] = counter;
            }

            //We calculate the desired pixesl per bin:
            float totalPixels = image.Size.Width * image.Size.Height;
            float idealPixelsPerBin = totalPixels / ((float)Image.TotalGrayValues);

            //g(v) = the new mapping for the image's pixels. So this array holds the new positions.
            int[] g = new int[Image.TotalGrayValues];
            for (int i = 0; i < Image.TotalGrayValues; i++)
            {
                int result = (int)(c[i] / idealPixelsPerBin + 0.5f) - 1; //these two magic numbers are part of the formula
                g[i] = Math.Max(0, result); //Make all negative numbers zero
            }

            //Remap image:
            int[,] resultAfterRemap = new int[image.Size.Width, image.Size.Height];
            for (int x = 0; x < image.Size.Width; x++)
            {
                for (int y = 0; y < image.Size.Height; y++)
                {
                    int originalPixel = image.GetPixelColor(x, y); //Get pixel at coordinate
                    int remapPixel = g[originalPixel]; //Find in remap list, which pixel this value should get
                    resultAfterRemap[x,y] = remapPixel; //Place this new value pixel in the temporary list
                }
            }
            image.SetPixels(resultAfterRemap);//Overwrite image with new values
        }
开发者ID:basterbogt,项目名称:ImageProcessing,代码行数:63,代码来源:HistogramEqualization.cs

示例2: Decode

        /// <summary>
        /// Decodes the image from the specified stream and sets
        /// the data to image.
        /// </summary>
        /// <param name="image">The image, where the data should be set to.
        /// Cannot be null (Nothing in Visual Basic).</param>
        /// <param name="stream">The stream, where the image should be
        /// decoded from. Cannot be null (Nothing in Visual Basic).</param>
        /// <exception cref="System.ArgumentNullException">
        /// 	<para><paramref name="image"/> is null (Nothing in Visual Basic).</para>
        /// 	<para>- or -</para>
        /// 	<para><paramref name="stream"/> is null (Nothing in Visual Basic).</para>
        /// </exception>
        public void Decode(Image image, Stream stream)
        {
            Guard.NotNull(image, "image");
            Guard.NotNull(stream, "stream");
            JpegImage jpg = new JpegImage(stream);

            int pixelWidth = jpg.Width;
            int pixelHeight = jpg.Height;

            float[] pixels = new float[pixelWidth * pixelHeight * 4];

            if (jpg.Colorspace == Colorspace.RGB && jpg.BitsPerComponent == 8)
            {
                Parallel.For(
                    0,
                    pixelHeight,
                    y =>
                        {
                            SampleRow row = jpg.GetRow(y);

                            for (int x = 0; x < pixelWidth; x++)
                            {
                                Sample sample = row.GetAt(x);

                                int offset = ((y * pixelWidth) + x) * 4;

                                pixels[offset + 0] = sample[0] / 255f;
                                pixels[offset + 1] = sample[1] / 255f;
                                pixels[offset + 2] = sample[2] / 255f;
                                pixels[offset + 3] = 1;
                            }
                        });
            }
            else if (jpg.Colorspace == Colorspace.Grayscale && jpg.BitsPerComponent == 8)
            {
                Parallel.For(
                    0,
                    pixelHeight,
                    y =>
                    {
                        SampleRow row = jpg.GetRow(y);

                        for (int x = 0; x < pixelWidth; x++)
                        {
                            Sample sample = row.GetAt(x);

                            int offset = ((y * pixelWidth) + x) * 4;

                            pixels[offset + 0] = sample[0] / 255f;
                            pixels[offset + 1] = sample[0] / 255f;
                            pixels[offset + 2] = sample[0] / 255f;
                            pixels[offset + 3] = 1;
                        }
                    });
            }
            else
            {
                throw new NotSupportedException("JpegDecoder only supports RGB and Grayscale color spaces.");
            }

            image.SetPixels(pixelWidth, pixelHeight, pixels);

            jpg.Dispose();
        }
开发者ID:joaonlopes,项目名称:ThumbnailCreator,代码行数:77,代码来源:JpegDecoder.cs

示例3: ToImage

        /// <summary>
        /// Converts this quantized image to a normal image.
        /// </summary>
        /// <returns>
        /// The <see cref="Image"/>
        /// </returns>
        public Image ToImage()
        {
            Image image = new Image();

            int pixelCount = this.Pixels.Length;
            int palletCount = this.Palette.Length - 1;
            float[] bgraPixels = new float[pixelCount * 4];

            Parallel.For(0, pixelCount,
                i =>
                    {
                        int offset = i * 4;
                        Color color = this.Palette[Math.Min(palletCount, this.Pixels[i])];
                        bgraPixels[offset] = color.R;
                        bgraPixels[offset + 1] = color.G;
                        bgraPixels[offset + 2] = color.B;
                        bgraPixels[offset + 3] = color.A;
                    });

            image.SetPixels(this.Width, this.Height, bgraPixels);
            return image;
        }
开发者ID:joaonlopes,项目名称:ThumbnailCreator,代码行数:28,代码来源:QuantizedImage.cs

示例4: Decode

        /// <summary>
        /// Decodes the image from the specified this._stream and sets
        /// the data to image.
        /// </summary>
        /// <param name="image">The image, where the data should be set to.
        /// Cannot be null (Nothing in Visual Basic).</param>
        /// <param name="stream">The this._stream, where the image should be
        /// decoded from. Cannot be null (Nothing in Visual Basic).</param>
        /// <exception cref="ArgumentNullException">
        ///    <para><paramref name="image"/> is null.</para>
        ///    <para>- or -</para>
        ///    <para><paramref name="stream"/> is null.</para>
        /// </exception>
        public void Decode(Image image, Stream stream)
        {
            this.currentStream = stream;

            try
            {
                this.ReadFileHeader();
                this.ReadInfoHeader();

                int colorMapSize = -1;

                if (this.infoHeader.ClrUsed == 0)
                {
                    if (this.infoHeader.BitsPerPixel == 1 ||
                        this.infoHeader.BitsPerPixel == 4 ||
                        this.infoHeader.BitsPerPixel == 8)
                    {
                        colorMapSize = (int)Math.Pow(2, this.infoHeader.BitsPerPixel) * 4;
                    }
                }
                else
                {
                    colorMapSize = this.infoHeader.ClrUsed * 4;
                }

                byte[] palette = null;

                if (colorMapSize > 0)
                {
                    // 255 * 4
                    if (colorMapSize > 1020)
                    {
                        throw new ImageFormatException($"Invalid bmp colormap size '{colorMapSize}'");
                    }

                    palette = new byte[colorMapSize];

                    this.currentStream.Read(palette, 0, colorMapSize);
                }

                if (this.infoHeader.Width > ImageBase.MaxWidth || this.infoHeader.Height > ImageBase.MaxHeight)
                {
                    throw new ArgumentOutOfRangeException(
                        $"The input bitmap '{this.infoHeader.Width}x{this.infoHeader.Height}' is "
                        + $"bigger then the max allowed size '{ImageBase.MaxWidth}x{ImageBase.MaxHeight}'");
                }

                float[] imageData = new float[this.infoHeader.Width * this.infoHeader.Height * 4];

                switch (this.infoHeader.Compression)
                {
                    case BmpCompression.RGB:
                        if (this.infoHeader.HeaderSize != 40)
                        {
                            throw new ImageFormatException(
                                $"Header Size value '{this.infoHeader.HeaderSize}' is not valid.");
                        }

                        if (this.infoHeader.BitsPerPixel == 32)
                        {
                            this.ReadRgb32(imageData, this.infoHeader.Width, this.infoHeader.Height);
                        }
                        else if (this.infoHeader.BitsPerPixel == 24)
                        {
                            this.ReadRgb24(imageData, this.infoHeader.Width, this.infoHeader.Height);
                        }
                        else if (this.infoHeader.BitsPerPixel == 16)
                        {
                            this.ReadRgb16(imageData, this.infoHeader.Width, this.infoHeader.Height);
                        }
                        else if (this.infoHeader.BitsPerPixel <= 8)
                        {
                            this.ReadRgbPalette(
                                imageData,
                                palette,
                                this.infoHeader.Width,
                                this.infoHeader.Height,
                                this.infoHeader.BitsPerPixel);
                        }

                        break;
                    default:
                        throw new NotSupportedException("Does not support this kind of bitmap files.");
                }

                image.SetPixels(this.infoHeader.Width, this.infoHeader.Height, imageData);
            }
//.........这里部分代码省略.........
开发者ID:joaonlopes,项目名称:ThumbnailCreator,代码行数:101,代码来源:BmpDecoderCore.cs


注:本文中的System.Image.SetPixels方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。