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),
40 mImageSize(cv::Size(0,0))
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, size_t width, size_t height)
153 mImageSize = cv::Size(width, height);
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");
198 void DfsOCV::Run(DfsData& base, DfsData& extra)
202 if (!base.data || !extra.data) {
203 throw std::runtime_error("invalid data pointer");
206 if (cv::Size(base.width, base.height) != mImageSize ||
207 cv::Size(extra.width, extra.height) != mImageSize) {
208 throw std::runtime_error("invalid size");
211 int baseCvType = ConvertDfsDataTypeToCV(base.type);
212 int extraCvType = ConvertDfsDataTypeToCV(extra.type);
213 if (baseCvType < 0 || extraCvType < 0) {
214 throw std::runtime_error("invalid data type");
217 cv::Mat baseMat(cv::Size(base.width, base.height), baseCvType, base.data);
218 cv::Mat extraMat(cv::Size(extra.width, extra.height), extraCvType, extra.data);
220 if (baseMat.size() != extraMat.size()) {
221 throw std::runtime_error("base and extra should be the same size");
224 if (baseMat.type() != extraMat.type()) {
225 throw std::runtime_error("base and extra should be the type");
228 cv::Mat rBaseMat, rExtraMat, dispMat;
229 cv::remap(baseMat, rBaseMat, mBaseReMap[0], mBaseReMap[1], cv::INTER_LINEAR);
230 cv::remap(extraMat, rExtraMat, mExtraReMap[0], mExtraReMap[1], cv::INTER_LINEAR);
232 mDfsOcv->compute(rBaseMat, rExtraMat, dispMat);
233 dispMat.convertTo(mDispMat, CV_8UC1, 1.0/16.0);
235 mDepthData.data = mDispMat.data;
236 mDepthData.type = DFS_DATA_TYPE_UINT8;
237 mDepthData.width = mDispMat.cols;
238 mDepthData.height = mDispMat.rows;
239 mDepthData.stride = mDispMat.elemSize();
244 DfsData& DfsOCV::GetDepthData()
255 class IDfsAdaptation *AdaptorInit(void)
257 //InferenceTFLite *engine = new InferenceTFLite();
258 DfsOCV *adaptor = new DfsOCV();
262 void AdaptorDestroy(class IDfsAdaptation *adaptor)