Subversion Repositories seema-scanner

Rev

Rev 228 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 228 Rev 242
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::COLOR_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::COLOR_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 bayer 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_BayerBG2RGB);
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;
-
 
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();
60
        return img;
64
        return img;
61
    } else {
65
    } else {
62
        std::cerr << "SMVideoWidget: cv::Mat could not be converted to QImage!";
66
        std::cerr << "SMVideoWidget: cv::Mat could not be converted to QImage!";
63
        return QImage();
67
        return QImage();
64
    }
68
    }
65
 
69
 
66
    // 8-bit unsigned gray-scale image
70
    // 8-bit unsigned gray-scale image
67
//    } else if(mat.type()==CV_8UC1) {
71
//    } else if(mat.type()==CV_8UC1) {
68
//        // Set the color table (used to tranMVate colour indexes to qRgb values)
72
//        // Set the color table (used to tranMVate colour indexes to qRgb values)
69
//        QVector<QRgb> colorTable;
73
//        QVector<QRgb> colorTable;
70
//        for (int i=0; i<256; i++)
74
//        for (int i=0; i<256; i++)
71
//            colorTable.push_back(qRgb(i,i,i));
75
//            colorTable.push_back(qRgb(i,i,i));
72
//        // Copy input Mat
76
//        // Copy input Mat
73
//        QImage img((const uchar*)mat.data, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);
77
//        QImage img((const uchar*)mat.data, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);
74
//        img.setColorTable(colorTable);
78
//        img.setColorTable(colorTable);
75
//        return img;
79
//        return img;
76
}
80
}
77
 
81
 
78
 
82
 
79
void SMVideoZoomWidget::showImageCV(cv::Mat image){
83
void SMVideoZoomWidget::showImageCV(cv::Mat image){
80
 
84
 
81
    if(image.empty())
85
    if(image.empty())
82
        return;
86
        return;
83
 
87
 
84
    QImage qimage = cvMat2qImage(image);
88
    QImage qimage = cvMat2qImage(image);
85
 
89
 
86
    // If first ever camera image, fit in view
90
    // If first ever camera image, fit in view
87
    if(item->pixmap().isNull()){
91
    if(item->pixmap().isNull()){
88
        item->setPixmap(QPixmap::fromImage(qimage));
92
        item->setPixmap(QPixmap::fromImage(qimage));
89
        this->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
93
        this->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
90
    } else {
94
    } else {
91
        item->setPixmap(QPixmap::fromImage(qimage));
95
        item->setPixmap(QPixmap::fromImage(qimage));
92
    }
96
    }
93
 
97
 
94
}
98
}
95
 
99
 
96
 
100
 
97
void SMVideoZoomWidget::mousePressEvent(QMouseEvent *event)
101
void SMVideoZoomWidget::mousePressEvent(QMouseEvent *event)
98
{
102
{
99
//    // Drag mode : change the cursor's shape
103
//    // Drag mode : change the cursor's shape
100
//    if (event->button() == Qt::LeftButton)
104
//    if (event->button() == Qt::LeftButton)
101
//        this->setDragMode(QGraphicsView::ScrollHandDrag);
105
//        this->setDragMode(QGraphicsView::ScrollHandDrag);
102
 
106
 
103
    QGraphicsView::mousePressEvent(event);
107
    QGraphicsView::mousePressEvent(event);
104
}
108
}
105
 
109
 
106
// Called when a mouse button is pressed
110
// Called when a mouse button is pressed
107
void SMVideoZoomWidget::mouseReleaseEvent(QMouseEvent *event)
111
void SMVideoZoomWidget::mouseReleaseEvent(QMouseEvent *event)
108
{
112
{
109
//    // Exit drag mode : change the cursor's shape
113
//    // Exit drag mode : change the cursor's shape
110
//    if (event->button() == Qt::LeftButton) this->setDragMode(QGraphicsView::NoDrag);
114
//    if (event->button() == Qt::LeftButton) this->setDragMode(QGraphicsView::NoDrag);
111
    QGraphicsView::mouseReleaseEvent(event);
115
    QGraphicsView::mouseReleaseEvent(event);
112
}
116
}
113
 
117
 
114
 
118
 
115
void SMVideoZoomWidget::wheelEvent(QWheelEvent *event)
119
void SMVideoZoomWidget::wheelEvent(QWheelEvent *event)
116
{
120
{
117
    // When zooming, the view stay centered over the mouse
121
    // When zooming, the view stay centered over the mouse
118
    this->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
122
    this->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
119
 
123
 
120
    double factor = 1.3;
124
    double factor = 1.3;
121
    if(event->delta() > 0)
125
    if(event->delta() > 0)
122
        // Zoom in
126
        // Zoom in
123
        scale(factor, factor);
127
        scale(factor, factor);
124
    else
128
    else
125
        // Zooming out
129
        // Zooming out
126
        scale(1.0 / factor, 1.0 / factor);
130
        scale(1.0 / factor, 1.0 / factor);
127
 
131
 
128
    // The event is processed
132
    // The event is processed
129
    event->accept();
133
    event->accept();
130
 
134
 
131
    if(this->horizontalScrollBar()->isVisible() || this->verticalScrollBar()->isVisible())
135
    if(this->horizontalScrollBar()->isVisible() || this->verticalScrollBar()->isVisible())
132
        this->setDragMode(QGraphicsView::ScrollHandDrag);
136
        this->setDragMode(QGraphicsView::ScrollHandDrag);
133
    else
137
    else
134
        this->setDragMode(QGraphicsView::NoDrag);
138
        this->setDragMode(QGraphicsView::NoDrag);
135
}
139
}
136
 
