2 * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "dfs_opencv_private.h"
19 #include <opencv2/core/persistence.hpp>
21 #define DEFAULT_STEREO_CALIB_FILE_NAME "stereoCalibZed.yaml"
23 namespace DfsAdaptationImpl
36 mCalibFilePath(DFS_CALIB_FILE_PATH),
38 mIsStereoCalibrated(false),
39 mUpdateStereoCalibration(false),
43 mCalibFilePath += std::string(DEFAULT_STEREO_CALIB_FILE_NAME);
53 void DfsOCV::InitializeStereoCalibration()
57 cv::Mat intrinsicTest;
59 cv::FileStorage fs(mCalibFilePath, cv::FileStorage::READ);
61 LOGE("Failed to open calib file %s", mCalibFilePath.c_str());
62 throw std::ios_base::failure("calibration");
65 fs["LEFT_CAM_VGA"] >> mStereoParam.baceCamera.intrinsic;
66 fs["LEFT_CAM_DISTCOEFFS"] >> mStereoParam.baceCamera.distCoeffs;
68 fs["RIGHT_CAM_VGA"] >> mStereoParam.extraCamera.intrinsic;
69 fs["RIGHT_CAM_DISTCOEFFS"] >> mStereoParam.extraCamera.distCoeffs;
71 fs["STEREO_TRANSLATION"] >> mStereoParam.translation;
72 fs["STEREO_ROTATION"] >> mStereoParam.rotation;
74 mIsStereoCalibrated = true;
76 } catch (const std::exception& e) {
77 LOGE("Failed to read calibration data %s", e.what());
78 throw std::ios_base::failure("calibration");
84 void DfsOCV::InitRectifyMap()
89 cv::Mat newBaseCameraIntrinsic = cv::getOptimalNewCameraMatrix(
90 mStereoParam.baceCamera.intrinsic,
91 mStereoParam.baceCamera.distCoeffs,
95 cv::Mat newExtraCameraIntrinsic = cv::getOptimalNewCameraMatrix(
96 mStereoParam.extraCamera.intrinsic,
97 mStereoParam.extraCamera.distCoeffs,
101 cv::Mat extraCameraRotation;
102 cv::Rodrigues(mStereoParam.rotation, extraCameraRotation);
104 cv::Mat baseRotation, extraRotation; // 3x3
105 cv::Mat baseProjection, extraProjection; // 3x4
106 cv::Mat disp2Depth; // 4x4
108 cv::stereoRectify(mStereoParam.baceCamera.intrinsic,
109 mStereoParam.baceCamera.distCoeffs,
110 mStereoParam.extraCamera.intrinsic,
111 mStereoParam.extraCamera.distCoeffs,
114 mStereoParam.translation,
121 cv::initUndistortRectifyMap(mStereoParam.baceCamera.intrinsic,
122 mStereoParam.baceCamera.distCoeffs,
124 newBaseCameraIntrinsic,
130 cv::initUndistortRectifyMap(mStereoParam.extraCamera.intrinsic,
131 mStereoParam.extraCamera.distCoeffs,
133 newExtraCameraIntrinsic,
139 } catch (const std::exception& e) {
140 LOGE("Failed to InitRectifyMap");
148 void DfsOCV::Initialize(DfsParameter& param)
154 mDfsOcv = cv::StereoSGBM::create(1, mNumDisparities, mBlockSize);
156 this->SetParameters();
159 this->InitializeStereoCalibration();
160 this->InitRectifyMap();
161 } catch (const std::exception& e) {
168 void DfsOCV::SetParameters()
172 mDfsOcv->setMinDisparity(mMinDisparity);
173 mDfsOcv->setNumDisparities(mNumDisparities);
174 mDfsOcv->setBlockSize(mBlockSize);
175 mDfsOcv->setP1(mP1 * mBlockSize * mBlockSize);
176 mDfsOcv->setP2(mP2 * mBlockSize * mBlockSize);
177 mDfsOcv->setPreFilterCap(mPreFilterCap);
179 mDfsOcv->setMode(cv::StereoSGBM::MODE_SGBM_3WAY);
183 int DfsOCV::ConvertDfsDataTypeToCV(int type)
187 case DFS_DATA_TYPE_UINT8:
190 LOGE("Invalide type");
197 void DfsOCV::Run(DfsData& base, DfsData& extra)
201 if (!base.data || !extra.data) {
202 throw std::runtime_error("invalid data pointer");
205 int baseCvType = ConvertDfsDataTypeToCV(base.type);
206 int extraCvType = ConvertDfsDataTypeToCV(extra.type);
207 if (baseCvType < 0 || extraCvType < 0) {
208 throw std::runtime_error("invalid data type");
211 cv::Mat baseMat(cv::Size(base.width, base.height), baseCvType, base.data);
212 cv::Mat extraMat(cv::Size(extra.width, extra.height), extraCvType, extra.data);
214 if (baseMat.size() != extraMat.size()) {
215 throw std::runtime_error("base and extra should be the same size");
218 if (baseMat.type() != extraMat.type()) {
219 throw std::runtime_error("base and extra should be the type");
222 cv::Mat rBaseMat, rExtraMat;
223 cv::remap(baseMat, rBaseMat, mBaseReMap[0], mBaseReMap[1], cv::INTER_LINEAR);
224 cv::remap(extraMat, rExtraMat, mExtraReMap[0], mExtraReMap[1], cv::INTER_LINEAR);
226 mDfsOcv->compute(rBaseMat, rExtraMat, mDispMat);
228 mDepthData.data = mDispMat.data;
229 mDepthData.type = DFS_DATA_TYPE_UINT8;
230 mDepthData.width = mDispMat.cols;
231 mDepthData.height = mDispMat.rows;
232 mDepthData.stride = mDispMat.elemSize();
237 DfsData& DfsOCV::GetDepthData()
248 class IDfsAdaptation *AdaptorInit(void)
250 //InferenceTFLite *engine = new InferenceTFLite();
251 DfsOCV *adaptor = new DfsOCV();
255 void AdaptorDestroy(class IDfsAdaptation *adaptor)