-
Notifications
You must be signed in to change notification settings - Fork 0
/
QtOpencvCore.cpp
52 lines (43 loc) · 1.38 KB
/
QtOpencvCore.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
#include "QtOpencvCore.hpp"
namespace QtOpencvCore
{
QImage img2qimg(cv::Mat& img)
{
QImage qimage;
// convert the color to RGB (OpenCV uses BGR)
switch (img.type()) {
case CV_8UC1:
cv::cvtColor(img, img, CV_GRAY2RGB);
qimage = QImage((uchar*) img.data, img.cols, img.rows, img.step, QImage::Format_RGB888);
cv::cvtColor(img, img, CV_RGB2GRAY);
break;
case CV_8UC3:
cv::cvtColor(img, img, CV_BGR2RGB);
qimage = QImage((uchar*) img.data, img.cols, img.rows, img.step, QImage::Format_RGB888);
cv::cvtColor(img, img, CV_RGB2BGR);
break;
}
// return the QImage
return qimage;
}
QPixmap img2qpix(cv::Mat& img)
{
return QPixmap::fromImage(img2qimg(img));
}
std::string qstr2str(QString const& qstr)
{
// return the converted QString (now std string)
return qstr.toStdString();
}
QString str2qstr(std::string const& str)
{
// return the converted std string (now QString)
return QString::fromStdString(str);
}
cv::Mat qimg2img(const QImage &qimg)
{
cv::Mat img;
img = cv::Mat(qimg.height(), qimg.width(), CV_8UC4, const_cast<uchar*>(qimg.bits()), qimg.bytesPerLine());
return img;
}
} // namespace QtOpencvCore