-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimglance.m
125 lines (122 loc) · 4.62 KB
/
imglance.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
function [f,a,ih] = imglance(im, scale)
% Get a quick look at an image, usually while debugging.
% Behaves somewhat like imshow(), but imshow() is suboptimal in several ways,
% IMHO.
% Note to self: The purpose of this is to plot something reasonable in all cases.
% Don't change it just becuse it doesn't plot something perfect in some weird case.
if ~exist('scale', 'var') || isempty(scale) ,
scale = '' ; % Let imglance() decide about scaling
end
if strcmp(scale, 'scale') || strcmp(scale, 'dont') || strcmp(scale, '') ,
% do nothing, all is well
else
error('scale_string should be one of: ''scale'', ''dont'' or ''''') ;
end
if ismatrix(im) ,
% Display as a grayscale image
if isa(im, 'uint8') && (isempty(scale) || strcmp(scale, 'dont')) ,
% Display with no scaling
f = figure() ;
a = axes(f) ;
ih = image(a, im) ;
ih.CDataMapping = 'direct' ;
axis(a, 'equal') ;
axis(a, 'tight') ;
%clim(a, [0 255])
colormap(a, 'gray') ;
%colorbar(a) ;
else
lo = min(min(im)) ;
hi = max(max(im)) ;
im = double(im) ;
f = figure() ;
a = axes(f) ;
ih = image(a, im) ;
ih.CDataMapping = 'scaled' ;
axis(a, 'equal') ;
axis(a, 'tight') ;
if isempty(scale) ,
if 0<=lo && hi<=1 ,
clim(a, [0 1]) ;
do_add_colorbar = true ;
else
clim(a, [lo hi]) ;
do_add_colorbar = true ;
end
elseif strcmp(scale, 'scale')
clim(a, [lo hi]) ;
do_add_colorbar = true ;
elseif strcmp(scale, 'dont')
clim(a, [0 1]) ;
do_add_colorbar = true ;
else
error('Internal error: scale has an illegal value, ''%s''', scale) ;
end
colormap(a, 'gray') ;
if do_add_colorbar ,
colorbar(a) ;
end
end
else
if ndims(im) > 3 ,
error('imglance() doesn''t work on arrays of dimension %d > 3', ndims(im)) ;
end
[r,c,p] = size(im) ;
if p == 1 ,
% Convert to a 2D image and recurse
im = reshape(im, [r c]) ;
imglance(im, scale) ;
elseif p == 3 ,
if isa(im, 'uint8') && (isempty(scale) || strcmp(scale, 'dont')) ,
% Display with no scaling
f = figure() ;
a = axes(f) ;
ih = image(a, im) ;
axis(a, 'equal') ;
axis(a, 'tight') ;
colormap(a, 'gray') ;
else
im = double(im) ;
lo = min(min(min(im))) ;
hi = max(max(max(im))) ;
if isempty(scale) ,
if 0<=lo && hi<=1 ,
% Don't scale pels
else
% Check if the pixel values are ints on [0,255], otherwise scale.
if does_look_like_uint8(im, lo, hi) ,
% If all pels are integers on [0,255], convert to uint8
im = uint8(im) ;
else
% Scale all pixels, channels to be on [0,1]
% Scale using the min, max values in the image
im = (im-lo)/(hi-lo) ;
end
end
elseif strcmp(scale, 'scale')
% Scale all pixels, channels to be on [0,1]
im = (im-lo)/(hi-lo) ;
elseif strcmp(scale, 'dont')
% Don't scale pixels
else
error('Internal error: scale has an illegal value, ''%s''', scale) ;
end
f = figure() ;
a = axes(f) ;
ih = image(a, im) ;
axis(a, 'equal') ;
axis(a, 'tight') ;
end
else
error('imglance() doesn''t work on 3D arrays with %d pages. Page count must be 1 or 3.', p) ;
end
end
end
function result = does_look_like_uint8(im, lo, hi)
% Test if a double mxnx3 image has all integer pixel values on [0,255]
if 0<=lo && hi<=255 ,
result = all(all(all(im==round(im)))) ;
else
result = false ;
end
end