-
Notifications
You must be signed in to change notification settings - Fork 0
/
normImage.m
48 lines (46 loc) · 1.46 KB
/
normImage.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
%% normImage: Normalizes an image
% img: Image to be normalized
% INput (img) will be converted to double for the normalization,
% and converted back to the original datatype before the function returns
% varargin:
% imgMin: Minimum value for normalized image
% imgMax: Maximum value for normalized image
function out = normImage(img, varargin)
inClass = class(img); % Save for later
img = double(img);
% Determine min and max from call arguments
if length(varargin) == 2
imgMin = varargin{1};
imgMax = varargin{2};
elseif isempty(varargin)
imgMin = 0;
imgMax = 1;
else
error('Call as normImage(img) or normImage(img, min, max).')
end
% Perform normalization
out = (img - min(img(:)))*(imgMax - imgMin)/(max(img(:)) - min(img(:))) + imgMin;
% Determine, and cast back to, original datatype
if strcmp(inClass, 'double')
return
else
switch inClass
case 'int8'
out =int8(out);
case 'int16'
out =int16(out);
case 'int32'
out =int32(out);
case 'int64'
out =int64(out);
case 'uint8'
out =uint8(out);
case 'uint16'
out =uint16(out);
case 'uint32'
out =uint32(out);
otherwise
out = uint64(out);
end % switch
end % else
end % function