-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyConvert.cpp
189 lines (168 loc) · 5.28 KB
/
MyConvert.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "qt_3dmeasure.h"
#include "MyConvert.h"
cv::Mat MyConverter::Matrix3ftoCvMat(const Eigen::Matrix3f &m)
{
cv::Mat cvMat(3, 3, CV_32F);
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cvMat.at<float>(i, j) = m(i, j);
return cvMat.clone();
}
Eigen::Matrix<float, 3, 3> MyConverter::CvMattoMatrix3f(const cv::Mat &cvMat3)
{
Eigen::Matrix<float, 3, 3> M;
M << cvMat3.at<float>(0, 0), cvMat3.at<float>(0, 1), cvMat3.at<float>(0, 2),
cvMat3.at<float>(1, 0), cvMat3.at<float>(1, 1), cvMat3.at<float>(1, 2),
cvMat3.at<float>(2, 0), cvMat3.at<float>(2, 1), cvMat3.at<float>(2, 2);
return M;
}
cv::Mat MyConverter::Matrix43dtoCvMat(const Eigen::Matrix<double, 4, 3> &m)
{
cv::Mat cvMat(4, 3, CV_64F);
for (int i = 0; i < 4; i++)
for (int j = 0; j < 3; j++)
cvMat.at<double>(i, j) = m(i, j);
return cvMat.clone();
}
Eigen::Matrix<double, 3, 1> MyConverter::CvMat31toMatrix3d(const cv::Mat &cvMat3)
{
Eigen::Matrix<double, 3, 1> M;
M << cvMat3.at<double>(0, 0), cvMat3.at<double>(1, 0), cvMat3.at<double>(2, 0);
return M;
}
cv::Mat MyConverter::Matrix3dtoCvMat(const Eigen::Matrix3d &m)
{
cv::Mat cvMat(3, 3, CV_64F);
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cvMat.at<double>(i, j) = m(i, j);
return cvMat.clone();
}
Eigen::Matrix<double, 4, 3> MyConverter::CvMattoMatrix43d(const cv::Mat &cvMat43)
{
Eigen::Matrix<double, 4, 3> M;
M << cvMat43.at<double>(0, 0), cvMat43.at<double>(0, 1), cvMat43.at<double>(0, 2),
cvMat43.at<double>(1, 0), cvMat43.at<double>(1, 1), cvMat43.at<double>(1, 2),
cvMat43.at<double>(2, 0), cvMat43.at<double>(2, 1), cvMat43.at<double>(2, 2),
cvMat43.at<double>(3, 0), cvMat43.at<double>(3, 1), cvMat43.at<double>(3, 2);
return M;
}
Eigen::Matrix<double, 3, 3> MyConverter::CvMattoMatrix3d(const cv::Mat &cvMat3)
{
Eigen::Matrix<double, 3, 3> M;
M << cvMat3.at<double>(0, 0), cvMat3.at<double>(0, 1), cvMat3.at<double>(0, 2),
cvMat3.at<double>(1, 0), cvMat3.at<double>(1, 1), cvMat3.at<double>(1, 2),
cvMat3.at<double>(2, 0), cvMat3.at<double>(2, 1), cvMat3.at<double>(2, 2);
return M;
}
//************************************
// Method: cvMat2QImage
// FullName: QT_ForTest::cvMat2QImage
// Access: private
// Returns: QT_NAMESPACE::QImage
// Qualifier:
// Parameter: cv::Mat & mat
//************************************
QImage MyConverter::cvMat2QImage(cv::Mat& mat)
{
// 8-bits unsigned, NO. OF CHANNELS = 1
if (mat.type() == CV_8UC1)
{
QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
// Set the color table (used to translate colour indexes to qRgb values)
image.setColorCount(256);
for (int i = 0; i < 256; i++)
{
image.setColor(i, qRgb(i, i, i));
}
// Copy input Mat
uchar *pSrc = mat.data;
for (int row = 0; row < mat.rows; row++)
{
uchar *pDest = image.scanLine(row);
memcpy(pDest, pSrc, mat.cols);
pSrc += mat.step;
}
return image;
}
else if (mat.type() == CV_16UC1)
{
cv::Mat mat8u(mat.size(), CV_8UC1);
for (int i = 0; i < mat.rows; i++)
{
for (int j = 0; j < mat.cols; j++)
{
int n = 255 * mat.at<ushort>(i, j) / 65535; //tt1
mat8u.at<uchar>(i, j) = n;
}
}
QImage image(mat8u.cols, mat8u.rows, QImage::Format_Indexed8);
// Set the color table (used to translate colour indexes to qRgb values)
image.setColorCount(256);
for (int i = 0; i < 256; i++)
{
image.setColor(i, qRgb(i, i, i));
}
// Copy input Mat
uchar *pSrc = mat8u.data;
for (int row = 0; row < mat8u.rows; row++)
{
uchar *pDest = image.scanLine(row);
memcpy(pDest, pSrc, mat8u.cols);
pSrc += mat8u.step;
}
return image;
}
// 8-bits unsigned, NO. OF CHANNELS = 3
else if (mat.type() == CV_8UC3)
{
// Copy input Mat
const uchar *pSrc = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
return image.rgbSwapped();
}
else if (mat.type() == CV_8UC4)
{
qDebug() << "CV_8UC4";
// Copy input Mat
const uchar *pSrc = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32);
return image.copy();
}
else
{
qDebug() << "ERROR: Mat could not be converted to QImage.";
return QImage();
}
}
//************************************
// Method: QImage2cvMat
// FullName: QT_ForTest::QImage2cvMat
// Access: private
// Returns: cv
// Qualifier:
// Parameter: QImage image
//************************************
cv::Mat MyConverter::QImage2cvMat(QImage image)
{
cv::Mat mat;
qDebug() << image.format();
switch (image.format())
{
case QImage::Format_ARGB32:
case QImage::Format_RGB32:
case QImage::Format_ARGB32_Premultiplied:
mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
break;
case QImage::Format_RGB888:
mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
cv::cvtColor(mat, mat, CV_BGR2RGB);
break;
case QImage::Format_Indexed8:
mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
break;
}
return mat;
}