MythTV  master
Histogram.cpp
Go to the documentation of this file.
1 #include <algorithm>
2 #include <cmath>
3 #include <cstring>
4 #include <string>
5 
6 #include "libmythtv/mythframe.h"
7 
8 #include "Histogram.h"
9 
10 void Histogram::generateFromImage(MythVideoFrame* frame, unsigned int frameWidth,
11  unsigned int frameHeight, unsigned int minScanX, unsigned int maxScanX,
12  unsigned int minScanY, unsigned int maxScanY, unsigned int XSpacing,
13  unsigned int YSpacing)
14 {
15  m_data.fill(0);
17 
18  maxScanX = std::min(maxScanX, frameWidth-1);
19 
20  maxScanY = std::min(maxScanY, frameHeight-1);
21 
22  unsigned char* framePtr = frame->m_buffer;
23  int bytesPerLine = frame->m_pitches[0];
24  for(unsigned int y = minScanY; y < maxScanY; y += YSpacing)
25  {
26  for(unsigned int x = minScanX; x < maxScanX; x += XSpacing)
27  {
28  m_data[framePtr[(y * bytesPerLine) + x]]++;
30  }
31  }
32 }
33 
34 unsigned int Histogram::getAverageIntensity(void) const
35 {
36  if (!m_numberOfSamples)
37  return 0;
38 
39  long value = 0;
40 
41  for(long i = 0; i < 256; i++)
42  {
43  value += m_data[i]*i;
44  }
45 
46  return value / m_numberOfSamples;
47 }
48 
49 unsigned int Histogram::getThresholdForPercentageOfPixels(float percentage)
50  const
51 {
52  long value = 0;
53 
54  for(int i = 255; i !=0; i--)
55  {
56  if (value > percentage*m_numberOfSamples)
57  return i;
58 
59  value += m_data[i];
60  }
61 
62  return 0;
63 }
64 
66 {
67  long similar = 0;
68 
69  for(unsigned int i = 0; i < 256; i++)
70  {
71  if (m_data[i] < other.m_data[i])
72  similar += m_data[i];
73  else
74  similar += other.m_data[i];
75  }
76 
77  //Using c style cast for old gcc compatibility.
78  return static_cast<float>(similar) / static_cast<float>(m_numberOfSamples);
79 }
80 
81 /* vim: set expandtab tabstop=4 shiftwidth=4: */
mythframe.h
Histogram::generateFromImage
void generateFromImage(MythVideoFrame *frame, unsigned int frameWidth, unsigned int frameHeight, unsigned int minScanX, unsigned int maxScanX, unsigned int minScanY, unsigned int maxScanY, unsigned int XSpacing, unsigned int YSpacing)
Definition: Histogram.cpp:10
Histogram::calculateSimilarityWith
float calculateSimilarityWith(const Histogram &other) const
Definition: Histogram.cpp:65
Histogram::m_numberOfSamples
unsigned int m_numberOfSamples
Definition: Histogram.h:27
Histogram::getAverageIntensity
unsigned int getAverageIntensity() const
Definition: Histogram.cpp:34
Histogram::m_data
std::array< int, 256 > m_data
Definition: Histogram.h:24
MythVideoFrame::m_pitches
FramePitches m_pitches
Definition: mythframe.h:142
Histogram::getThresholdForPercentageOfPixels
unsigned int getThresholdForPercentageOfPixels(float percentage) const
Definition: Histogram.cpp:49
Histogram.h
Histogram
Definition: Histogram.h:6
MythVideoFrame
Definition: mythframe.h:88
MythVideoFrame::m_buffer
uint8_t * m_buffer
Definition: mythframe.h:120