Introduction
Image datasets are becoming increasingly popular in deep learning.
mlpack's image saving/loading functionality is based on stb/.
Table of Contents
This tutorial is split into the following sections:
Model API
Image utilities supports loading and saving of images.
It supports filetypes "jpg", "png", "tga","bmp", "psd", "gif", "hdr", "pic", "pnm" for loading and "jpg", "png", "tga", "bmp", "hdr" for saving.
The datatype associated is unsigned char to support RGB values in the range 1-255. To feed data into the network typecast of arma::Mat
may be required. Images are stored in matrix as (width * height * channels, NumberOfImages). Therefore imageMatrix.col(0) would be the first image if images are loaded in imageMatrix.
ImageInfo
ImageInfo class contains the metadata of the images.
ImageInfo(const size_t width,
const size_t height,
const size_t channels);
Other public memebers include:
- quality Compression of the image if saved as jpg (0-100).
Load
Standalone loading of images.
template<typename eT>
bool Load(
const std::string& filename,
arma::Mat<eT>& matrix,
ImageInfo& info,
const bool fatal,
const bool transpose);
bool Load(const std::string &filename, arma::Mat< eT > &matrix, const bool fatal=false, const bool transpose=true, const arma::file_type inputLoadType=arma::auto_detect)
Loads a matrix from file, guessing the filetype from the extension.
Loading a test image. It also fills up the ImageInfo class object.
data::ImageInfo info;
data::Load(
"test_image.png", matrix, info,
false,
true);
ImageInfo requires height, width, number of channels of the image.
size_t height = 64, width = 64, channels = 1;
data::ImageInfo info(width, height, channels);
More than one image can be loaded into the same matrix.
Loading multiple images:
template<typename eT>
bool Load(
const std::vector<std::string>& files,
arma::Mat<eT>& matrix,
ImageInfo& info,
const bool fatal,
const bool transpose);
data::ImageInfo info;
std::vector<std::string>> files{"test_image1.bmp","test_image2.bmp"};
data::load(files, matrix, info, false, true);
Save
Save images expects a matrix of type unsigned char in the form (width * height * channels, NumberOfImages). Just like load it can be used to save one image or multiple images. Besides image data it also expects the shape of the image as input (width, height, channels).
Saving one image:
template<typename eT>
bool Save(
const std::string& filename,
arma::Mat<eT>& matrix,
ImageInfo& info,
const bool fatal,
const bool transpose);
bool Save(const std::string &filename, const arma::Mat< eT > &matrix, const bool fatal=false, bool transpose=true, arma::file_type inputSaveType=arma::auto_detect)
Saves a matrix to file, guessing the filetype from the extension.
data::ImageInfo info;
info.width = info.height = 25;
info.channels = 3;
info.quality = 90;
data::Save(
"test_image.bmp", matrix, info,
false,
true);
If the matrix contains more than one image, only the first one is saved.
Saving multiple images:
template<typename eT>
bool Save(
const std::vector<std::string>& files,
arma::Mat<eT>& matrix,
ImageInfo& info,
const bool fatal,
const bool transpose);
data::ImageInfo info;
info.width = info.height = 25;
info.channels = 3;
info.quality = 90;
std::vector<std::string>> files{"test_image1.bmp", "test_image2.bmp"};
Multiple images are saved according to the vector of filenames specified.