Subversion Repositories seema-scanner

Rev

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

Rev 28 Rev 99
1
#include "SMVideoWidget.h"
1
#include "SMVideoWidget.h"
2
 
2
 
3
SMVideoWidget::SMVideoWidget(QWidget *parent) : QLabel(parent){
3
SMVideoWidget::SMVideoWidget(QWidget *parent) : QLabel(parent){
4
 
4
 
5
}
5
}
6
 
6
 
7
static QImage cvMat2qImage(cv::Mat mat){
7
static QImage cvMat2qImage(cv::Mat mat){
8
 
8
 
9
    // 8-bits unsigned, NO. OF CHANNELS=1
9
    // 8-bits unsigned, NO. OF CHANNELS=1
10
    if(mat.type()==CV_8UC1) {
10
    if(mat.type()==CV_8UC1) {
11
        // Set the color table (used to tranMVate colour indexes to qRgb values)
11
        // Set the color table (used to tranMVate colour indexes to qRgb values)
12
        QVector<QRgb> colorTable;
12
        QVector<QRgb> colorTable;
13
        for (int i=0; i<256; i++)
13
        for (int i=0; i<256; i++)
14
            colorTable.push_back(qRgb(i,i,i));
14
            colorTable.push_back(qRgb(i,i,i));
15
        // Copy input Mat
15
        // Copy input Mat
16
        QImage img((const uchar*)mat.data, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);
16
        QImage img((const uchar*)mat.data, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);
17
        img.setColorTable(colorTable);
17
        img.setColorTable(colorTable);
18
        return img;
18
        return img;
19
    } else if(mat.type()==CV_8UC3) {
19
    } else if(mat.type()==CV_8UC3) {
20
        // Copy input Mat
20
        // Copy input Mat
21
        QImage img((const uchar*)mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
21
        QImage img((const uchar*)mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
22
        return img;
22
        return img;
23
    } else if(mat.type()==CV_16UC1) {
23
    } else if(mat.type()==CV_16UC1) {
24
        mat.convertTo(mat, CV_8UC1, 1.0/256.0);
24
        mat.convertTo(mat, CV_8UC1, 1.0/256.0);
25
        return cvMat2qImage(mat);
25
        return cvMat2qImage(mat);
26
    } else if(mat.type()==CV_16UC3) {
26
    } else if(mat.type()==CV_16UC3) {
27
        mat.convertTo(mat, CV_8UC3, 1.0/256.0);
27
        mat.convertTo(mat, CV_8UC3, 1.0/256.0);
28
        return cvMat2qImage(mat);
28
        return cvMat2qImage(mat);
29
    } else if(mat.type()==CV_32FC1) {
29
    } else if(mat.type()==CV_32FC1) {
30
        cv::Mat rgb(mat.size(), CV_32FC3);
30
        cv::Mat rgb(mat.size(), CV_32FC3);
31
        rgb.addref();
31
        rgb.addref();
32
        cv::cvtColor(mat, rgb, cv::COLOR_GRAY2RGB);
32
        cv::cvtColor(mat, rgb, cv::COLOR_GRAY2RGB);
33
        // Copy input Mat
33
        // Copy input Mat
34
        QImage img((const uchar*)rgb.data, rgb.cols, rgb.rows, rgb.step, QImage::Format_RGB32);
34
        QImage img((const uchar*)rgb.data, rgb.cols, rgb.rows, rgb.step, QImage::Format_RGB32);
35
        rgb.release();
35
        rgb.release();
36
        return img;
36
        return img;
37
    } else {
37
    } else {
38
        std::cerr << "SMVideoWidget: cv::Mat could not be converted to QImage!";
38
        std::cerr << "SMVideoWidget: cv::Mat could not be converted to QImage!";
39
        return QImage();
39
        return QImage();
40
    }
40
    }
41
}
41
}
42
 
42
 
-
 
43
void SMVideoWidget::showImageCV(std::vector<char> compressedImage){
-
 
44
 
-
 
45
    cv::Mat image;
-
 
46
    cv::imdecode(compressedImage, image);
-
 
47
    showImageCV(image);
-
 
48
 
-
 
49
}
43
 
50
 
44
void SMVideoWidget::showImageCV(cv::Mat image){
51
void SMVideoWidget::showImageCV(cv::Mat image){
45
 
52
 
46
    QImage qimage = cvMat2qImage(image);
53
    QImage qimage = cvMat2qImage(image);
47
 
54
 
48
    // correct size only if label has no borders/frame!
55
    // correct size only if label has no borders/frame!
49
    int w = this->width();
56
    int w = this->width();
50
    int h = this->height();
57
    int h = this->height();
51
 
58
 
52
    pixmap = QPixmap::fromImage(qimage);
59
    pixmap = QPixmap::fromImage(qimage);
53
    this->setPixmap(pixmap.scaled(w,h,Qt::KeepAspectRatio));
60
    this->setPixmap(pixmap.scaled(w,h,Qt::KeepAspectRatio));
54
 
61
 
55
}
62
}
56
 
63
 
57
void SMVideoWidget::resizeEvent(QResizeEvent *event){
64
void SMVideoWidget::resizeEvent(QResizeEvent *event){
58
 
65
 
59
    if(!pixmap.isNull()){
66
    if(!pixmap.isNull()){
60
        // correct size only if label has no borders/frame!
67
        // correct size only if label has no borders/frame!
61
        int w = event->size().width();
68
        int w = event->size().width();
62
        int h = event->size().height();
69
        int h = event->size().height();
63
        this->setPixmap(pixmap.scaled(w,h,Qt::KeepAspectRatio));
70
        this->setPixmap(pixmap.scaled(w,h,Qt::KeepAspectRatio));
64
    }
71
    }
65
}
72
}
66
 
73