matlab - 不同堆叠图像的图像归一化、图像范围和图像缩放

标签 matlab image-processing normalization

我对图像归一化、图像范围和图像缩放感到很困惑。 我正在使用一种算法(我已将算法上传到我的 Previous Question 中),应用该算法后,我正在使用来自 wikipedia 的公式规范化图像:

enter image description here

使用来自 MATLAB 的 getrangefromclass(filtImag1{i}),应用算法之前矩阵的范围是 [0 255],应用算法之后范围是 [0 1]。

问题是我需要找一个引用来确定归一化公式是否正确?我还有 5 叠图像,每叠包含 600 张图像。我已经为每个堆栈应用了算法,因为算法的结果是每个堆栈有 10 张图像,所以我最终会得到 50 张图像,我需要分析并将它们放在一起比较。我找到 50 张图像的最大值和最小值,然后将每张图像传递到公式中以对图像进行归一化。

虽然图像的范围是 [0 1] 但图像的最大值是: 最大值 = 3.6714e+004

为什么?不应该是1吗? 这是正常化的正确方法吗? 我怎样才能应用缩放?我需要这样做吗?

这是规范化代码:

%%%%%%%%%%%%%%Find Min and Max between the results%%%%%%%%%%%%%%%%%%%%%%% 
pre_max = max(filtImag1{1}(:));
for i=1:10
   new_max = max(filtImag1{i}(:));
    if (pre_max<new_max)
        pre_max=max(filtImag1{i}(:));
    end
end
new_max = pre_max;

pre_min = min(filtImag1{1}(:));
for i=1:10
   new_min = min(filtImag1{i}(:));
    if (pre_min>new_min)
        pre_min = min(filtImag1{i}(:));
    end
end
new_min = pre_min;

%%%%%%%%%%%%%%normalization %%%%%%%%%%%%%%%%%%%%%%%
 for i=1:10
 temp_imag = filtImag1{i}(:,:);
 x=isnan(temp_imag);
 temp_imag(x)=0;
 t_max = max(max(temp_imag));
 t_min = min(min(temp_imag));
 temp_imag = (double(temp_imag-t_min)).*((double(new_max)-double(new_min))/double(t_max-t_min))+(double(new_min));
 imag_test2{i}(:,:) = temp_imag;
 end

编辑 我已经根据建议的答案更改了代码,但结果是黑色图像

