两个图像的直方图匹配,不使用histeq [英] Histogram matching of two Images without using histeq

查看:384
本文介绍了两个图像的直方图匹配,不使用histeq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,MATLAB中的 histeq 可以执行直方图匹配,以便将图像的直方图转换为另一个直方图。我试图在不使用 histeq 的情况下执行相同的操作。我知道你需要计算两张图像之间的CDF,但我不知道接下来要做什么。我该怎么办?

It is well known that histeq in MATLAB can perform histogram matching so that an image's histogram is transformed to look like another histogram. I am trying to perform this same operation without using histeq. I'm aware that you need to calculate the CDFs between the two images, but I'm not sure what to do next. What do I do?

推荐答案

直方图匹配涉及转换一个图像的直方图,使其看起来像另一个。基本原则是单独计算每个图像的直方图,然后计算它们的离散累积分布函数(CDF)。我们将第一张图像的CDF表示为,而第二张图像的CDF是。因此,表示CDF值对于强度 x

Histogram matching is concerned with transforming one image's histogram so that it looks like another. The basic principle is to compute the histogram of each image individually, then compute their discrete cumulative distribution functions (CDFs). Let's denote the CDF of first image as while the CDF of the second image is . Therefore, would denote what the CDF value is for intensity x for the first image.

计算每张图片的CDF后,您需要计算从第一张图片转换一种强度的映射这使得它与第二图像的强度分布一致。要做到这一点,对于第一张图片中的每个强度 - 让我们称之为,它将来自 [0,255] 假设一个8位图像 - 我们必须找到一个强度 in第二张图片(也在 [0,255] 的范围内),以便:

Once you calculate the CDFs for each of the images, you need to compute a mapping that transforms one intensity from the first image so that it is in agreement with the intensity distribution of the second image. To do this, for each intensity in the first image - let's call this which will be from [0,255] assuming an 8-bit image - we must find an intensity in the second image (also in the range of [0,255]) such that:

可能有一个案例我们赢了' t 完全是一个相等,所以你需要做的是找到和。换句话说,对于映射 M ,对于,我们必须找到一个强度这样:

There may be a case where we won't get exactly an equality, so what you would need to do is find the smallest absolute difference between and . In other words, for a mapping M, for each entry of , we must find an intensity such that:

您将对所有256个值执行此操作,我们将生成映射。找到此映射后,您只需在第一个图像上应用此映射,使其看起来像第二个图像的强度分布。粗略(也许效率低下)的算法看起来像这样。让 im1 成为第一张图片(类型为 uint8 ),而 im2 是第二张图片(类型 uint8 ):

You would do this for all 256 values, and we would produce a mapping. Once you find this mapping, you simply have to apply this mapping on the first image to get it to look like the intensity distribution of the second image. A rough (and perhaps inefficient) algorithm would look something like this. Let im1 be the first image (of type uint8) while im2 is the second image (of type uint8):

M = zeros(256,1,'uint8'); %// Store mapping - Cast to uint8 to respect data type
hist1 = imhist(im1); %// Compute histograms
hist2 = imhist(im2);
cdf1 = cumsum(hist1) / numel(im1); %// Compute CDFs
cdf2 = cumsum(hist2) / numel(im2);

%// Compute the mapping
for idx = 1 : 256
    [~,ind] = min(abs(cdf1(idx) - cdf2));
    M(idx) = ind-1;
end

%// Now apply the mapping to get first image to make
%// the image look like the distribution of the second image
out = M(double(im1)+1);

out 应包含匹配的图像变换第一图像的强度分布以匹配第二图像的强度分布。特别注意 out 语句。 im1 的强度范围介于 [0,255] 之间,但MATLAB对数组的索引从 1开始。因此,我们需要为 im1 的每个值添加1,这样我们就可以正确地索引到 M 来生成输出。但是, im1 的类型为 uint8 ,如果您尝试超过255,MATLAB会使值饱和。因此,确保我们达到256,我们必须转换为超过8位精度的数据类型。我决定使用 double ,然后当我们为 im1 中的每个值加1时,我们将介于1到256之间所以我们可以正确地索引到 M 。当我找到最小化差异的位置时,我也必须减去1,因为数据类型跨越 [0,255]

out should contain your matched image where it transforms the intensity distribution of the first image to match that of the second image. Take special care of the out statement. The intensity range of im1 spans between [0,255], but MATLAB's indexing for arrays starts at 1. Therefore, we need to add 1 to every value of im1 so we can properly index into M to produce our output. However, im1 is of type uint8, and MATLAB saturates values should you try and go beyond 255. As such, to ensure that we get to 256, we must cast to a data type that is beyond 8-bit precision. I decided to use double, then when we add 1 to every value in im1, we will span between 1 to 256 so we can properly index into M. Also take not that when I find the location that minimizes the difference, I also must subtract by 1 as the data type spans from [0,255].

这篇关于两个图像的直方图匹配,不使用histeq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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