-
Notifications
You must be signed in to change notification settings - Fork 0
/
Qt-OpenCV-AdditionalLibrary.cpp
42 lines (41 loc) · 1.48 KB
/
Qt-OpenCV-AdditionalLibrary.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "Qt-OpenCV-AdditionalLibrary.h"
QPixmap MatToQPixmap(const Mat& mat)
{
// 8-bits unsigned, NO. OF CHANNELS=1
if(mat.type()==CV_8UC1)
{
// Set the color table (used to translate colour indexes to qRgb values)
QVector<QRgb> colorTable;
for (int i=0; i<256; i++)
colorTable.push_back(qRgb(i,i,i));
// Copy input Mat
const uchar *qImageBuffer = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);
img.setColorTable(colorTable);
return QPixmap::fromImage(img);
//return img;
}
// 8-bits unsigned, NO. OF CHANNELS = 3
else if(mat.type()==CV_8UC3)
{
// Copy input Mat
const uchar *qImageBuffer = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
return QPixmap::fromImage(img.rgbSwapped());
}
else if(mat.type()==CV_8UC4)
{
// Copy input Mat
const uchar *qImageBuffer = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
return QPixmap::fromImage(img.rgbSwapped());
}
else
{
qDebug() << "ERROR: Mat could not be converted to QImage.";
return QPixmap();
}
}