本文整理汇总了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
}
示例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();
}
示例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;
}
示例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);
}
//.........这里部分代码省略.........