You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.1 KiB
C
111 lines
3.1 KiB
C
|
4 years ago
|
/*!
|
||
|
|
* \file SampleInstance.h
|
||
|
|
* \date 2019/12/17
|
||
|
|
*
|
||
|
|
* \author Lin, Chi
|
||
|
|
* Contact: lin.chi@hzleaper.com
|
||
|
|
*
|
||
|
|
*
|
||
|
|
* \note
|
||
|
|
*/
|
||
|
|
|
||
|
|
#ifndef __SampleInstance_h_
|
||
|
|
#define __SampleInstance_h_
|
||
|
|
|
||
|
|
#include "StdUtils.h"
|
||
|
|
#include "DetectRoi.h"
|
||
|
|
#include <unordered_map>
|
||
|
|
|
||
|
|
/*! \brief Define a sample instance used for training or test, organized and stored by SampleDBManager
|
||
|
|
*/
|
||
|
|
struct SampleInstance
|
||
|
|
{
|
||
|
|
/*! filename(or rather relative path to root folder) */
|
||
|
|
std::string name;
|
||
|
|
/*! original image, could be empty if the instance is unloaded */
|
||
|
|
Mat img;
|
||
|
|
/*! original mask, generated by detection roi */
|
||
|
|
Mat mask;
|
||
|
|
/*! detect roi on original image, could be empty if the instance is unloaded */
|
||
|
|
DetectRoi droi;
|
||
|
|
|
||
|
|
/*! cached size-uniformed image */
|
||
|
|
Mat simg;
|
||
|
|
/*! cached size-uniformed mask */
|
||
|
|
Mat smask;
|
||
|
|
/*! cached feature vector */
|
||
|
|
Mat fv;
|
||
|
|
|
||
|
|
/*! User-defined properties. */
|
||
|
|
typedef std::map<int, Mat> UserDataMap;
|
||
|
|
UserDataMap userData;
|
||
|
|
|
||
|
|
SampleInstance() : rootFolder("") {}
|
||
|
|
SampleInstance(const std::string& dir, const std::string& n) : rootFolder(dir), name(n) {}
|
||
|
|
|
||
|
|
/*! load image and roi from disk
|
||
|
|
* @forceReload whether to always reload sample image from disk
|
||
|
|
* @size resize sample image to provided size, do nothing for null size
|
||
|
|
* @return true if already loaded or successfully loaded
|
||
|
|
*/
|
||
|
|
virtual bool load(bool forceReload = false, const Size& size = Size());
|
||
|
|
|
||
|
|
/** @overload */
|
||
|
|
virtual bool load(const Mat& sampleImg, DetectRoi& roi, const Size& size = Size());
|
||
|
|
|
||
|
|
/** @overload */
|
||
|
|
virtual bool load(const Mat& sampleImg, const Size& size = Size());
|
||
|
|
|
||
|
|
/*! cleanup loaded image and roi */
|
||
|
|
virtual void unload();
|
||
|
|
|
||
|
|
/*! save sample image and roi to disk */
|
||
|
|
virtual bool save();
|
||
|
|
|
||
|
|
/*! get thumbnail for preview */
|
||
|
|
virtual Mat getThumb();
|
||
|
|
|
||
|
|
/*! get full path to the sample file */
|
||
|
|
std::string getFullPath() const { return rootFolder + name; }
|
||
|
|
|
||
|
|
/*! clean up cached feature vector */
|
||
|
|
void cleanup() { fv = Mat(); }
|
||
|
|
|
||
|
|
/*! check whether the sample instance is located in provided folder */
|
||
|
|
bool isInFolder(const string& folderPath) const { return rootFolder == folderPath; }
|
||
|
|
|
||
|
|
/*! get where the sample instance is located */
|
||
|
|
const std::string& getFolder() const { return rootFolder; }
|
||
|
|
|
||
|
|
/*! query user data */
|
||
|
|
Mat getUserData(int idx) const {
|
||
|
|
auto it = userData.find(idx);
|
||
|
|
if (it == userData.end()) return Mat();
|
||
|
|
else return it->second;
|
||
|
|
}
|
||
|
|
|
||
|
|
/*! generate a new sample instance after apply transformation(shift, scale, rotate) */
|
||
|
|
virtual bool convert(double alpha, double beta, double dx, double dy,
|
||
|
|
std::shared_ptr<SampleInstance>& siPtr);
|
||
|
|
|
||
|
|
/*! whether this is a generated sample */
|
||
|
|
virtual bool isGenerated() const { return generated; }
|
||
|
|
|
||
|
|
private:
|
||
|
|
void refreshImgCache(const Size& size = Size());
|
||
|
|
Mat resizeImg(const Mat& src, const Size& size, int interFlag);
|
||
|
|
|
||
|
|
private:
|
||
|
|
const std::string& rootFolder; // root folder
|
||
|
|
bool generated = false;
|
||
|
|
|
||
|
|
};
|
||
|
|
typedef std::shared_ptr<SampleInstance> SampleInstPtr;
|
||
|
|
typedef std::vector<SampleInstPtr> SampleInstVec;
|
||
|
|
typedef std::unordered_map<std::string, SampleInstPtr> SampleInstMap;
|
||
|
|
typedef ObjectVectorIterator<SampleInstPtr> SampleInstIterator;
|
||
|
|
|
||
|
|
|
||
|
|
#endif // SampleInstance_h_
|
||
|
|
|