我需要计算图像的累积分布函数。我使用以下代码对值进行了标准化:
im = imread('cameraman.tif');
im_hist = imhist(im);
tf = cumsum(im_hist); %transformation function
tf_norm = tf / max(tf);
plot(tf_norm), axis tight
此外,当绘制 CDF 函数时,该图是否必须是一条直线,理想情况下应该是一条直线来表示像素强度的相等表示?
您可以通过以下方式轻松获得 CDF:
A = imread('cameraman.tif');
[histIM, bins] = imhist(A);
cdf = cumsum(counts) / sum(counts);
plot(cdf); % If you want to be more precise on the X axis plot it against bins
对于著名的cameraman.tif
它导致:
至于你的第二个问题。当直方图完全均衡时(即,当每个强度对应的像素数量大致相同时)您的 CDF 将看起来像一条直线 45° 线。
编辑:严格来说 cumsum
,单独的 CDF 并不是一个合适的 CDF,因为 CDF 描述了概率,因此它必须遵守概率公理。特别是概率的第一公理告诉我们,概率值应该在范围内[0 ... 1]
,cumsum
单独并不能保证这一点。
function icdf = imgcdf(img)
% Author: Javier Montoya (jmontoyaz@gmail.com).
% http://www.lis.ic.unicamp.br/~jmontoya
%
% IMGCDF calculates the Cumulative Distribution Function of image I.
% Input parameters:
% img: image I (passed as a bidimensional matrix).
% Ouput parameters:
% icdf: cumulative distribution function.
%
% See also: IMGHIST
%
% Usage:
% I = imread('tire.tif');
% icdf = imgcdf(I);
% figure; stem(icdf); title('Cumulative Distribution Function (CDF)');
if exist('img', 'var') == 0
error('Error: Specify an input image.');
end
icdf = [];
ihist = imghist(img);
maxgval = 255;
icdf = zeros(1,maxgval);
icdf(1)= ihist(1);
for i=2:1:maxgval+1
icdf(i) = ihist(i) + icdf(i-1);
end
end
它不是我的代码,但它对我有用!还要检查统计工具箱中的 cdf 函数