function g=gscale(f,varargin)
% GSACLE函式實現對輸入影象的標度
% G = GSACLE(F, "full8")將影象標度到[0, 255]全範圍,預設值
% G = GSACLE(F, "full16")將影象標度到[0, 65535]全範圍
% G = GSACLE(F, "minmax", Low, High)將影象標度到[Low, High]全範圍,注意,Low,High
% 的範圍為[0, 1],具體範圍由輸入決定,即輸入輸出的class一致。
% 如果輸入影象是double類,並且值不在[0, 1]的範圍內,那麼在標度之前要進行轉換
if isempty(varargin) % 對程式碼進行最佳化,該語句比 if length(varargin) == 0 快
method="full8"; % 採用預設值
else
method=varargin{1};
end
% 預處理
if strcmp(class(f),"double")&(max(f(:))>1 | min(f(:))<0)
f=mat2gray(f);
% 實現標度
switch method
case "full8"
g=im2uint8(mat2gray(double(f)));
case "full16"
g=im2uint16(mat2gray(double(f)));
case "minmax"
low = varargin{2};high = varargin{3};
if low>1 | low<0 |high>1 | high<0 % 判斷引數是否合法
error("Parameters low and high must be in the range [0,1]")
if strcmp(class(f),"double")
low_in=min(f(:));
high_in=max(f(:));
elseif strcmp(class(f),"uint8")
low_in=double(min(f(:)))./255;
high_in=double(max(f(:)))./255;
elseif strcmp(class(f),"uint16")
low_in=double(min(f(:)))./65535;
high_in=double(max(f(:)))./65535;
g=imadjust(f,[low_in high_in],[low high]);
otherwise
error("Unknown method")
function g=gscale(f,varargin)
% GSACLE函式實現對輸入影象的標度
% G = GSACLE(F, "full8")將影象標度到[0, 255]全範圍,預設值
% G = GSACLE(F, "full16")將影象標度到[0, 65535]全範圍
% G = GSACLE(F, "minmax", Low, High)將影象標度到[Low, High]全範圍,注意,Low,High
% 的範圍為[0, 1],具體範圍由輸入決定,即輸入輸出的class一致。
% 如果輸入影象是double類,並且值不在[0, 1]的範圍內,那麼在標度之前要進行轉換
if isempty(varargin) % 對程式碼進行最佳化,該語句比 if length(varargin) == 0 快
method="full8"; % 採用預設值
else
method=varargin{1};
end
% 預處理
if strcmp(class(f),"double")&(max(f(:))>1 | min(f(:))<0)
f=mat2gray(f);
end
% 實現標度
switch method
case "full8"
g=im2uint8(mat2gray(double(f)));
case "full16"
g=im2uint16(mat2gray(double(f)));
case "minmax"
low = varargin{2};high = varargin{3};
if low>1 | low<0 |high>1 | high<0 % 判斷引數是否合法
error("Parameters low and high must be in the range [0,1]")
end
if strcmp(class(f),"double")
low_in=min(f(:));
high_in=max(f(:));
elseif strcmp(class(f),"uint8")
low_in=double(min(f(:)))./255;
high_in=double(max(f(:)))./255;
elseif strcmp(class(f),"uint16")
low_in=double(min(f(:)))./65535;
high_in=double(max(f(:)))./65535;
end
g=imadjust(f,[low_in high_in],[low high]);
otherwise
error("Unknown method")
end