140
 
137
// Overloaded functionthat catch the resize event
141
// Overloaded functionthat catch the resize event
138
void SMVideoZoomWidget::resizeEvent(QResizeEvent *event)
142
void SMVideoZoomWidget::resizeEvent(QResizeEvent *event)
139
{
143
{
140
//    // First call, the scene is created
144
//    // First call, the scene is created
141
//    if(event->oldSize().width() == -1 || event->oldSize().height() == -1) return;
145
//    if(event->oldSize().width() == -1 || event->oldSize().height() == -1) return;
142
 
146
 
143
//    // Get the previous rectangle of the scene in the viewport
147
//    // Get the previous rectangle of the scene in the viewport
144
//    QPointF P1=mapToScene(QPoint(0,0));
148
//    QPointF P1=mapToScene(QPoint(0,0));
145
//    QPointF P2=mapToScene(QPoint(event->oldSize().width(),event->oldSize().height()));
149
//    QPointF P2=mapToScene(QPoint(event->oldSize().width(),event->oldSize().height()));
146
 
150
 
147
//    // Stretch the rectangle around the scene
151
//    // Stretch the rectangle around the scene
148
//    if (P1.x()<0) P1.setX(0);
152
//    if (P1.x()<0) P1.setX(0);
149
//    if (P1.y()<0) P1.setY(0);
153
//    if (P1.y()<0) P1.setY(0);
150
//    if (P2.x()>scene->width()) P2.setX(scene->width());
154
//    if (P2.x()>scene->width()) P2.setX(scene->width());
151
//    if (P2.y()>scene->height()) P2.setY(scene->height());
155
//    if (P2.y()>scene->height()) P2.setY(scene->height());
152
 
156
 
153
//    // Fit the previous area in the scene
157
//    // Fit the previous area in the scene
154
//    this->fitInView(QRect(P1.toPoint(),P2.toPoint()),Qt::KeepAspectRatio);
158
//    this->fitInView(QRect(P1.toPoint(),P2.toPoint()),Qt::KeepAspectRatio);
155
 
159
 
156
    if(this->horizontalScrollBar()->isVisible() || this->verticalScrollBar()->isVisible())
160
    if(this->horizontalScrollBar()->isVisible() || this->verticalScrollBar()->isVisible())
157
        this->setDragMode(QGraphicsView::ScrollHandDrag);
161
        this->setDragMode(QGraphicsView::ScrollHandDrag);
158
    else
162
    else
159
        this->setDragMode(QGraphicsView::NoDrag);
163
        this->setDragMode(QGraphicsView::NoDrag);
160
 
164
 
161
    QGraphicsView::resizeEvent(event);
165
    QGraphicsView::resizeEvent(event);
162
}
166
}
163
 
167
 
164
// Display contextual menu
168
// Display contextual menu
165
void SMVideoZoomWidget::showContextMenu(const QPoint &pos)
169
void SMVideoZoomWidget::showContextMenu(const QPoint &pos)
166
{
170
{
167
    // Get the mouse position in the scene
171
    // Get the mouse position in the scene
168
    QPoint globalPos = mapToGlobal(pos);
172
    QPoint globalPos = mapToGlobal(pos);
169
    // Create the menu and add action
173
    // Create the menu and add action
170
    QMenu contextMenu;
174
    QMenu contextMenu;
171
    contextMenu.addAction("Reset view", this, SLOT(fitImage()));
175
    contextMenu.addAction("Reset view", this, SLOT(fitImage()));
172
    // Display the menu
176
    // Display the menu
173
    contextMenu.exec(globalPos);
177
    contextMenu.exec(globalPos);
174
}
178
}
175
 
179
 
176
void SMVideoZoomWidget::fitImage(){
180
void SMVideoZoomWidget::fitImage(){
177
 
181
 
178
    // Turn scroll bars off temporarily to get correct widget size
182
    // Turn scroll bars off temporarily to get correct widget size
179
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
183
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
180
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
184
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
181
 
185
 
182
    this->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
186
    this->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
183
 
187
 
184
    // Restore scroll bar policy
188
    // Restore scroll bar policy
185
    setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
189
    setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
186
    setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
190
    setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
187
}
191
}
188
 
192
 
189
SMVideoZoomWidget::~SMVideoZoomWidget(){
193
SMVideoZoomWidget::~SMVideoZoomWidget(){
190
    if(item) delete item;
194
    if(item) delete item;
191
    if(scene) delete scene;
195
    if(scene) delete scene;
192
}
196
}
193
 
197