Subversion Repositories seema-scanner

Rev

Rev 228 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
201 jakw 1
#include "SMVideoZoomWidget.h"
2
 
3
#include "cvtools.h"
4
 
5
#include <QScrollBar>
6
#include <QMenu>
7
 
8
SMVideoZoomWidget::SMVideoZoomWidget(QWidget *parent) : QGraphicsView(parent)
9
{
10
    scene = new QGraphicsScene(this);
11
    item = new QGraphicsPixmapItem;
12
 
13
    scene->addItem(item);
14
 
15
    setScene(scene);
16
 
17
    // Causes weird scroll bars
18
    //this->setStyleSheet("background: transparent");
19
 
20
    setContextMenuPolicy(Qt::CustomContextMenu);
21
    connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showContextMenu(const QPoint&)));
22
 
23
}
24
 
25
 
26
static QImage cvMat2qImage(cv::Mat mat){
27
 
28
    // 8-bits unsigned, raw bayer image
29
    if(mat.type()==CV_8UC1) {
30
        cv::Mat rgb;
242 jakw 31
        cv::cvtColor(mat, rgb, cv::COLOR_BayerBG2RGB);
201 jakw 32
        QImage img((const uchar*)rgb.data, rgb.cols, rgb.rows, rgb.step, QImage::Format_RGB888);
33
        img = img.copy();
34
        return img;
35
    // 8-bit unsigned rgb image
36
    } else if(mat.type()==CV_8UC3) {
37
        // Copy input Mat
38
        QImage img((const uchar*)mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
39
        return img;
40
    // 16-bit unsigned, raw bayer image
41
    } else if(mat.type()==CV_16UC1) {
42
        cv::Mat mat8bit = mat.clone();
43
        cvtools::rshift(mat8bit, 8);
44
        mat8bit.convertTo(mat8bit, CV_8UC1);
45
        cv::Mat rgb;
242 jakw 46
        cv::cvtColor(mat8bit, rgb, cv::COLOR_BayerBG2RGB);
201 jakw 47
        QImage img((const uchar*)rgb.data, rgb.cols, rgb.rows, rgb.step, QImage::Format_RGB888);
48
        img = img.copy();
49
        return img;
50
    // 16-bit unsigned rgb image
51
    } else if(mat.type()==CV_16UC3) {
52
        mat.convertTo(mat, CV_8UC3, 1.0/256.0);
53
        return cvMat2qImage(mat);
242 jakw 54
    // 32bit bayer image
201 jakw 55
    } else if(mat.type()==CV_32FC1) {
56
        cv::Mat rgb(mat.size(), CV_32FC3);
242 jakw 57
        cv::cvtColor(mat, rgb, cv::COLOR_BayerBG2RGB);
201 jakw 58
        QImage img((const uchar*)rgb.data, rgb.cols, rgb.rows, rgb.step, QImage::Format_RGB32);
59
        img = img.copy();
60
        return img;
242 jakw 61
    } else if(mat.type()==CV_32FC3) {
62
        QImage img((const uchar*)mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB32);
63
        img = img.copy();
64
        return img;
201 jakw 65
    } else {
66
        std::cerr << "SMVideoWidget: cv::Mat could not be converted to QImage!";
67
        return QImage();
68
    }
69
 
70
    // 8-bit unsigned gray-scale image
71
//    } else if(mat.type()==CV_8UC1) {
72
//        // Set the color table (used to tranMVate colour indexes to qRgb values)
73
//        QVector<QRgb> colorTable;
74
//        for (int i=0; i<256; i++)
75
//            colorTable.push_back(qRgb(i,i,i));
76
//        // Copy input Mat
77
//        QImage img((const uchar*)mat.data, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);
78
//        img.setColorTable(colorTable);
79
//        return img;
80
}
81
 
82
 
83
void SMVideoZoomWidget::showImageCV(cv::Mat image){
84
 
228 jakw 85
    if(image.empty())
86
        return;
87
 
201 jakw 88
    QImage qimage = cvMat2qImage(image);
89
 
90
    // If first ever camera image, fit in view
91
    if(item->pixmap().isNull()){
92
        item->setPixmap(QPixmap::fromImage(qimage));
93
        this->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
94
    } else {
95
        item->setPixmap(QPixmap::fromImage(qimage));
96
    }
97
 
98
}
99
 
100
 
101
void SMVideoZoomWidget::mousePressEvent(QMouseEvent *event)
102
{
103
//    // Drag mode : change the cursor's shape
104
//    if (event->button() == Qt::LeftButton)
105
//        this->setDragMode(QGraphicsView::ScrollHandDrag);
106
 
107
    QGraphicsView::mousePressEvent(event);
108
}
109
 
110
// Called when a mouse button is pressed
111
void SMVideoZoomWidget::mouseReleaseEvent(QMouseEvent *event)
112
{
113
//    // Exit drag mode : change the cursor's shape
114
//    if (event->button() == Qt::LeftButton) this->setDragMode(QGraphicsView::NoDrag);
115
    QGraphicsView::mouseReleaseEvent(event);
116
}
117
 
118
 
119
void SMVideoZoomWidget::wheelEvent(QWheelEvent *event)
120
{
121
    // When zooming, the view stay centered over the mouse
122
    this->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
123
 
124
    double factor = 1.3;
125
    if(event->delta() > 0)
126
        // Zoom in
127
        scale(factor, factor);
128
    else
129
        // Zooming out
130
        scale(1.0 / factor, 1.0 / factor);
131
 
132
    // The event is processed
133
    event->accept();
134
 
135
    if(this->horizontalScrollBar()->isVisible() || this->verticalScrollBar()->isVisible())
136
        this->setDragMode(QGraphicsView::ScrollHandDrag);
137
    else
138
        this->setDragMode(QGraphicsView::NoDrag);
139
}
140
 
141
// Overloaded functionthat catch the resize event
142
void SMVideoZoomWidget::resizeEvent(QResizeEvent *event)
143
{
144
//    // First call, the scene is created
145
//    if(event->oldSize().width() == -1 || event->oldSize().height() == -1) return;
146
 
147
//    // Get the previous rectangle of the scene in the viewport
148
//    QPointF P1=mapToScene(QPoint(0,0));
149
//    QPointF P2=mapToScene(QPoint(event->oldSize().width(),event->oldSize().height()));
150
 
151
//    // Stretch the rectangle around the scene
152
//    if (P1.x()<0) P1.setX(0);
153
//    if (P1.y()<0) P1.setY(0);
154
//    if (P2.x()>scene->width()) P2.setX(scene->width());
155
//    if (P2.y()>scene->height()) P2.setY(scene->height());
156
 
157
//    // Fit the previous area in the scene
158
//    this->fitInView(QRect(P1.toPoint(),P2.toPoint()),Qt::KeepAspectRatio);
159
 
160
    if(this->horizontalScrollBar()->isVisible() || this->verticalScrollBar()->isVisible())
161
        this->setDragMode(QGraphicsView::ScrollHandDrag);
162
    else
163
        this->setDragMode(QGraphicsView::NoDrag);
164
 
165
    QGraphicsView::resizeEvent(event);
166
}
167
 
168
// Display contextual menu
169
void SMVideoZoomWidget::showContextMenu(const QPoint &pos)
170
{
171
    // Get the mouse position in the scene
172
    QPoint globalPos = mapToGlobal(pos);
173
    // Create the menu and add action
174
    QMenu contextMenu;
175
    contextMenu.addAction("Reset view", this, SLOT(fitImage()));
176
    // Display the menu
177
    contextMenu.exec(globalPos);
178
}
179
 
180
void SMVideoZoomWidget::fitImage(){
181
 
182
    // Turn scroll bars off temporarily to get correct widget size
183
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
184
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
185
 
186
    this->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
187
 
188
    // Restore scroll bar policy
189
    setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
190
    setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
191
}
192
 
193
SMVideoZoomWidget::~SMVideoZoomWidget(){
220 flgw 194
    if(item) delete item;
195
    if(scene) delete scene;
201 jakw 196
}