26.1.8 Extracting data from images
Obtaining the size of an image.
The size of an image can be obtained by using the
size command.
-
size takes img, an image object.
- size(img) returns a list of two integers
representing the width and the height of img (in that order).
Obtaining pixel data.
Channel data can be extracted from image objects to matrices by using
the [] operator or the
channel_data command.
-
Let img be an image object.
- The command img[chn ⟨,x,y ⟨,w,h ⟩⟩],
where
-
chn is a channel index, which should be either a
nonnegative integer or red for the red channel, green for the
green channel, blue for the blue channel, grey for the
grayscale channel resp. tran for the transparency channel,
- (optionally) x, y are integers and
- (optionally) w, h are positive integers (if these are given, then
x and y must also be given),
returns
-
the full matrix of the channel chn if no optional arguments are given,
- the chn component of the pixel (x,y) if w and h are not given,
- the matrix of pixel data for the channel chn in the
rectangle with upper left corner (x,y) and lower right corner (x+w−1,y+h−1)
if x, y, w and h are given.
- If chn=grey and img is a color image, then the returned
pixel value(s) are obtained by averaging the color channels.
- The channel_data command extracts a channel (sub)matrix. Precisely,
channel_data(img,args) is equivalent to
img[args], where img is an image object
and args is a sequence of arguments as described above.
Flattening images.
The flatten command returns a list
representation of an image, which is suitable for e.g. passing the image to a neural
network. It accepts a single argument, an image object img, and returns the
list in which the pixel data is stored row by row, with individual channel values for
the same pixel occupying consecutive entries in the list. (See the MNIST example
in Section 20.2.2.)