我需要做一个直方图拉伸。

5
我有一个BitmapFrames数组,需要进行直方图拉伸。我知道这与直方图均衡不同,以及最终结果是什么...但问题是在获取直方图后,我完全不知道该怎么做。
到目前为止,我的代码创建了一个直方图数组,所以我知道每个值有多少像素。但在此之后,我不知道该怎么办。
这是我目前的代码...现在它生成直方图,然后进行直方图均衡...这不是我想要的...我只是想学习更多关于直方图的知识。
[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);
            }
3个回答

9

直方图拉伸是将像素值进行映射,使得:

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

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

这不应影响直方图峰值的高度,但仅影响它们的分布(在这一点上,示例有点误导人)。

直方图拉伸 http://cct.rncan.gc.ca/resource/tutor/fundam/images/linstre.gif

图片来源

实际上,这是您将应用于图像像素的映射(伪代码):

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

我尝试着实现了那个...请看下面的"答案"...这是我为它编写的新代码,但它不起作用。 - dabonz413

0

如果您可以控制相机的光照、增益/偏移,则可以优化它们并根据需要拉伸直方图。


我完全无法控制相机。这都是处理任何随机给定的图像。 - dabonz413

0

不要忘记考虑浮点数。因此,对dabonz413的答案进行轻微修改:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接