-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathUtilityFunctions.cpp
76 lines (68 loc) · 2.45 KB
/
UtilityFunctions.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
//-----------------------------------------------------------------------------
#include "UtilityFunctions.h"
#include <QDebug>
//-----------------------------------------------------------------------------
QImage CvMatToQImage(const cv::Mat &inmat)
{
switch (inmat.type())
{
case CV_8UC4: // 8-bit, 4 channel
{
QImage image(inmat.data, inmat.cols, inmat.rows, static_cast<int>(inmat.step), QImage::Format_RGB32);
return image;
}
case CV_8UC3: // 8-bit, 3 channel
{
QImage image(inmat.data, inmat.cols, inmat.rows, static_cast<int>(inmat.step), QImage::Format_RGB888);
return image.rgbSwapped();
}
case CV_8UC1: // 8-bit, 1 channel
{
static QVector<QRgb> s_color_table;
// only create our color table once
if (s_color_table.isEmpty())
{
for (int i = 0; i < 256; ++i)
{
s_color_table.push_back(qRgb(i, i, i));
}
}
QImage image(inmat.data, inmat.cols, inmat.rows, static_cast<int>(inmat.step), QImage::Format_Indexed8);
image.setColorTable(s_color_table);
return image;
}
default:
qWarning() << "ASM::cvMatToQImage() - cv::Mat image type not handled in switch:" << inmat.type();
break;
}
return QImage();
}
//-----------------------------------------------------------------------------
cv::Mat QImageToCvMat(const QImage &in_image, bool in_clone_image_data)
{
switch (in_image.format())
{
case QImage::Format_RGB32: // 8-bit, 4 channel
{
cv::Mat mat(in_image.height(), in_image.width(), CV_8UC4, const_cast<uchar*>(in_image.bits()), in_image.bytesPerLine());
return (in_clone_image_data ? mat.clone() : mat);
}
case QImage::Format_RGB888: // 8-bit, 3 channel
{
if (!in_clone_image_data)
qWarning() << "ASM::QImageToCvMat() - Conversion requires cloning since we use a temporary QImage";
QImage swapped = in_image.rgbSwapped();
return cv::Mat(swapped.height(), swapped.width(), CV_8UC3, const_cast<uchar*>(swapped.bits()), swapped.bytesPerLine()).clone();
}
case QImage::Format_Indexed8: // 8-bit, 1 channel
{
cv::Mat mat(in_image.height(), in_image.width(), CV_8UC1, const_cast<uchar*>(in_image.bits()), in_image.bytesPerLine());
return (in_clone_image_data ? mat.clone() : mat);
}
default:
qWarning() << "ASM::QImageToCvMat() - QImage format not handled in switch:" << in_image.format();
break;
}
return cv::Mat();
}
//-----------------------------------------------------------------------------