%找到它们之间的最大值和最小值 pre_max = max(stStack{1}(:)); 因为我=1:40 newMax = max(stTStack{i}(:)); 如果(pre_max

pre_min = min(sTStack{1}(:)); 因为我=1:40 newMin = min(stTStack{i}(:)); 如果(pre_min>newMin) pre_min = min(stStack{i}(:)); 结尾 结尾 t_min = pre_min;

%%%%%%%%%%%%%%%%%%%%%%% 归一化图像:%%%%%%%%%%%%%%%%%%%%% %%%%%%%%

对于 i=1:40 NTstack{i} = (sTStack{i} - t_min)/(t_max-t_min); 结束

对于 i=10:10:40 图,imshow(NTstack{i}); 彩条 色图射流 结束

最佳答案

您提供的维基百科片段是正确的,可用于使用以下 MATLAB 代码规范化图像:

%% Create an Example Image:
rand('seed', 1982);
n = 16;
myImg= rand(n,n)*.2 + .5;

%% Normalize the Image:
myRange = getrangefromclass(myImg(1));
newMax = myRange(2);
newMin = myRange(1);

myImgNorm = (myImg - min(myImg(:)))*(newMax - newMin)/(max(myImg(:)) - min(myImg(:))) + newMin;

一些图像的问题在于,尽管它们只占据了很小范围的可能值。如果您的值可以介于 0 和 1 之间,那么黑色将为 0,白色将为 1。但是,如果图像中最暗的点是 .5 而最亮的点是 .7,那么它可能看起来对您的处理或用户何时可视化(请注意,正是出于这个原因,MATLAB 的 imagesc 会在显示之前自动规范化图像)。

如果您使用 hist(myImg(:)) 查看图像的直方图,您可以判断图像实际使用的允许值的必须程度。在标准化图像中,最小值为 0,最大值为 1(或您使用的任何范围)。

实现此等式的一个常见错误是没有正确放置括号,没有在缩放之前减去图像的最小值,或者没有在“newMin”中加回去。

您可以在下面的代码和图像中看到所有内容。请注意原始图像 (1) 如何只使用空间 (2) 的一小部分,因此当我们不让 imagesc 自动缩放 clim 参数时它看起来被冲掉了。然而,一旦我们对 (3) 进行归一化,图像就会同时具有非常暗和非常亮的值,并且直方图从 0 一直延伸到 1 (4)。虽然不清楚您的代码在做什么或没有做什么,但将其与此示例进行比较应该可以解决您的问题。

%% Create an Example Image:
rand('seed', 1982);
n = 16;
myImg= rand(n,n)*.2 + .5;

%% Normalize the Image:
myRange = getrangefromclass(myImg(1));
newMax = myRange(2);
newMin = myRange(1);

myImgNorm = (myImg - min(myImg(:)))*(newMax - newMin)/(max(myImg(:)) - min(myImg(:))) + newMin;

%% Display the Image:
figure(42);
clf;

% Display the original:
subplot(2,2,1);
imagesc(myImg);
set(gca, 'clim', [0,1]);;
title('(1) Original Image');

% Display the hist of the original:
subplot(2,2,3);
hist(myImg(:))
xlim([0,1]);
title('(2) Histogram Of Original Image');

% Display the normalized image:
subplot(2,2,2);
imagesc(myImgNorm);
title('(3) Normalized Image');

% Display the hist of the normalized image:
subplot(2,2,4);
hist(myImgNorm(:))
title('(4) Histogram of Normalized Image');
xlim([0,1]);

colormap gray

enter image description here

编辑:

此外,关于 getrangefromclass(...) 如何处理您的问题,还有一些要点需要注意。此函数返回“基于其类的图像的默认显示范围”——也就是说,它返回 MATLAB 认为该数据类型代表图片的合理值范围。对于 uint8 数据,这是 [0, 255]。对于 int16,这是 [-32768, 32767]。对于您的情况,double,范围是 [0, 1] 不是因为这是最小值和最大值,而是因为这是常规的,并且 double 数据类型具有特殊的表示形式,使该范围非常合理。 请注意,范围与您的实际数据无关。如果您的数据小于或大于最小值,则最大值将与 MATLAB 认为适合图片的值大不相同。在 double 或 single 的情况下,您的值(value)可能会大得多。要将值标准化到 [0, 1] 之间,我们可以使用我们一直在讨论的代码。

在这种情况下,我们创建了一个具有较大正值和负值的随机图像,但我们会将它们全部缩放到介于 0 和 1 之间。也就是说,我们将最暗的颜色设为 0,将最亮的颜色设为 1——而之前最小的是负千,最大的是正千。但是,请注意直方图形状如何在 x 轴值更改为 0,1 时保持不变。这应该可以证明为什么 MATLAB 范围是 [0,1] 但您的最小值/最大值不同——这很好,您的规范化代码将修复 0 和 1 之间的所有内容。

randn('seed', 1982);
myImg = randn(n,n)*1000;
% Normalize the Image:
myRange = getrangefromclass(myImg(1));
newMax = myRange(2);
newMin = myRange(1);

myImgNorm = (myImg - min(myImg(:)))*(newMax - newMin)/(max(myImg(:)) - min(myImg(:))) + newMin;

% Display the Image:
figure(42);
clf;

% Display the original:
subplot(2,2,1);
imagesc(myImg);
% set(gca, 'clim', [0,1]);;
title('(1) Original Image');
colorbar
% Display the hist of the original:
subplot(2,2,3);
hist(myImg(:))
title('(2) Histogram Of Original Image');
axis tight;

% Display the normalized image:
subplot(2,2,2);
imagesc(myImgNorm);
title('(3) Normalized Image');
colorbar

% Display the hist of the normalized image:
subplot(2,2,4);
hist(myImgNorm(:))
title('(4) Histogram of Normalized Image');
axis tight;

colormap gray

enter image description here

关于matlab - 不同堆叠图像的图像归一化、图像范围和图像缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11178925/

相关文章:

sql - 向非程序员解释为什么 "Just add another column to the DB"是个坏主意

c++ - 在 C++ 代码中集成 matlab C++ 共享库

c++ - 通过 mex 使用 matlab 运行 opencv 代码失败,而在 VisualStudio 上它可以工作

matlab - 在 MATLAB 中验证输入的最佳实践

c# - 计算 FFT 相关系数

algorithm - 白平衡算法

opencv - 霍夫变换线位置(OpenCV)

javascript - 将 0 - 1 中的任何数字归一化的函数

matlab - matlab 将某个位设置为 1

Python Pandas 数据框 : Normalize data between 0. 01 和 0.99?