-
Notifications
You must be signed in to change notification settings - Fork 97
/
ImageSource.m
177 lines (154 loc) · 5.55 KB
/
ImageSource.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
%ImageSource Abstract class for image sources
%
% An abstract superclass for implementing image sources.
%
% Methods::
% grab Aquire and return the next image
% close Close the image source
% iscolor True if image is color
% size Size of image
% char Convert image source parameters to human readable string
% display Display image source parameters in human readable form
%
% See also AxisWebCamera, Video, Movie.
% Copyright (C) 1993-2011, by Peter I. Corke
%
% This file is part of The Machine Vision Toolbox for Matlab (MVTB).
%
% MVTB is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% MVTB is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU Lesser General Public License for more details.
%
% You should have received a copy of the GNU Leser General Public License
% along with MVTB. If not, see <http://www.gnu.org/licenses/>.
classdef ImageSource < handle
properties
width % width of each frame
height % height of each frame
color % is a color image
% options set at construction time
imageType
makeGrey
gamma
scaleFactor
args % other passed options (cell array)
end
methods (Abstract)
im = grab()
close()
paramSet()
end
methods
function imsource = ImageSource(varargin)
%ImageSource.ImageSource Image source constructor
%
% I = ImageSource(OPTIONS) is an ImageSource object that holds parameters
% related to acquisition from some particular image source.
%
% Options::
% 'width',W Set image width to W
% 'height',H Set image height to H
% 'uint8' Return image with uint8 pixels (default)
% 'int16' Return image with int16 pixels
% 'int32' Return image with int32 pixels
% 'float' Return image with float pixels
% 'double' Return image with double precision pixels
% 'grey' Return image is greyscale
% 'gamma',G Apply gamma correction with gamma=G
% 'scale',S Subsample the image by S in both directions.
% set default options
opt.imageType = {'uint8', 'float', 'double'};
opt.grey = false;
opt.gamma = [];
opt.scale = 1;
opt.width = [];
opt.height = [];
[opt,args] = tb_optparse(opt, varargin);
imsource.imageType = opt.imageType;
imsource.makeGrey = opt.grey;
imsource.gamma = opt.gamma;
imsource.scaleFactor = opt.scale;
imsource.width = opt.width;
imsource.height = opt.height;
imsource.args = args;
% remaining arguments get passed to the subclass
imsource.paramSet(args{:});
end
function b = iscolor(imsource)
b = imsource.color;
end
function im2 = convert(imsource, im)
im2 = [];
% apply options specified at construction time
if imsource.scaleFactor > 1
im = im(1:imsource.scaleFactor:end, 1:imsource.scaleFactor:end, :);
end
if imsource.makeGrey & (ndims(im) == 3)
im = imono(im);
end
if ~isempty(imsource.imageType)
switch imsource.imageType
case 'double'
im = idouble(im);
case 'float'
im = idouble(im, 'float');
otherwise
im = cast(im, imsource.imageType);
end
end
if ~isempty(imsource.gamma)
im = igamm(im, imsource.gamma);
end
if isempty(im2)
im2 = im;
end
end
function s = size(imsource)
s = [imsource.height imsource.width];
end
function s = char(imsource)
s = '';
if imsource.scaleFactor > 1
s = strcat(s, sprintf('subsample by %d: ', imsource.scaleFactor));
end
if imsource.makeGrey
s = strcat(s, 'convert to grey: ');
end
if ~isempty(imsource.imageType)
s = strcat(s, sprintf('cast to %s: ', imsource.imageType));
end
if ~isempty(imsource.gamma)
s = strcat(s, sprintf('gamma correct %f: ', imsource.gamma));
end
end
function display(imsource)
%ImageSource.display Display value
%
% I.display() displays the state of the image source object in human
% readable form.
%
% Notes::
% - This method is invoked implicitly at the command line when the result
% of an expression is an ImageSource object and the command has no trailing
% semicolon.
loose = strcmp( get(0, 'FormatSpacing'), 'loose');
if loose
disp(' ');
end
disp([inputname(1), ' = '])
if loose
disp(' ');
end
disp(char(imsource))
if loose
disp(' ');
end
end
end
end