Subversion Repositories seema-scanner

Rev

Rev 220 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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