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(VideoFrame* 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  memset(m_data,0,sizeof(m_data));
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->buf;
23  int bytesPerLine = frame->pitches[0];
24  for(unsigned int y = minScanY; y < maxScanY; y += YSpacing)
25  for(unsigned int x = minScanX; x < maxScanX; x += XSpacing)
26  {
27  m_data[framePtr[y * bytesPerLine + x]]++;
29  }
30 }
31 
32 unsigned int Histogram::getAverageIntensity(void) const
33 {
34  if (!m_numberOfSamples)
35  return 0;
36 
37  long value = 0;
38 
39  for(int i = 0; i < 256; i++)
40  {
41  value += m_data[i]*i;
42  }
43 
44  return value / m_numberOfSamples;
45 }
46 
47 unsigned int Histogram::getThresholdForPercentageOfPixels(float percentage)
48  const
49 {
50  long value = 0;
51 
52  for(int i = 255; i !=0; i--)
53  {
54  if (value > percentage*m_numberOfSamples)
55  return i;
56 
57  value += m_data[i];
58  }
59 
60  return 0;
61 }
62 
64 {
65  long similar = 0;
66 
67  for(unsigned int i = 0; i < 256; i++)
68  {
69  if (m_data[i] < other.m_data[i])
70  similar += m_data[i];
71  else
72  similar += other.m_data[i];
73  }
74 
75  //Using c style cast for old gcc compatibility.
76  return static_cast<float>(similar) / static_cast<float>(m_numberOfSamples);
77 }
78 
79 /* vim: set expandtab tabstop=4 shiftwidth=4: */
int pitches[3]
Y, U, & V pitches.
Definition: mythframe.h:159
int m_data[256]
Definition: Histogram.h:24
unsigned char * buf
Definition: mythframe.h:139
float calculateSimilarityWith(const Histogram &) const
Definition: Histogram.cpp:63
unsigned int getAverageIntensity() const
Definition: Histogram.cpp:32
unsigned int m_numberOfSamples
Definition: Histogram.h:27
void generateFromImage(VideoFrame *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
unsigned int getThresholdForPercentageOfPixels(float percentage) const
Definition: Histogram.cpp:47