tofloat解决的问题
fft2会导致一定的问题,例如:f = ifft2(F)
fft2会将图像变成double类,因此反变换的过程有着一个陷阱:反变换后的图像类型可能和原来输入类型并不相同。
例如:f如果是uint8类型,就是范围在[0,255]内的整数,fft2将其变换成为double类型,通过ifft2将其变换成为了范围为[0,255],但是变成了double类,这会导致一些问题。
冈萨雷斯书籍中,使用tofloat函数解决这一问题,将输入的图像转化为[0,1]内的浮点数,通过revertclass这一参数将反变换中的图像变为和原图像相同的类型。
function [out,revertclass] = tofloat(in)
% tofloat将输入转换为浮点型,使用fft2将会导致标定的问题。
% revertclass将输出类型转化为和输入相同的类型
% 如果输入的图像不是single类型,tofloat将会将其转换为single类型
%
% implement:将所有图像类型以及操作存储起来(cell),通过函数句柄,将
% 非double和single的函数进行转化,将其转化为single类型的函数(im2single)
%
% revertclass 通过im2xx将其转化为输入的类型
identity = @(x) x;
tosingle = @im2single;
table = {'uint8', tosingle, @im2uint8
'uint16',tosingle,@im2uint16
'int16',tosingle,@im2int16
'logical',tosingle,@logical
'double',identity,identity
'single',identity,identity};
classIndex = find(strcmp(class(in),table(:,1)));
% class(in) return the type of the input image, then compare with
% the first column in the table(cell).
if isempty(classIndex)
error('Unsupported input image class');
end
% determine whether to find
out = table{classIndex,2}(in);
revertclass = table{classIndex,3};
元胞cell说明
元胞数组是一种包含名为元胞的索引数据容器的数据类型,其中的每个元胞都可以包含任意类型的数据。元胞数组通常包含文本列表、文本和数字的组合或者不同大小的数值数组。通过将索引括在圆括号 ()
中可以引用元胞集。使用花括号 {}
进行索引来访问元胞的内容。