我需要做直方图拉伸

发布于 2024-08-02 09:06:45 字数 4342 浏览 2 评论 0原文

我有一个 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);
            }

I have an array of BitmapFrames and need to do a histogram stretch. I know this is different from a histogram equalization, and what the final outcome is... sorta. the problem is I have absolutely no idea what to do after I get the histogram.

So far my code creates an array for the histogram so I know how many pixels of each value i have. But after that I don't know what to do.

this is the code I have so far... right now it makes the histogram and then histogram equalizes... which is NOT what I want... I was just trying to learn more about histograms

[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);
    }
}

this is what the histograms should look like according to my teacher:

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

I ran the code above... and got a straight black image.
here's my code:

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);
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事与诗 2024-08-09 09:06:45

直方图拉伸是像素值的映射,使得:

  • 最低值(例如图中的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

Histogram stretching is the mapping of pixel values such that:

  • the lowest value (e.g. 84 in the illustration) becomes 0
  • the highest value (e.g. 153 in the illustration) becomes 255 (in 8bit images)
  • and all the intermediate values are interpolated between that range (see illustration).

In other words histogram stretching means stretching the dynamic of the image data (84:153) to the greatest possible dynamic (0:255).

This should not affect the hight of the histogram peaks, but only their spread (the illustration is a bit misleading on this point).

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

Image source

In practice this is the mapping you would apply to the pixels of the image (pseudocode):

maxVal = image.maximumValue() # 153
minVal = image.minumumValue() # 84
dynamic = maxVal-minVal
for pixel in image.Pixels():
    newPixel = ((pixel-minVal)/dynamic)*255
美人骨 2024-08-09 09:06:45

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

If you have control over the lighting and or gain/offset of the camera, you can optimize those and stretch the histogram as you desire.

流云如水 2024-08-09 09:06:45

不要忘记考虑浮动。所以对dabonz413的答案稍作修改:

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

Don't forget to consider float. So a slight modification to dabonz413's answer:

maxVal = image.maximumValue() # 153
minVal = image.minumumValue() # 84
dynamic = maxVal-minVal
for pixel in image.Pixels():
    newPixel = ((float) (pixel-minVal)/dynamic)*255
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文