Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnImageCache.h
1 /*****************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnImageCache.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
13 #pragma once
14 
15 #include <cstdlib>
16 #include <cassert>
17 #include <map>
18 
19 #include "cv.h"
20 
21 #include "drwnBase.h"
22 #include "drwnIO.h"
23 
24 using namespace std;
25 
26 // drwnImageCache ------------------------------------------------------------
64 
66 protected:
67  vector<string> _filenames;
68 
69  vector<cv::Mat> _images;
70  vector<list<unsigned>::iterator> _free_list_map;
71  vector<unsigned> _lock_counter;
72 
74  list<unsigned> _free_list;
75 
76  bool _bGreyImages;
78  size_t _imagesLoaded;
79  size_t _memoryUsed;
80 
81 #ifdef DRWN_USE_PTHREADS
82  pthread_mutex_t _mutex; // thread safety
83 #endif
84 
85  // debug statistics
86  unsigned _dbImagesLocked;
87  unsigned _dbImagesLoaded;
88  size_t _dbMaxMemUsed;
89 
90 public:
91  static size_t MAX_IMAGES;
92  static size_t MAX_MEMORY;
93  static bool GREY_IMAGES;
94  static bool BIG_MEMORY;
95 
96 public:
98  drwnImageCache(bool bGreyImages = GREY_IMAGES, bool bBigMemory = BIG_MEMORY);
100  drwnImageCache(const vector<string>& filenames, bool bGreyImages = GREY_IMAGES,
101  bool bBigMemory = BIG_MEMORY);
103  virtual ~drwnImageCache();
104 
106  void initialize(const vector<string>& filenames);
107 
109  void append(const string& filename);
110 
113  void clear();
114 
116  bool empty() const { return (_imagesLoaded == 0); }
118  size_t size() const { return _imagesLoaded; }
120  size_t numLocked() const { return _imagesLoaded - _free_list.size(); }
122  size_t memory() const { return _memoryUsed; }
123 
125  const string& filename(unsigned indx) const { return _filenames[indx]; }
126 
128  int index(const string& filename) const {
129  vector<string>::const_iterator it = find(_filenames.begin(), _filenames.end(), filename);
130  if (it == _filenames.end()) return -1;
131  return (int)(it - _filenames.begin());
132  }
133 
135  void lock(unsigned indx);
137  void lock(const string& filename) { lock((unsigned)index(filename)); }
138 
140  inline const cv::Mat& get(unsigned indx) const { return _images[indx]; }
142  inline const cv::Mat& get(const string& filename) const {
143  return _images[(unsigned)index(filename)];
144  }
145 
147  void unlock(unsigned indx);
149  void unlock(const string& filename) { unlock((unsigned)index(filename)); }
150 
152  cv::Mat copy(unsigned indx);
154  cv::Mat copy(const string& filename) { return copy((unsigned)index(filename)); }
155 
156 protected:
158  void enforceLimits();
159 
161  virtual cv::Mat load(const string& filename) const;
162 };
163 
bool empty() const
returns true if the cache is empty (but may still be initialized with filenames)
Definition: drwnImageCache.h:116
vector< string > _filenames
image filenames
Definition: drwnImageCache.h:67
size_t _imagesLoaded
number of images loaded
Definition: drwnImageCache.h:78
vector< list< unsigned >::iterator > _free_list_map
ref to location in _free_list
Definition: drwnImageCache.h:70
Caches images in memory up to a maximum number of images or memory limit.
Definition: drwnImageCache.h:65
static bool GREY_IMAGES
default setting for caching images in greyscale
Definition: drwnImageCache.h:93
size_t size() const
returns number of images stored in the cache
Definition: drwnImageCache.h:118
static size_t MAX_MEMORY
maximum number of bytes used at any one time
Definition: drwnImageCache.h:92
vector< unsigned > _lock_counter
allows for multiple locks
Definition: drwnImageCache.h:71
size_t _memoryUsed
bytes used by loaded images
Definition: drwnImageCache.h:79
vector< cv::Mat > _images
loaded images
Definition: drwnImageCache.h:69
size_t numLocked() const
returns number of locked images in the cache
Definition: drwnImageCache.h:120
void unlock(const string &filename)
marks an image as free
Definition: drwnImageCache.h:149
size_t memory() const
returns memory used by in-memory images
Definition: drwnImageCache.h:122
static size_t MAX_IMAGES
maximum number of images in memory at any one time
Definition: drwnImageCache.h:91
bool _bBigMemoryModel
load all images into memory (ignores MAX_IMAGES and MAX_MEMORY)
Definition: drwnImageCache.h:77
const string & filename(unsigned indx) const
return the filename for image indx
Definition: drwnImageCache.h:125
cv::Mat copy(const string &filename)
copies an image without locking it (caller must free the image)
Definition: drwnImageCache.h:154
int index(const string &filename) const
returns the index for filename (slow)
Definition: drwnImageCache.h:128
bool _bGreyImages
store images in greyscale (instead of RGB)
Definition: drwnImageCache.h:76
list< unsigned > _free_list
index of images that can be safely released in least-recently-used order
Definition: drwnImageCache.h:74
void lock(const string &filename)
lock an image for use (loads if not already in the cache)
Definition: drwnImageCache.h:137
static bool BIG_MEMORY
default setting for big memory mode
Definition: drwnImageCache.h:94