4 #include "ImageIO/ImageIO.h" 6 #include "util/fs/File.h" 7 #include "util/fs/Directory.h" 17 template<
class I,
class S>
21 using ImgPixelType = I;
22 using SegPixelType = S;
27 using ImgContainerType = std::vector<ImgType>;
28 using SegContainerType = std::vector<SegType>;
30 using ConstImgIterator =
typename ImgContainerType::const_iterator;
31 using ConstSegIterator =
typename SegContainerType::const_iterator;
45 void Add(
const std::string &imgFileName,
const std::string &segFileName);
65 const std::string &imgFileName =
"",
66 const std::string &segFileName =
"");
69 void ReadFromConfig(
const std::string &fileName);
100 inline const ImgType & GetImage(
size_t i)
const;
101 inline const SegType & GetSegmentation(
size_t i)
const;
103 inline std::string GetImageName(
size_t i)
const;
104 inline std::string GetSegmentationName(
size_t j)
const;
106 inline size_t GetImageCount()
const;
107 inline bool IsEmpty()
const;
110 size_t GetImageHeight()
const {
return imageHeight; }
111 size_t GetImageWidth()
const {
return imageWidth; }
114 inline ConstImgIterator img_cbegin()
const {
return images.cbegin(); }
115 inline ConstSegIterator seg_cbegin()
const {
return segmentations.cbegin(); }
116 inline ConstImgIterator img_cend()
const {
return images.end(); }
117 inline ConstSegIterator seg_cend()
const {
return segmentations.cend(); }
120 void ReadFromJson(
const util::Json &jsonConfig);
127 const std::string §ion,
128 std::vector<std::string> &absFiles);
131 ImgContainerType images;
132 SegContainerType segmentations;
134 std::vector<std::string> imgNames;
135 std::vector<std::string> segNames;
144 template<
class I,
class S>
146 const std::string &segFileName)
148 auto imgMat = ImageIO::ReadImage<ImgPixelType>(imgFileName);
149 auto segMat = ImageIO::ReadImage<SegPixelType>(segFileName);
151 Add(imgMat, segMat, imgFileName, segFileName);
155 template <
class I,
class S>
158 const std::string &imgFileName,
159 const std::string &segFileName)
162 if (imgMat.isEmpty() || segMat.isEmpty())
163 throw std::runtime_error(
"Loaded empty image or segmentation!");
166 if (imgMat.getHeight() != segMat.getHeight() ||
167 imgMat.getWidth() != segMat.getWidth())
168 throw std::runtime_error(
"Image/segmentation size mismatch!");
171 if (images.size() == 0) {
173 imageHeight = imgMat.getHeight();
174 imageWidth = imgMat.getWidth();
176 assert(imageHeight != 0 && imageWidth != 0 &&
177 "Only one of image dimensions is 0!");
180 if (imgMat.getHeight() != imageHeight || imgMat.getWidth() != imageWidth)
181 throw std::runtime_error(
182 "Size of new image/segmentation doesn't suit the database!");
186 images.push_back(imgMat);
187 segmentations.push_back(segMat);
190 imgNames.push_back(imgFileName);
191 segNames.push_back(segFileName);
195 template<
class I,
class S>
198 return ReadFromJson(util::JsonFromFile(fileName));
202 template <
class I,
class S>
206 auto name = jsonConfig[
"name"];
208 dbName = name.string_value();
210 std::vector<std::string> imagesFiles;
211 ParseConfigSection(jsonConfig,
"images", imagesFiles);
213 std::vector<std::string> segFiles;
214 ParseConfigSection(jsonConfig,
"segmentations", segFiles);
216 if (imagesFiles.size() != segFiles.size())
217 throw std::runtime_error(
"Images / segmentations files count mismatch");
220 for (
size_t i = 0; i < imagesFiles.size(); ++i)
221 Add(imagesFiles[i], segFiles[i]);
225 template<
class I,
class S>
227 const std::string §ion,
228 std::vector<std::string> &absFiles)
230 std::string folder =
"";
231 std::string extension =
"";
232 std::vector<std::string> configFiles;
236 auto jsonSection = config[section];
237 if (jsonSection.is_null())
238 throw std::runtime_error(
"'" + section +
"' section not found in config");
250 auto folderSection = jsonSection[
"folder"];
251 auto filesSection = jsonSection[
"files"];
252 auto extensionSection = jsonSection[
"extension"];
253 if (!folderSection.is_string() && !filesSection.is_array())
254 throw std::runtime_error(
"'files' and 'folder' subsections not found for '" 255 + section +
"' section");
257 if (folderSection.is_string())
258 folder = folderSection.string_value();
260 if (extensionSection.is_string())
261 extension = extensionSection.string_value();
263 if (filesSection.is_array())
264 for (
const auto &item : filesSection.array_items()) {
265 if (!item.is_string())
266 throw std::runtime_error(
"'files' section must contain only strings");
267 configFiles.push_back(item.string_value());
271 if (configFiles.empty()) {
272 absFiles = util::ListDir(folder, extension,
true);
274 for (
const auto &f : configFiles)
275 absFiles.push_back(util::ConcatPaths(folder, f));
280 template<
class I,
class S>
283 std::ifstream ifs(fileName);
285 std::string imgFileName, segFileName;
286 while (ifs >> imgFileName) {
287 if (!(ifs >> segFileName))
289 Add(imgFileName, segFileName);
295 template<
class I,
class S>
303 template<
class I,
class S>
307 segmentations.clear();
308 imageHeight = imageWidth = 0;
312 template<
class I,
class S>
316 assert(i < images.size() &&
"Image index is out of range!");
323 template<
class I,
class S>
327 assert(i < segmentations.size() &&
"Segmentation index is out of range!");
329 return segmentations[i];
333 template <
class I,
class S>
336 assert(i < imgNames.size() &&
"Index of image name is out of range!");
342 template <
class I,
class S>
345 assert(i < segNames.size() &&
"Index of segmentation name is out of range!");
351 template<
class I,
class S>
354 assert(images.size() == segmentations.size() &&
355 "Image and segmentation databases must have the same size!");
356 return images.size();
360 template<
class I,
class S>
363 assert(images.size() == segmentations.size() &&
364 "Image and segmentation databases must have the same size!");
365 return images.size() == 0;
void AppendFilesFromList(const std::string &fileName)
Read a list of filenames as 'image, segmentation' pairs and add them to the database.
Definition: ImageDatabase.h:281
Definition: ImageDatabase.h:18
void Clear()
Clear the contents of the database.
Definition: ImageDatabase.h:304
Header file declaring interface for various operations with JSON.
void Add(const std::string &imgFileName, const std::string &segFileName)
Adds a pair of image and its segmentation.
Definition: ImageDatabase.h:145
Definition: json11.hpp:79
void ReadFilesFromList(const std::string &fileName)
Clear the database and read from list.
Definition: ImageDatabase.h:296