MythTV  master
TemplateMatcher.h
Go to the documentation of this file.
1 /*
2  * TemplateMatcher
3  *
4  * Decide whether or not an image matches a given template.
5  *
6  * This TemplateMatcher is tuned to yield very few false positives, at the cost
7  * of slightly more false negatives.
8  *
9  * After all images are analyzed, attempt to remove the mis-categorized images
10  * based on inter-image information (e.g., minimum sequence lengths of template
11  * matching or non-matching).
12  *
13  * This TemplateMatcher only expects to successfully match templates in a fixed
14  * location. It would be trivial to add code to search the entire frame for a
15  * template known to change locations, but at a very large cost of time.
16  */
17 
18 #ifndef TEMPLATEMATCHER_H
19 #define TEMPLATEMATCHER_H
20 
21 extern "C" {
22 #include "libavcodec/avcodec.h" /* AVFrame */
23 }
24 #include "FrameAnalyzer.h"
25 
26 using AVFrame = struct AVFrame;
27 class PGMConverter;
28 class EdgeDetector;
29 class TemplateFinder;
30 
32 {
33 public:
34  /* Ctor/dtor. */
35  TemplateMatcher(std::shared_ptr<PGMConverter> pgmc,
36  std::shared_ptr<EdgeDetector> ed, TemplateFinder *tf,
37  const QString& debugdir);
38  ~TemplateMatcher(void) override;
39 
40  /* FrameAnalyzer interface. */
41  const char *name(void) const override // FrameAnalyzer
42  { return "TemplateMatcher"; }
44  long long nframes) override; // FrameAnalyzer
45  enum analyzeFrameResult analyzeFrame(const VideoFrame *frame,
46  long long frameno, long long *pNextFrame) override; // FrameAnalyzer
47  int finished(long long nframes, bool final) override; // FrameAnalyzer
48  int reportTime(void) const override; // FrameAnalyzer
49  FrameMap GetMap(unsigned int /*index*/) const override // FrameAnalyzer
50  { return m_breakMap; }
51 
52  /* TemplateMatcher interface. */
53  int templateCoverage(long long nframes, bool final) const;
54  const FrameAnalyzer::FrameMap *getBreaks(void) const { return &m_breakMap; }
55  int adjustForBlanks(const BlankFrameDetector *blankFrameDetector, long long nframes);
56  int computeBreaks(FrameMap *breaks);
57 
58 private:
59  std::shared_ptr<PGMConverter> m_pgmConverter {nullptr};
60  std::shared_ptr<EdgeDetector> m_edgeDetector {nullptr};
62  const struct AVFrame *m_tmpl {nullptr}; /* template image */
63  int m_tmplRow {-1}; /* template location */
64  int m_tmplCol {-1}; /* template location */
65  int m_tmplWidth {-1}; /* template dimensions */
66  int m_tmplHeight {-1}; /* template dimensions */
67 
68  /* Per-frame info. */
69  unsigned short *m_matches {nullptr}; /* matching pixels */
70  unsigned char *m_match {nullptr}; /* boolean result: 1/0 */
71 
72  float m_fps {0.0F};
73  AVFrame m_cropped {}; /* pre-allocated buffer */
74  FrameAnalyzer::FrameMap m_breakMap; /* frameno => nframes */
75 
76  /* Debugging */
77  int m_debugLevel {0};
78  QString m_debugDir;
79  QString m_debugData; /* filename */
80  MythPlayer *m_player {nullptr};
81  bool m_debugMatches {false};
82  bool m_debugRemoveRunts {false};
83  bool m_matchesDone {false};
84  struct timeval m_analyzeTime {0,0};
85 };
86 
87 #endif /* !TEMPLATEMATCHER_H */
88 
89 /* vim: set expandtab tabstop=4 shiftwidth=4: */
TemplateMatcher::m_tmplCol
int m_tmplCol
Definition: TemplateMatcher.h:64
TemplateMatcher::getBreaks
const FrameAnalyzer::FrameMap * getBreaks(void) const
Definition: TemplateMatcher.h:54
TemplateMatcher::m_player
MythPlayer * m_player
Definition: TemplateMatcher.h:80
TemplateMatcher::m_fps
float m_fps
Definition: TemplateMatcher.h:72
FrameMap
QMap< int, FrameList * > FrameMap
Definition: mpeg2fix.h:115
TemplateMatcher::m_edgeDetector
std::shared_ptr< EdgeDetector > m_edgeDetector
Definition: TemplateMatcher.h:60
TemplateMatcher::name
const char * name(void) const override
Definition: TemplateMatcher.h:41
FrameAnalyzer::FrameMap
QMap< long long, long long > FrameMap
Definition: FrameAnalyzer.h:45
TemplateMatcher::m_tmplRow
int m_tmplRow
Definition: TemplateMatcher.h:63
TemplateMatcher::m_debugLevel
int m_debugLevel
Definition: TemplateMatcher.h:77
TemplateMatcher::GetMap
FrameMap GetMap(unsigned int) const override
Definition: TemplateMatcher.h:49
MythPlayer
Definition: mythplayer.h:165
VideoFrame
Definition: mythframe.h:138
TemplateMatcher::m_breakMap
FrameAnalyzer::FrameMap m_breakMap
Definition: TemplateMatcher.h:74
AVFrame
struct AVFrame AVFrame
Definition: BorderDetector.h:15
FrameAnalyzer
Definition: FrameAnalyzer.h:29
TemplateFinder
Definition: TemplateFinder.h:31
TemplateMatcher::m_templateFinder
TemplateFinder * m_templateFinder
Definition: TemplateMatcher.h:61
TemplateMatcher::m_matchesDone
bool m_matchesDone
Definition: TemplateMatcher.h:83
TemplateMatcher::m_debugMatches
bool m_debugMatches
Definition: TemplateMatcher.h:81
TemplateMatcher::templateCoverage
int templateCoverage(long long nframes, bool final) const
Definition: TemplateMatcher.cpp:590
TemplateMatcher::TemplateMatcher
TemplateMatcher(std::shared_ptr< PGMConverter > pgmc, std::shared_ptr< EdgeDetector > ed, TemplateFinder *tf, const QString &debugdir)
Definition: TemplateMatcher.cpp:310
TemplateMatcher::m_debugDir
QString m_debugDir
Definition: TemplateMatcher.h:78
TemplateMatcher::analyzeFrame
enum analyzeFrameResult analyzeFrame(const VideoFrame *frame, long long frameno, long long *pNextFrame) override
Definition: TemplateMatcher.cpp:402
FrameAnalyzer::analyzeFrameResult
analyzeFrameResult
Definition: FrameAnalyzer.h:36
FrameAnalyzer.h
TemplateMatcher::m_pgmConverter
std::shared_ptr< PGMConverter > m_pgmConverter
Definition: TemplateMatcher.h:59
TemplateMatcher::m_cropped
AVFrame m_cropped
Definition: TemplateMatcher.h:73
TemplateMatcher::m_debugData
QString m_debugData
Definition: TemplateMatcher.h:79
TemplateMatcher::m_matches
unsigned short * m_matches
Definition: TemplateMatcher.h:69
EdgeDetector
Definition: EdgeDetector.h:28
BlankFrameDetector
Definition: BlankFrameDetector.h:16
TemplateMatcher::m_analyzeTime
Definition: TemplateMatcher.h:84
PGMConverter
Definition: PGMConverter.h:30
TemplateMatcher::m_debugRemoveRunts
bool m_debugRemoveRunts
Definition: TemplateMatcher.h:82
TemplateMatcher::finished
int finished(long long nframes, bool final) override
Definition: TemplateMatcher.cpp:480
TemplateMatcher::m_tmpl
const struct AVFrame * m_tmpl
Definition: TemplateMatcher.h:62
TemplateMatcher::~TemplateMatcher
~TemplateMatcher(void) override
Definition: TemplateMatcher.cpp:340
TemplateMatcher::computeBreaks
int computeBreaks(FrameMap *breaks)
Definition: TemplateMatcher.cpp:814
TemplateMatcher::reportTime
int reportTime(void) const override
Definition: TemplateMatcher.cpp:579
TemplateMatcher::m_tmplHeight
int m_tmplHeight
Definition: TemplateMatcher.h:66
TemplateMatcher::MythPlayerInited
enum analyzeFrameResult MythPlayerInited(MythPlayer *player, long long nframes) override
Definition: TemplateMatcher.cpp:348
TemplateMatcher::adjustForBlanks
int adjustForBlanks(const BlankFrameDetector *blankFrameDetector, long long nframes)
Definition: TemplateMatcher.cpp:646
TemplateMatcher::m_tmplWidth
int m_tmplWidth
Definition: TemplateMatcher.h:65
TemplateMatcher::m_match
unsigned char * m_match
Definition: TemplateMatcher.h:70
TemplateMatcher
Definition: TemplateMatcher.h:32