Blame view

HsHistoWidget.cpp 1.62 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
  #include <opencv2/imgproc/imgproc.hpp>
  #include <iostream>
  #include <QPainter>
  #include "HsHistoWidget.h"
  
  using namespace cv;
  using namespace std;
  
  HsHistoWidget::HsHistoWidget(int width, int height, int paletteHeight, QWidget *parent)
    : QWidget(parent),
      m_width(width),
      m_height(height),
      m_paletteHeight(paletteHeight)
  {
    setFixedSize(m_width, m_height + m_paletteHeight + 1);
  }
  
  ///////////
  // slots //
  ///////////
  
  void HsHistoWidget::setHisto(const Mat &hsHisto)
  {
    // normalizing histogram between 0 and 255 for drawing purpose
    normalize(hsHisto, m_hsHisto, 0, 255, NORM_MINMAX, -1, Mat());
    update();
  }
  
  void HsHistoWidget::paintEvent(QPaintEvent *event)
  {
    Q_UNUSED(event);
    float binWidth = static_cast<float>(m_width) / m_hsHisto.rows;
    float binHeight = static_cast<float>(m_height) / m_hsHisto.cols;
  
    QPainter painter(this);
    float hNormFac = 360.0 / m_hsHisto.rows;
  
    painter.fillRect(0, 0, m_width, m_height, QBrush(Qt::white));
  
    for (int i = 1; i <= m_hsHisto.rows; i++)  {
      
      // drawing hue palette
      QRectF palette(QPointF((i-1) * binWidth, m_height + 1),
  		   QPointF(i * binWidth, m_height + 1 + m_paletteHeight));
      painter.fillRect(palette, QColor::fromHsv(qRound((i-1) * hNormFac), 255, 255));
  
      // plotting histogram values
      for (int j = 1; j <= m_hsHisto.cols; j++) {
        QRectF rectangle(QPointF((i-1) * binWidth, m_height - (j-1) * binHeight),
  		       QPointF(i * binWidth, m_height - j * binHeight));
        uchar grayLevel = qRound(m_hsHisto.at<float>(i, j));
        QColor grayColor(grayLevel, grayLevel, grayLevel);
        painter.fillRect(rectangle, QBrush(grayColor));
      }
    }
  }