MATLAB中直方图均衡化函数的解释 [英] Explanation of the Histogram Equalization function in MATLAB

查看:22
本文介绍了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

然而,执行直方图均衡的最终方程本质上是一个 1 对 1 的映射.对于图像中的每个像素,您提取其强度,然后通过此函数运行它.然后它会为您提供一个输出强度以放置在您的输出图像中.

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 的直方图 bin 计数code> 并除以图像中的像素总数).鉴于您的图像具有 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.

作为一个练习,让我们使用一个名为 pout.tif 的图像,它是 MATLAB 发行版的一部分.让我们也展示一下它的直方图.

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天全站免登陆