-
Notifications
You must be signed in to change notification settings - Fork 67
/
MatToQImage.cpp
108 lines (88 loc) · 3.22 KB
/
MatToQImage.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
Copyright 2012-2014 Benjamin Vedder [email protected]
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "MatToQImage.h"
// Found here: http://asmaloney.com/2013/11/code/converting-between-cvmat-and-qimage-or-qpixmap/
QImage MatToQImage(const Mat& inMat)
{
switch (inMat.type()) {
case CV_8UC4: // 8-bit, 4 channel
{
QImage image(inMat.data, inMat.cols, inMat.rows, inMat.step, QImage::Format_RGB32);
return image;
}
// 8-bit, 3 channel
case CV_8UC3:
{
QImage image(inMat.data, inMat.cols, inMat.rows, inMat.step, QImage::Format_RGB888);
return image.rgbSwapped();
}
// 8-bit, 1 channel
case CV_8UC1:
{
static QVector<QRgb> sColorTable;
// only create our color table once
if (sColorTable.isEmpty()) {
for ( int i = 0; i < 256; ++i ) {
sColorTable.push_back( qRgb( i, i, i ) );
}
}
QImage image(inMat.data, inMat.cols, inMat.rows, inMat.step, QImage::Format_Indexed8);
image.setColorTable(sColorTable);
return image;
}
default:
qWarning() << "ASM::cvMatToQImage() - cv::Mat image type not handled in switch:" << inMat.type();
break;
}
return QImage();
}
QPixmap MatToQPixmap(const Mat &inMat)
{
return QPixmap::fromImage(MatToQImage(inMat));
}
Mat QImageToMat(const QImage &inImage, bool inCloneImageData)
{
switch (inImage.format())
{
// 8-bit, 4 channel
case QImage::Format_RGB32:
{
cv::Mat mat(inImage.height(), inImage.width(), CV_8UC4, const_cast<uchar*>(inImage.bits()), inImage.bytesPerLine());
return (inCloneImageData ? mat.clone() : mat);
}
// 8-bit, 3 channel
case QImage::Format_RGB888:
{
if (!inCloneImageData) {
qWarning() << "ASM::QImageToCvMat() - Conversion requires cloning since we use a temporary QImage";
}
QImage swapped = inImage.rgbSwapped();
return cv::Mat(swapped.height(), swapped.width(), CV_8UC3, const_cast<uchar*>(swapped.bits()), swapped.bytesPerLine()).clone();
}
// 8-bit, 1 channel
case QImage::Format_Indexed8:
{
cv::Mat mat(inImage.height(), inImage.width(), CV_8UC1, const_cast<uchar*>(inImage.bits()), inImage.bytesPerLine());
return (inCloneImageData ? mat.clone() : mat);
}
default:
qWarning() << "ASM::QImageToCvMat() - QImage format not handled in switch:" << inImage.format();
break;
}
return cv::Mat();
}
Mat QPixmapToMat(const QPixmap &inPixmap, bool inCloneImageData)
{
return QImageToMat(inPixmap.toImage(), inCloneImageData);
}