forked from SolarFramework/SolARModuleOpenCV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolARBaseCameraOpencv.cpp
More file actions
163 lines (138 loc) · 5.6 KB
/
SolARBaseCameraOpencv.cpp
File metadata and controls
163 lines (138 loc) · 5.6 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
/**
* @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 "SolARBaseCameraOpencv.h"
#include "SolAROpenCVHelper.h"
#include "core/Log.h"
namespace xpcf = org::bcom::xpcf;
XPCF_DEFINE_FACTORY_CREATE_INSTANCE(SolAR::MODULES::OPENCV::SolARBaseCameraOpencv)
namespace SolAR {
using namespace datastructure;
namespace MODULES {
namespace OPENCV {
SolARBaseCameraOpencv::SolARBaseCameraOpencv(const org::bcom::xpcf::uuids::uuid & uuid):ConfigurableBase(uuid)
{
declareInterface<api::input::devices::ICamera>(this);
declareProperty("calibrationFile", m_calibrationFile);
m_is_resolution_set = false;
m_parameters.distortion = CamDistortion::Zero();
m_parameters.intrinsic = CamCalibration::Identity();
}
SolARBaseCameraOpencv::~SolARBaseCameraOpencv()
{
if (m_capture.isOpened())
{
m_capture.release();
}
}
xpcf::XPCFErrorCode SolARBaseCameraOpencv::onConfigured()
{
LOG_DEBUG(" SolARBaseCameraOpencv onConfigured");
if (m_calibrationFile.empty())
{
LOG_ERROR("Camera Calibration file path is empty");
return xpcf::XPCFErrorCode::_FAIL;
}
cv::FileStorage fs(m_calibrationFile, cv::FileStorage::READ);
cv::Mat intrinsic_parameters;
cv::Mat distortion_parameters;
if (fs.isOpened())
{
int width, height;
fs["image_width"] >> width;
fs["image_height"] >> height;
fs["camera_matrix"] >> intrinsic_parameters;
fs["distortion_coefficients"] >> distortion_parameters;
m_parameters.resolution.width = width;
m_parameters.resolution.height = height;
m_is_resolution_set = true;
if (intrinsic_parameters.empty())
{
LOG_ERROR ("SolARBaseCameraOpencv::loadCameraParameters: Use the landmark camera_matrix to define the intrinsic matrix in the .yml camera calibration file")
return xpcf::XPCFErrorCode::_FAIL;
}
if (intrinsic_parameters.rows == m_parameters.intrinsic.rows() && intrinsic_parameters.cols == m_parameters.intrinsic.cols())
for (int i = 0; i < intrinsic_parameters.rows; i++)
for (int j = 0; j < intrinsic_parameters.cols; j++)
m_parameters.intrinsic(i,j) = (float)intrinsic_parameters.at<double>(i,j);
else
{
LOG_ERROR("SolARBaseCameraOpencv::loadCameraParameters: Camera Calibration should be a 3x3 Matrix")
return xpcf::XPCFErrorCode::_FAIL;
}
if (distortion_parameters.empty())
{
LOG_ERROR("SolARBaseCameraOpencv::loadCameraParameters: Use the landmark distortion_coefficients to define the distortion vector in the .yml camera calibration file")
return xpcf::XPCFErrorCode::_FAIL;
}
if (distortion_parameters.rows == m_parameters.distortion.rows() && distortion_parameters.cols == m_parameters.distortion.cols())
for (int i = 0; i < distortion_parameters.rows; i++)
for (int j = 0; j < distortion_parameters.cols; j++)
m_parameters.distortion(i,j) = distortion_parameters.at<double>(i,j);
else
{
LOG_ERROR("SolARBaseCameraOpencv::loadCameraParameters: Camera distortion matrix should be a 5x1 Matrix")
return xpcf::XPCFErrorCode::_FAIL;
}
return xpcf::XPCFErrorCode::_SUCCESS;
}
else
{
LOG_ERROR("SolARBaseCameraOpencv::loadCameraParameters: Cannot open camera calibration file ")
return xpcf::XPCFErrorCode::_FAIL;
}
}
void SolARBaseCameraOpencv::setResolution(const Sizei & resolution)
{
m_parameters.resolution = resolution;
m_is_resolution_set = true;
}
FrameworkReturnCode SolARBaseCameraOpencv::stop()
{
if(m_capture.isOpened())
{
m_capture.release();
}
return FrameworkReturnCode::_SUCCESS;
}
void SolARBaseCameraOpencv::setIntrinsicParameters(const CamCalibration & intrinsic_parameters){
m_parameters.intrinsic = intrinsic_parameters;
}
void SolARBaseCameraOpencv::setDistortionParameters(const CamDistortion & distortion_parameters){
m_parameters.distortion = distortion_parameters;
}
void SolARBaseCameraOpencv::setParameters(const CameraParameters & parameters)
{
m_parameters = parameters;
}
const CameraParameters & SolARBaseCameraOpencv::getParameters() const
{
return m_parameters;
}
Sizei SolARBaseCameraOpencv::getResolution() const
{
return m_parameters.resolution;
}
const CamCalibration & SolARBaseCameraOpencv::getIntrinsicsParameters() const
{
return m_parameters.intrinsic;
}
const CamDistortion & SolARBaseCameraOpencv::getDistortionParameters() const
{
return m_parameters.distortion;
}
}
}
}