forked from SolarFramework/SolARModuleOpenCV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolAR3DOverlayBoxOpencv.cpp
More file actions
166 lines (131 loc) · 6.01 KB
/
SolAR3DOverlayBoxOpencv.cpp
File metadata and controls
166 lines (131 loc) · 6.01 KB
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
/**
* @copyright Copyright (c) 2017 B-com http://www.b-com.com/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "SolAR3DOverlayBoxOpencv.h"
#include "SolAROpenCVHelper.h"
#include "core/Log.h"
#include "opencv2/core.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/calib3d/calib3d.hpp"
namespace xpcf = org::bcom::xpcf;
XPCF_DEFINE_FACTORY_CREATE_INSTANCE(SolAR::MODULES::OPENCV::SolAR3DOverlayBoxOpencv)
namespace SolAR {
using namespace datastructure;
namespace MODULES {
namespace OPENCV {
SolAR3DOverlayBoxOpencv::SolAR3DOverlayBoxOpencv():ConfigurableBase(xpcf::toUUID<SolAR3DOverlayBoxOpencv>())
{
declareInterface<api::display::I3DOverlay>(this);
m_camMatrix.create(3, 3, CV_32FC1);
m_camDistorsion.create(5, 1, CV_32FC1);
m_parallelepiped.create(8, 3, CV_32FC1);
declarePropertySequence("orientation", m_orientation);
declarePropertySequence("position", m_position);
declarePropertySequence("size", m_size);
LOG_DEBUG(" SolAR3DOverlayBoxOpencv constructor");
}
xpcf::XPCFErrorCode SolAR3DOverlayBoxOpencv::onConfigured()
{
LOG_DEBUG(" SolAR3DOverlayBoxOpencv onConfigured");
float half_X = m_size[0] * 0.5f;
float half_Y = m_size[1] * 0.5f;
float Z = m_size[2];
RotationMatrixf rotation;
rotation = Maths::AngleAxisf(m_orientation[0] * SOLAR_DEG2RAD, Vector3f::UnitX())
* Maths::AngleAxisf(m_orientation[1] * SOLAR_DEG2RAD, Vector3f::UnitY())
* Maths::AngleAxisf(m_orientation[2] * SOLAR_DEG2RAD, Vector3f::UnitZ());
Vector3f translation;
translation(0) = m_position[0];
translation(1) = m_position[1];
translation(2) = m_position[2];
Transform3Df transform;
transform.setIdentity();
transform.translate(translation);
transform.rotate(rotation);
std::vector<Vector4f> parallelepiped;
parallelepiped.push_back(transform * Vector4f(-half_X, -half_Y, 0.0f, 1.0f));
parallelepiped.push_back(transform * Vector4f( half_X, -half_Y, 0.0f, 1.0f));
parallelepiped.push_back(transform * Vector4f( half_X, half_Y, 0.0f, 1.0f));
parallelepiped.push_back(transform * Vector4f(-half_X, half_Y, 0.0f, 1.0f));
parallelepiped.push_back(transform * Vector4f(-half_X, -half_Y, -Z, 1.0f));
parallelepiped.push_back(transform * Vector4f( half_X, -half_Y, -Z, 1.0f));
parallelepiped.push_back(transform * Vector4f( half_X, half_Y, -Z, 1.0f));
parallelepiped.push_back(transform * Vector4f(-half_X, half_Y, -Z, 1.0f));
for (unsigned int i = 0; i < parallelepiped.size(); i++)
for (int j = 0; j < 3; j++)
m_parallelepiped.at< float >(i, j) = parallelepiped[i](j);
return xpcf::XPCFErrorCode::_SUCCESS;
}
void SolAR3DOverlayBoxOpencv::draw (const Transform3Df & pose, SRef<Image> displayImage)
{
Transform3Df poseInverse = pose.inverse();
// image where parallelepiped will be displayed
cv::Mat displayedImage = SolAROpenCVHelper::mapToOpenCV(displayImage);
// where to store image points of parallelepiped with pose applied
std::vector<cv::Point2f> imagePoints;
// Rotation and Translation from input pose
cv::Mat Rvec; Rvec.create(3, 3, CV_32FC1);
cv::Mat Tvec; Tvec.create(3, 1, CV_32FC1);
Rvec.at<float>(0,0) = poseInverse(0,0);
Rvec.at<float>(0,1) = poseInverse(0,1);
Rvec.at<float>(0,2) = poseInverse(0,2);
Rvec.at<float>(1,0) = poseInverse(1,0);
Rvec.at<float>(1,1) = poseInverse(1,1);
Rvec.at<float>(1,2) = poseInverse(1,2);
Rvec.at<float>(2,0) = poseInverse(2,0);
Rvec.at<float>(2,1) = poseInverse(2,1);
Rvec.at<float>(2,2) = poseInverse(2,2);
Tvec.at<float>(0,0) = poseInverse(0,3);
Tvec.at<float>(1,0) = poseInverse(1,3);
Tvec.at<float>(2,0) = poseInverse(2,3);
cv::Mat rodrig;
cv::Rodrigues(Rvec,rodrig);
// compute the projection of the points of the cube
cv::projectPoints(m_parallelepiped, rodrig, Tvec, m_camMatrix, m_camDistorsion, imagePoints);
// draw parallelepiped
// circle around corners
for(auto & element : imagePoints)
{
if (element.x >= 0 && element.x < displayedImage.cols && element.y >= 0 && element.y < displayedImage.rows)
circle(displayedImage, element, 8, cv::Scalar(128, 0, 128), -1);
}
// finally draw cube
for (int i = 0; i < 4; i++)
{
SolAROpenCVHelper::drawCVLine(displayedImage, imagePoints[i], imagePoints[(i + 1) % 4], cv::Scalar(0,0,255), 4);
SolAROpenCVHelper::drawCVLine(displayedImage, imagePoints[i + 4], imagePoints[4 + (i + 1) % 4], cv::Scalar(0,255,0), 4);
SolAROpenCVHelper::drawCVLine(displayedImage, imagePoints[i], imagePoints[i + 4], cv::Scalar(255,0,0), 4);
}
}
void SolAR3DOverlayBoxOpencv::setCameraParameters(const CamCalibration & intrinsic_param, const CamDistortion & distorsion_param)
{
m_camDistorsion.at<float>(0, 0) = distorsion_param(0);
m_camDistorsion.at<float>(1, 0) = distorsion_param(1);
m_camDistorsion.at<float>(2, 0) = distorsion_param(2);
m_camDistorsion.at<float>(3, 0) = distorsion_param(3);
m_camDistorsion.at<float>(4, 0) = distorsion_param(4);
m_camMatrix.at<float>(0, 0) = intrinsic_param(0,0);
m_camMatrix.at<float>(0, 1) = intrinsic_param(0,1);
m_camMatrix.at<float>(0, 2) = intrinsic_param(0,2);
m_camMatrix.at<float>(1, 0) = intrinsic_param(1,0);
m_camMatrix.at<float>(1, 1) = intrinsic_param(1,1);
m_camMatrix.at<float>(1, 2) = intrinsic_param(1,2);
m_camMatrix.at<float>(2, 0) = intrinsic_param(2,0);
m_camMatrix.at<float>(2, 1) = intrinsic_param(2,1);
m_camMatrix.at<float>(2, 2) = intrinsic_param(2,2);
}
}
}
}