Blame view

VideoFrameProcessor.cpp 2.98 KB
3f2992b2c   bostx   V1.0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  #include <opencv2/imgproc/imgproc.hpp>
  
  #include "VideoFrameProcessor.h"
  
  using namespace cv;
  
  VideoFrameProcessor::VideoFrameProcessor(int metrics, QObject *parent)
    : QObject(parent),
      m_metrics(metrics),
      m_normFactor(1.0)
  {
  }
  
  ///////////
  // slots //
  ///////////
  
  cv::Mat VideoFrameProcessor::genVHisto(const cv::Mat &frame, int vBins)
  {
    // histogram
    Mat vHisto;
  
    // use the 3-rd channel
    int channels[] = { 2 };
  
    // value varies from 0 to 255
    float range[] = { 0, 256 };
    const float* histRange = { range };
  
    // computing and normalizing histogram
    calcHist(&frame, 1, channels, Mat(), vHisto, 1, &vBins, &histRange, true, false);
    normalize(vHisto, vHisto, 1, 0, NORM_L1);
  
    return vHisto;
  }
   
  cv::Mat VideoFrameProcessor::genHsHisto(const cv::Mat &frame, int hBins, int sBins)
  {
    // histogram
    Mat hsHisto;
  
    // use the 1-st and 2-nd channels
    int channels[] = { 0, 1 };
    
    // histogram sizes as defined by user
    int histSize[] = { hBins, sBins };
  
    // hue varies from 0 to 255, saturation from 0 to 180
    float hRange[] = { 0, 256 };
    float sRange[] = { 0, 180 };
    const float* ranges[] = { hRange, sRange };
  
    // computing and normalizing histogram
    calcHist(&frame, 1, channels, Mat(), hsHisto, 2, histSize, ranges, true, false);
    normalize(hsHisto, hsHisto, 1, 0, NORM_L1);
  
    return hsHisto;
  }
   
  cv::Mat VideoFrameProcessor::genHsvHisto(const cv::Mat &frame, int hBins, int sBins, int vBins)
  {
    // histogram
    Mat hsvHisto;
  
    // use the three channels
    int channels[] = { 0, 1, 2 };
    
    // histogram sizes as defined by user
    int histSize[] = { hBins, sBins, vBins };
  
    // hue varies from 0 to 255, saturation from 0 to 180 and value from 0 to 255
    float hRange[] = { 0, 256 };
    float sRange[] = { 0, 180 };
    float vRange[] = { 0, 255 };
    const float* ranges[] = { hRange, sRange, vRange };
  
    // computing and normalizing histogram
    calcHist(&frame, 1, channels, Mat(), hsvHisto, 3, histSize, ranges, true, false);
    normalize(hsvHisto, hsvHisto, 1, 0, NORM_L1);
  
    return hsvHisto;
  }
  
  double VideoFrameProcessor::distanceFromPrev(const Mat &hist, const Mat &prevHist)
  {
    double dist(1.0);
    
    if (hist.size() == prevHist.size()) {
      if (m_metrics == NORM_L1 || m_metrics == NORM_L2)
        dist = norm(hist, prevHist, m_metrics);
      else
        dist = compareHist(hist, prevHist, m_metrics);
    }
    else
      dist = -1.0;
    
    if (m_metrics == CV_COMP_CORREL)
      dist = 1.0 - dist;
  
    return dist;
  }
  
  void VideoFrameProcessor::activL1()
  {
    m_metrics = NORM_L1;
    m_normFactor = 0.5;
  }
  
  void VideoFrameProcessor::activL2()
  {
    m_metrics = NORM_L2;
    m_normFactor = 1 / sqrt(2);
  }
  
  void VideoFrameProcessor::activCorrel()
  {
    m_metrics = CV_COMP_CORREL;
    m_normFactor = 1.0;
  }
   
  void VideoFrameProcessor::activChiSqr()
  {
    m_metrics = CV_COMP_CHISQR;
    m_normFactor = 1.0;
  }
   
  void VideoFrameProcessor::activIntersect()
  {
    m_metrics = CV_COMP_INTERSECT;
    m_normFactor = 1.0;
  }
   
  void VideoFrameProcessor::activHellinger()
  {
    m_metrics = CV_COMP_HELLINGER;
    m_normFactor = 1.0;
  }