MythTV  master
Histogram.cpp
Go to the documentation of this file.
1 #include "Histogram.h"
2 #include <string>
3 #include <cmath>
4 #include <cstring>
5 
6 #include "mythframe.h"
7 
8 void Histogram::generateFromImage(MythVideoFrame* frame, unsigned int frameWidth,
9  unsigned int frameHeight, unsigned int minScanX, unsigned int maxScanX,
10  unsigned int minScanY, unsigned int maxScanY, unsigned int XSpacing,
11  unsigned int YSpacing)
12 {
13  m_data.fill(0);
15 
16  if (maxScanX > frameWidth-1)
17  maxScanX = frameWidth-1;
18 
19  if (maxScanY > frameHeight-1)
20  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(int 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:8
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
Y, U, & V pitches.
Definition: mythframe.h:123
Histogram::getThresholdForPercentageOfPixels
unsigned int getThresholdForPercentageOfPixels(float percentage) const
Definition: Histogram.cpp:49
Histogram.h
Histogram
Definition: Histogram.h:7
MythVideoFrame
Definition: mythframe.h:83
MythVideoFrame::m_buffer
uint8_t * m_buffer
Definition: mythframe.h:101