Subversion Repositories seema-scanner

Rev

Rev 100 | Rev 113 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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