MATLAB中直方图均衡功能的说明 [英] Explanation of the Histogram Equalization function in MATLAB

查看:166
本文介绍了MATLAB中直方图均衡功能的说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们在MATLAB中使用 histeq 函数时,请解释一下图像会发生什么?数学解释将非常有用。

Please explain as to what happens to an image when we use histeq function in MATLAB? A mathematical explanation would be really helpful.

推荐答案

直方图均衡寻求展平您的图像直方图。基本上,它将图像建模为概率密度函数(或者更简单地说,直方图,您可以将每个条目标准化为图像中的像素总数),并尝试确保像素采用特定强度的概率等概率(概率相等)。

Histogram equalization seeks to flatten your image histogram. Basically, it models the image as a probability density function (or in simpler terms, a histogram where you normalize each entry by the total number of pixels in the image) and tries to ensure that the probability for a pixel to take on a particular intensity is equiprobable (with equal probability).

直方图均衡背后的前提是对比度差的图像。看起来像是太暗的图像,或者它们太褪色,或者它们太亮的图像是适合直方图均衡的好选择。如果绘制直方图,则像素的范围限制在非常窄的范围内。通过直方图均衡,直方图将因此变平并为您提供更好的对比度图像。这与直方图的影响是它延伸了直方图的动态范围。

The premise behind histogram equalization is for images that have poor contrast. Images that look like they're too dark, or if they're too washed out, or if they're too bright are good candidates for you to apply histogram equalization. If you plot the histogram, the spread of the pixels is limited to a very narrow range. By doing histogram equalization, the histogram will thus flatten and give you a better contrast image. The effect of this with the histogram is that it stretches the dynamic range of your histogram.

就数学定义而言,我不会厌烦你的细节和我希望有一些LaTeX在这里做,但它不受支持。因此,我推迟到这个链接,更详细地解释它: http://www.math.uci.edu/icamp/courses/math77c/demos/hist_eq.pdf

In terms of the mathematical definition, I won't bore you with the details and I would love to have some LaTeX to do it here, but it isn't supported. As such, I defer you to this link that explains it in more detail: http://www.math.uci.edu/icamp/courses/math77c/demos/hist_eq.pdf

然而,你最后的等式get用于执行直方图均衡本质上是一对一映射。对于图像中的每个像素,您可以提取其强度,然后通过此函数运行它。然后它会为您提供输出图像中的输出强度。

However, the final equation that you get for performing histogram equalization is essentially a 1-to-1 mapping. For each pixel in your image, you extract its intensity, then run it through this function. It then gives you an output intensity to be placed in your output image.

假设 p_i 是概率您会在图像中遇到强度 i 的像素(取像素强度 i 的直方图箱数并除以图像中的总像素数)。鉴于您的图像中有 L 强度,此位置的输出强度给定强度为 i 被指定为:

Supposing that p_i is the probability that you would encounter a pixel with intensity i in your image (take the histogram bin count for pixel intensity i and divide by the total number of pixels in your image). Given that you have L intensities in your image, the output intensity at this location given the intensity of i is dictated as:

g_i = floor( (L-1) * sum_{n=0}^{i} p_i )

您将所有概率从像素强度0,然后是1,然后是2加起来,一直到强度 i 。这通常被称为累积分布函数。

You add up all of the probabilities from pixel intensity 0, then 1, then 2, all the way up to intensity i. This is familiarly known as the Cumulative Distribution Function.

MATLAB基本上使用这种方法执行直方图均衡。但是,如果你想自己实现它,它实际上非常简单。假设您有一个输入图像 im ,它是一个无符号的8位整数类型。

MATLAB essentially performs histogram equalization using this approach. However, if you want to implement this yourself, it's actually pretty simple. Assume that you have an input image im that is of an unsigned 8-bit integer type.

function [out] = hist_eq(im, L)

if (~exist(L, 'var'))
    L = 256;
end

h = imhist(im) / numel(im);
cdf = cumsum(h);
out = (L-1)*cdf(double(im)+1);
out = uint8(out);

此函数接收假定为无符号8位整数的图像。您可以选择指定输出的级别数。通常,对于8位图像, L = 256 ,因此如果省略第二个参数,则假设 L 因此。第一行计算概率。下一行计算累积分布函数(CDF)。计算输入/输出后的下两行使用直方图均衡,然后转换回无符号的8位整数。请注意, uint8 投射会隐式执行 floor 操作。您需要注意我们在访问CDF时必须添加偏移量1 。原因是因为MATLAB开始索引为1,而图像的强度从0开始。

This function takes in an image that is assumed to be unsigned 8-bit integer. You can optionally specify the number of levels for the output. Usually, L = 256 for an 8-bit image and so if you omit the second parameter, L would be assumed as such. The first line computes the probabilities. The next line computes the Cumulative Distribution Function (CDF). The next two lines after compute input/output using histogram equalization, and then convert back to unsigned 8-bit integer. Note that the uint8 casting implicitly performs the floor operation for us. You'll need to take note that we have to add an offset of 1 when accessing the CDF. The reason why is because MATLAB starts indexing at 1, while the intensities in your image start at 0.

MATLAB命令 histeq 几乎做同样的事情,除非你打电话给 histeq(im),它假设你的图像中有32个强度。因此,您可以通过指定一个额外的参数来覆盖 histeq 函数,该参数指定在图像中看到的强度值的数量,就像我们上面所做的那样。因此,你会做 histeq(im,256); 。在MATLAB中调用它,并使用我上面写的函数应该给你相同的结果。

The MATLAB command histeq pretty much does the same thing, except that if you call histeq(im), it assumes that you have 32 intensities in your image. Therefore, you can override the histeq function by specifying an additional parameter that specifies how many intensity values are seen in the image just like what we did above. As such, you would do histeq(im, 256);. Calling this in MATLAB, and using the function I wrote above should give you identical results.

作为一个练习,让我们使用一个作为MATLAB发行版的一部分的图像,名为 pout.tif 。我们还要显示它的直方图。

As a bit of an exercise, let's use an image that is part of the MATLAB distribution called pout.tif. Let's also show its histogram.

im = imread('pout.tif');
figure;
subplot(2,1,1);
imshow(im);
subplot(2,1,2);
imhist(im);

如您所见,图像的对比度较差,因为大多数强度值都适合窄范围。直方图均衡化将使图像变平并因此增加图像的对比度。因此,尝试这样做:

As you can see, the image has poor contrast because most of the intensity values fit in a narrow range. Histogram equalization will flatten the image and thus increase the contrast of the image. As such, try doing this:

out = histeq(im, 256); %//or you can use my function: out = hist_eq(im);
figure;
subplot(2,1,1);
imshow(out);
subplot(2,1,2);
imhist(out);

这是我们得到的:

正如您所见对比度更好。较暗的像素倾向于朝向较暗的一端移动,而较亮的像素被推向较轻的一端。我认为成功的结果!请记住,当您尝试进行直方图均衡时,并非所有图像都能为您提供良好的结果。图像处理主要是反复试验,因此你将不同技术的混合物放在一起,直到你得到一个好的结果。

As you can see the contrast is better. Darker pixels tend to move towards the darker end, while lighter pixels get pushed towards the lighter end. Successful result I think! Bear in mind that not all images will give you a good result when you try and do histogram equalization. Image processing is mostly a trial and error thing, and so you put a mishmash of different techniques together until you get a good result.

这应该有希望让你开始。祝你好运!

This should hopefully get you started. Good luck!

这篇关于MATLAB中直方图均衡功能的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