Change output type from 32float to unsigned 16bit integer
[platform/core/multimedia/dfs-opencv.git] / src / dfs_opencv_private.h
1 /**
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifndef __DFS_OPENCV_PRIVATE_H__
18 #define __DFS_OPENCV_PRIVATE_H__
19
20 #include <dfs_adaptation.h>
21 #include <dfs_parameter.h>
22 #include <thread>
23 #include <future>
24 #include <queue>
25 #include <mutex>
26 #include <condition_variable>
27 #include <functional>
28
29 #include <opencv2/core.hpp>
30 #include <opencv2/calib3d.hpp>
31 #include <opencv2/imgproc.hpp>
32 #include <opencv2/ximgproc.hpp>
33 #include <opencv2/ximgproc/disparity_filter.hpp>
34
35 /**
36 * @file dfs_opencv_private.h
37 * @brief This file contains the DfsOCV class which
38 *        provides OpenCV based depth functionality
39 */
40
41 #ifdef LOG_TAG
42 #undef LOG_TAG
43 #endif
44
45 #define LOG_TAG "DFS_OCV"
46
47 using namespace DfsAdaptation;
48
49 namespace DfsAdaptationImpl
50 {
51         typedef struct _CameraParam {
52                 cv::Mat intrinsic;
53                 cv::Mat distCoeffs;
54
55                 _CameraParam()
56                 {
57                         intrinsic = cv::Mat(cv::Size(3,3), CV_32FC1);
58                         distCoeffs = cv::Mat(cv::Size(1,5), CV_32FC1);
59                 }
60         } CameraParam;
61
62         typedef struct _StereoParam {
63                 CameraParam baceCamera;
64                 CameraParam extraCamera;
65
66                 cv::Mat translation;
67                 cv::Mat rotation;
68                 float dispOffset;
69
70                 _StereoParam()
71                 {
72                         translation = cv::Mat(cv::Size(1,3), CV_32FC1);
73                         rotation = cv::Mat(cv::Size(1,3), CV_32FC1);
74                         dispOffset = 0.f;
75                 }
76         } StereoParam;
77
78         class DfsOCV : public IDfsAdaptation
79         {
80         private:
81                 cv::Ptr<cv::StereoSGBM> mDfsOcv;
82                 cv::Ptr<cv::StereoMatcher> mDfsOcvExtra;
83                 cv::Ptr<cv::ximgproc::DisparityWLSFilter> mDfsPostOcv;
84
85                 DfsParameter mDfsParam;
86                 size_t mNumDisparities;
87                 size_t mBlockSize;
88                 size_t mMinDisparity;
89                 size_t mP1;
90                 size_t mP2;
91                 size_t mPreFilterCap;
92                 static constexpr size_t mDispShift = 16;
93                 static constexpr float mDispShiftInv = 1.0f/16.f;
94
95
96                 cv::Mat mDispMat;
97                 cv::Mat mDepthMat;
98                 cv::Mat mDisp2DepthMat;
99                 cv::Mat mDispOffsetMat;
100                 DfsData mDepthData;
101
102                 StereoParam mStereoParam;
103                 bool mIsStereoCalibrated;
104                 bool mUpdateStereoCalibration;
105
106                 cv::Size mImageSize;
107                 cv::Mat mBaseReMap[2];
108                 cv::Mat mExtraReMap[2];
109
110                 size_t mDownScale;
111
112                 bool mIsStopAll;
113                 std::vector<std::thread> mThreadPool;
114                 std::queue<std::function<void()> > mJobs;
115                 std::mutex mMutexJob;
116                 std::condition_variable mControlJob;
117
118                 void Runner();
119
120                 template <class F, class... Args>
121                 std::future<bool> EnqueueJob(F f, Args... args);
122
123                 void SetParameters();
124                 int  ConvertDfsDataTypeToCV(int type);
125                 void InitializeStereoCalibration(std::string filepath);
126                 void InitRectifyMap();
127
128                 bool computeL(const cv::Mat& baseMat, const cv::Mat& extraMat, cv::Mat& disp);
129                 bool computeR(const cv::Mat& baseMat, const cv::Mat& extraMat, cv::Mat& disp);
130
131         public:
132                 DfsOCV();
133                 ~DfsOCV();
134
135                 void Initialize(DfsParameter& param, size_t width, size_t height,
136                                                 size_t minDisp, size_t maxDisp, std::string stereoConfigPath) override;
137                 void Run(DfsData& base, DfsData& extra) override;
138
139                 DfsData& GetDepthData() override;
140
141         };
142 }
143
144 #endif /* __DFS_OPENCV_PRIVATE_H__ */