Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnImagePyramidCache.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: drwnImagePyramidCache.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 // drwnImagePyramidCache -----------------------------------------------------
31 
33 protected:
34  vector<string> _filenames;
35 
36  vector<vector<cv::Mat> > _images;
37  vector<list<unsigned>::iterator> _free_list_map;
38  vector<unsigned> _lock_counter;
39 
41  list<unsigned> _free_list;
42 
43  bool _bGreyImages;
45  size_t _imagesLoaded;
46  size_t _memoryUsed;
47 
48  double _downSampleRate;
50  int _maxLevels;
51 
52 
53 #ifdef DRWN_USE_PTHREADS
54  pthread_mutex_t _mutex;
55 #endif
56 
57  // debug statistics
58  unsigned _dbImagesLocked;
59  unsigned _dbImagesLoaded;
60  size_t _dbMaxMemUsed;
61 
62 public:
63  static size_t MAX_IMAGES;
64  static size_t MAX_MEMORY;
65  static bool GREY_IMAGES;
66  static bool BIG_MEMORY;
67 
68 public:
70  drwnImagePyramidCache(double downSampleRate = 0.5, int minSize = 32,
71  int maxLevels = DRWN_INT_MAX, bool bGreyImages = GREY_IMAGES,
72  bool bBigMemory = BIG_MEMORY);
74  drwnImagePyramidCache(const vector<string>& filenames, double downSampleRate = 0.5,
75  int minSize = 32, int maxLevels = DRWN_INT_MAX, bool bGreyImages = GREY_IMAGES,
76  bool bBigMemory = BIG_MEMORY);
79 
81  void initialize(const vector<string>& filenames);
82 
84  void append(const string& filename);
85 
88  void clear();
89 
91  bool empty() const { return (_imagesLoaded == 0); }
93  size_t size() const { return _imagesLoaded; }
95  size_t numLocked() const { return _imagesLoaded - _free_list.size(); }
97  size_t memory() const { return _memoryUsed; }
98 
100  const string& filename(unsigned indx) const { return _filenames[indx]; }
101 
103  int index(const string& filename) const {
104  vector<string>::const_iterator it = find(_filenames.begin(), _filenames.end(), filename);
105  if (it == _filenames.end()) return -1;
106  return (int)(it - _filenames.begin());
107  }
108 
110  void lock(unsigned indx);
112  void lock(const string& filename) { lock((unsigned)index(filename)); }
113 
115  inline size_t levels(unsigned indx) const { return _images[indx].size(); }
117  inline size_t levels(const string& filename) const {
118  return _images[(unsigned)index(filename)].size();
119  }
120 
122  inline const cv::Mat& get(unsigned indx, unsigned level = 0) const {
123  return _images[indx][level];
124  }
126  inline const cv::Mat& get(const string& filename, unsigned level = 0) const {
127  return _images[(unsigned)index(filename)][level];
128  }
129 
131  void unlock(unsigned indx);
133  void unlock(const string& filename) { unlock((unsigned)index(filename)); }
134 
136  cv::Mat copy(unsigned indx, unsigned level = 0);
138  cv::Mat copy(const string& filename, unsigned level = 0) {
139  return copy((unsigned)index(filename), level);
140  }
141 
142 protected:
144  void enforceLimits();
145 
147  size_t memory(const vector<cv::Mat>& images) const;
148 
150  vector<cv::Mat> load(const string& filename) const;
151 };
152 
list< unsigned > _free_list
index of images that can be safely released in least-recently-used order
Definition: drwnImagePyramidCache.h:41
const string & filename(unsigned indx) const
return the filename for image indx
Definition: drwnImagePyramidCache.h:100
int _minImageSize
minimum image dimension
Definition: drwnImagePyramidCache.h:49
vector< list< unsigned >::iterator > _free_list_map
ref to location in _free_list
Definition: drwnImagePyramidCache.h:37
void lock(const string &filename)
lock an image pyramid for use (loads if not already in the cache)
Definition: drwnImagePyramidCache.h:112
int index(const string &filename) const
returns the index for filename (slow)
Definition: drwnImagePyramidCache.h:103
size_t numLocked() const
returns number of locked image pyramids in the cache
Definition: drwnImagePyramidCache.h:95
bool empty() const
returns true if the cache is empty (but may still be initialized with filenames)
Definition: drwnImagePyramidCache.h:91
bool _bBigMemoryModel
load all images into memory (ignores MAX_IMAGES and MAX_MEMORY)
Definition: drwnImagePyramidCache.h:44
size_t _imagesLoaded
number of images loaded
Definition: drwnImagePyramidCache.h:45
static size_t MAX_IMAGES
maximum number of images in memory at any one time
Definition: drwnImagePyramidCache.h:63
int _maxLevels
maximum pyramid height
Definition: drwnImagePyramidCache.h:50
static size_t MAX_MEMORY
maximum number of bytes used at any one time
Definition: drwnImagePyramidCache.h:64
size_t levels(unsigned indx) const
returns the number of levels in an image pyramid (which must first be locked)
Definition: drwnImagePyramidCache.h:115
static bool BIG_MEMORY
default setting for big memory mode
Definition: drwnImagePyramidCache.h:66
static bool GREY_IMAGES
default setting for caching images in greyscale
Definition: drwnImagePyramidCache.h:65
vector< unsigned > _lock_counter
allows for multiple locks
Definition: drwnImagePyramidCache.h:38
double _downSampleRate
downsampling rate
Definition: drwnImagePyramidCache.h:48
size_t levels(const string &filename) const
returns the number of levels in an image pyramid (which must first be locked)
Definition: drwnImagePyramidCache.h:117
size_t memory() const
returns memory used by in-memory image pyramids
Definition: drwnImagePyramidCache.h:97
cv::Mat copy(const string &filename, unsigned level=0)
copies an image without locking it (caller must free the image)
Definition: drwnImagePyramidCache.h:138
size_t size() const
returns number of image pyramids stored in the cache
Definition: drwnImagePyramidCache.h:93
vector< string > _filenames
image filenames
Definition: drwnImagePyramidCache.h:34
Caches image pyramids in main memory up to a maximum number of images or memory limit.
Definition: drwnImagePyramidCache.h:32
vector< vector< cv::Mat > > _images
image pyramids
Definition: drwnImagePyramidCache.h:36
void unlock(const string &filename)
marks an image as free
Definition: drwnImagePyramidCache.h:133
size_t _memoryUsed
bytes used by loaded images
Definition: drwnImagePyramidCache.h:46
bool _bGreyImages
store images in greyscale (instead of RGB)
Definition: drwnImagePyramidCache.h:43