gpt4 book ai didi

c# - 我需要做一个直方图拉伸(stretch)

转载 作者:太空狗 更新时间:2023-10-29 22:35:19 25 4
gpt4 key购买 nike

我有一个 BitmapFrames 数组,需要进行直方图拉伸(stretch)。我知道这与直方图均衡不同,最终结果是什么......有点。问题是我完全不知道在获得直方图后要做什么。

到目前为止,我的代码为直方图创建了一个数组,因此我知道每个值有多少像素。但是在那之后我不知道该怎么办。

这是我到目前为止的代码...现在它生成直方图,然后使直方图均衡...这不是我想要的...我只是想了解有关直方图的更多信息

[Cmdlet(VerbsData.ConvertTo, "HistoStretch")]
public class HistoStretchCmdlet : PSCmdlet
{
private BitmapFrame[] bFrame, outFrame;
private BitmapSource src;
private double pixelsize;
private byte[] pixels, outPixels;
private byte MAX_VAL;
private int[] histogram;
private int cf, start;

[Parameter(ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true), ValidateNotNullOrEmpty]
public BitmapFrame[] Bitmap
{
get
{
return bFrame;
}
set
{
bFrame = value;
}
}

protected override void ProcessRecord()
{
base.ProcessRecord();
Console.Write("Applying a histogram stretch to the image...\n\n");
outFrame = new BitmapFrame[bFrame.Length];
for (int c = 0; c < bFrame.Length; c++)
{
MAX_VAL = (byte)((1 << bFrame[c].Format.BitsPerPixel) - 1);
histogram = new int[MAX_VAL + 1];
for (int i = 0; i <= MAX_VAL; i++)
{
histogram[i] = 0;
}

pixelsize = bFrame[c].PixelWidth * bFrame[c].PixelHeight;
pixels = new byte[(int)pixelsize];
outPixels = new byte[(int)pixelsize];
bFrame[c].CopyPixels(pixels,(int)bFrame[c].Width * (bFrame[c].Format.BitsPerPixel / 8),0);

for (int i = 0; i < pixelsize; i++)
{
histogram[(int)pixels[i]] = histogram[(int)pixels[i]] + 1;
}
for (int i = 0; i <= MAX_VAL; i++)
{
Console.Write("{0}: {1}\n", i, histogram[i]);
}
for (int i = 0; i <= MAX_VAL; i++)
{
if (histogram[i] >= 1)
{
start = i;
break;
}
}

for (int i = 0; i < pixelsize; i++)
{
cf = 0;
for (int g = 0; g <= MAX_VAL; g++)
{
cf += histogram[g];
if (g == pixels[i])
{
break;
}
}
outPixels[i] = (byte)(cf * (MAX_VAL / pixelsize));
}

src = BitmapSource.Create(bFrame[c].PixelWidth, bFrame[c].PixelHeight, bFrame[c].DpiX, bFrame[c].DpiY,
bFrame[c].Format, bFrame[c].Palette, outPixels, (int)(bFrame[c].Width * (bFrame[c].Format.BitsPerPixel / 8)));
outFrame[c] = BitmapFrame.Create(src);
}
WriteObject(outFrame);
}
}

根据我的老师的说法,直方图应该是这样的:

http://www.fileden.com/files/2009/8/18/2547657/histostretch.PNG

我运行了上面的代码……得到了一张纯黑色的图像。这是我的代码:

outFrame = new BitmapFrame[bFrame.Length];
for (int c = 0; c < bFrame.Length; c++)
{
MAX_VAL = (byte)((1 << bFrame[c].Format.BitsPerPixel) - 1);
histogram = new int[MAX_VAL + 1];
for (int i = 0; i <= MAX_VAL; i++)
{
histogram[i] = 0;
}

pixelsize = bFrame[c].PixelWidth * bFrame[c].PixelHeight;
pixels = new byte[(int)pixelsize];
outPixels = new byte[(int)pixelsize];
bFrame[c].CopyPixels(pixels,(int)bFrame[c].Width * (bFrame[c].Format.BitsPerPixel / 8),0);
max = pixels[0];
min = pixels[0];

for (int i = 0; i < pixelsize; i++)
{
histogram[(int)pixels[i]] = histogram[(int)pixels[i]] + 1;
if((int)pixels[i] > max)
max = pixels[i];
if((int)pixels[i] < min)
min = pixels[i];
}

dynamic = max - min;

for (int i = 0; i < pixelsize; i++)
{
outPixels[i] = (byte)(((pixels[i] - min) / dynamic) * MAX_VAL);
}

最佳答案

直方图拉伸(stretch)是像素值的映射,这样:

  • 最低值(例如图中的 84)变为 0
  • 最高值(例如图中的 153)变为 255(在 8 位图像中)
  • 所有中间值都在该范围内插入(见插图)。

换句话说,直方图拉伸(stretch)意味着将图像数据的动态 (84:153) 拉伸(stretch)到最大可能的动态 (0:255)。

这不应该影响直方图峰值的高度,而只会影响它们的分布(插图在这一点上有点误导)。

histogram stretch http://cct.rncan.gc.ca/resource/tutor/fundam/images/linstre.gif

Image source

在实践中这是您将应用于图像像素的映射(伪代码):

maxVal = image.maximumValue() # 153
minVal = image.minumumValue() # 84
dynamic = maxVal-minVal
for pixel in image.Pixels():
newPixel = ((pixel-minVal)/dynamic)*255

关于c# - 我需要做一个直方图拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1295057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com