-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathtestStereo.cpp
246 lines (211 loc) · 7.64 KB
/
testStereo.cpp
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <opencp.hpp>
using namespace std;
using namespace cv;
using namespace cp;
void guiDisparityPlaneFitSLICTest(Mat& leftim, Mat& rightim, Mat& GT)
{
string wname = "disparitySLIC";
namedWindow(wname);
int mindisparity = 40;
Ptr<StereoSGBM> sgbm = StereoSGBM::create(mindisparity, 112, 3);
Mat disparity;
//guiShift(leftim, rightim);
sgbm->compute(leftim, rightim, disparity);
//GT.convertTo(disparity, CV_16S, 8);
Mat disp = disparity.clone();
//fillOcclusion(disparity, 0, cp::FILL_DISPARITY);
fillOcclusion(disparity, 16 * (mindisparity - 1), cp::FILL_DISPARITY);
/*{
Mat temp1, temp2;
disparity.convertTo(temp1, CV_8U, 0.125);
disp.convertTo(temp2, CV_8U, 0.125);
guiAlphaBlend(temp1, temp2);
}*/
Mat dispshowRef;
disparity.convertTo(dispshowRef, CV_8U, 0.125);
int a = 0; createTrackbar("a", wname, &a, 100);
int S = 16; createTrackbar("S", wname, &S, 200);
int m = 30; createTrackbar("m", wname, &m, 800);
int mrs = 10; createTrackbar("ratio of min region size", wname, &mrs, 100);
int iter = 20; createTrackbar("iteration", wname, &iter, 1000);
int nransac = 30; createTrackbar("samples", wname, &nransac, 1000);
int transac = 3; createTrackbar("threshold", wname, &transac, 1000);
int key = 0;
Mat refine;
Mat dispshow;
Mat show;
Mat seg;
Mat slicout;
bool isStop = false;
Mat buffer;
namedWindow("SLIC"); moveWindow("SLIC", 0, 0);
while (key != 'q')
{
{
Timer t;
disparityFitPlane(disparity, leftim, refine, S, (float)m, (float)(mrs / 100.0f), iter, nransac, (float)transac);
}
//binalyWeightedRangeFilter(disparity, disparity, Size(7, 7), 16);
if (isStop) buffer.copyTo(refine);
else refine.copyTo(buffer);
SLIC(leftim, seg, S, (float)m, mrs / 100.0f, iter);
refine.convertTo(dispshow, CV_8U, 0.125);
imshow(wname, dispshow);
drawSLIC(dispshow, seg, slicout, false, true, Scalar(255, 255, 0));
imshow("SLIC", slicout);
alphaBlend(dispshowRef, dispshow, a / 100.0, show);
imshow(wname, show);
key = waitKey(1);
Mat save; cvtColor(show, save, COLOR_GRAY2BGR); static int count = 0; imwrite(format("GIF/out%d.png", count++), save);
if (key == 'p')
{
isStop = (isStop) ? false : true;
}
if (key == 'f')
{
a = (a != 0) ? 0 : 100;
setTrackbarPos("a", wname, a);
}
}
}
void testCVStereoBM()
{
Mat disp;
Mat leftim = imread("img/stereo/Dolls/view1.png", 0);
Mat rightim = imread("img/stereo/Dolls/view5.png", 0);
Mat dmap_ = imread("img/stereo/Dolls/disp1.png", 0);
//guiShift(left, right, 300);
const int disp_min = get_simd_ceil(32, 16);
const int disp_max = get_simd_ceil(100, 16);
cp::StereoEval eval(dmap_, 2, disp_max);
String wname = "Stereo BM Test";
namedWindow(wname);
int blockRadius = 4; createTrackbar("bs", wname, &blockRadius, 30);
int prefilterCap = 31; createTrackbar("pre cap", wname, &prefilterCap, 63);
int textureThreshold = 10; createTrackbar("Tx thresh", wname, &textureThreshold, 255);
int uniquenessRatio = 15; createTrackbar("uniqueness", wname, &uniquenessRatio, 100);
int lrThreshold = 1; createTrackbar("LR thresh", wname, &lrThreshold, 20);
int spWindow = 100; createTrackbar("speckle win", wname, &spWindow, 2550);
int spRange = 32; createTrackbar("speck range", wname, &spRange, 255);
int minDisp = disp_min; createTrackbar("min disp", wname, &minDisp, 255);
//int numDisp = numDisparities / 16; createTrackbar("num disp", wname, &numDisp, numDisparities / 16 * 2);
Ptr<StereoBM> bm = StereoBM::create(disp_max - disp_min, 2 * blockRadius + 1);
Mat dispshow;
int key = 0;
ConsoleImage ci;
Timer t;
while (key != 'q')
{
int SADWindowSize = 2 * blockRadius + 1;
bm->setPreFilterCap(max(1, prefilterCap));
bm->setBlockSize(max(SADWindowSize, 5));
bm->setTextureThreshold(textureThreshold);
bm->setUniquenessRatio(uniquenessRatio);
bm->setDisp12MaxDiff(lrThreshold);
bm->setSpeckleWindowSize(spWindow);
bm->setSpeckleRange(spRange);
bm->setMinDisparity(minDisp);
//bm->setNumDisparities(numDisp * 16);
t.start();
bm->compute(leftim, rightim, disp);
t.getpushLapTime();
const int invalid = (minDisp - 1) * 16;
fillOcclusion(disp, invalid);
//normalize(disp, dispshow, minDisp, numDisp * 16, NORM_MINMAX, CV_8U);
//disp.convertTo(dispshow, CV_8U, 255.0/(16.0*numDisparities));
ci("Time %5.2f ms", t.getLapTimeMedian());
ci("th 0.5:" + eval(disp, 0.5, 16, false));
ci("th 1.0:" + eval(disp, 1.0, 16, false));
ci("th 2.0:" + eval(disp, 2.0, 16, false));
imshowScale(wname, disp, 2.0 / 16);
ci.show();
key = waitKey(1);
}
}
void testCVStereoSGBM()
{
Mat disp;
Mat leftim = imread("img/stereo/Dolls/view1.png");
Mat rightim = imread("img/stereo/Dolls/view5.png");
Mat dmap_ = imread("img/stereo/Dolls/disp1.png", 0);
//guiShift(left, right, 300);
const int disp_min = get_simd_ceil(32, 16);
const int disp_max = get_simd_ceil(100, 16);
int numDisparities = disp_max - disp_min;
cp::StereoEval eval(dmap_, 2, disp_max);
String wname = "Stereo SGBM Test";
namedWindow(wname);
int blockRadius = 1; createTrackbar("bs", wname, &blockRadius, 30);
int P1 = 8; createTrackbar("P1", wname, &P1, 255);
int P2 = 32; createTrackbar("P2", wname, &P2, 255);
int prefilterCap = 31; createTrackbar("pre cap", wname, &prefilterCap, 63);
//int textureThreshold = 10; createTrackbar("Tx thresh", wname, &textureThreshold, 255);
int uniquenessRatio = 15; createTrackbar("uniqueness", wname, &uniquenessRatio, 100);
int lrThreshold = 1; createTrackbar("LR thresh", wname, &lrThreshold, 20);
int spWindow = 100; createTrackbar("speckle win", wname, &spWindow, 2550);
int spRange = 32; createTrackbar("speck range", wname, &spRange, 255);
Ptr<StereoSGBM> bm = StereoSGBM::create(disp_min, numDisparities, 2 * blockRadius + 1);
Mat lim; copyMakeBorder(leftim, lim, 0, 0, numDisparities, 0, BORDER_REPLICATE);
Mat rim; copyMakeBorder(rightim, rim, 0, 0, numDisparities, 0, BORDER_REPLICATE);
Mat dispshowROI, dispshow;
int key = 0;
ConsoleImage ci;
Timer t;
while (key != 'q')
{
int SADWindowSize = 2 * blockRadius + 1;
bm->setPreFilterCap(max(1, prefilterCap));
bm->setP1(P1);
bm->setP2(P2);
bm->setBlockSize(SADWindowSize);
//bm->setTextureThreshold(textureThreshold);
bm->setUniquenessRatio(uniquenessRatio);
bm->setDisp12MaxDiff(lrThreshold);
bm->setSpeckleWindowSize(spWindow);
bm->setSpeckleRange(spRange);
/*
bm->setROI1(roi1);
bm->setROI2(roi2);
bm->setMinDisparity(0);
bm->setNumDisparities(numberOfDisparities);
*/
t.start();
bm->compute(lim, rim, disp);
t.getpushLapTime();
const int invalid = (disp_min - 1) * 16;
fillOcclusion(disp, invalid);
Mat(disp(Rect(numDisparities, 0, leftim.cols, leftim.rows))).copyTo(dispshow);
//guiAlphaBlend(dispshow, dmap_);
imshowScale(wname, dispshow, 2.0 / 16);
ci("Time %5.2f ms", t.getLapTimeMedian());
ci("th 0.5:" + eval(dispshow, 0.5, 16, false));
ci("th 1.0:" + eval(dispshow, 1.0, 16, false));
ci("th 2.0:" + eval(dispshow, 2.0, 16, false));
ci.show();
key = waitKey(1);
}
}
void testStereoBase()
{
Mat disp;
Mat left_ = imread("img/stereo/Dolls/view1.png");
Mat right_ = imread("img/stereo/Dolls/view5.png");
Mat dmap_ = imread("img/stereo/Dolls/disp1.png", 0);
Mat left = left_;
Mat right = right_;
Mat dmap = dmap_;
const bool isPad = false;
if (isPad)
{
int pad = get_simd_ceil(left_.cols, 16) - left_.cols;
copyMakeBorder(left_, left, 0, 0, 0, pad, cv::BORDER_DEFAULT);
copyMakeBorder(right_, right, 0, 0, 0, pad, cv::BORDER_DEFAULT);
copyMakeBorder(dmap_, dmap, 0, 0, 0, pad, cv::BORDER_DEFAULT);
}
//guiShift(left, right, 300);
const int disp_min = get_simd_ceil(32, 16);
const int disp_max = get_simd_ceil(100, 16);
cp::StereoEval eval(dmap, 2, disp_max);
StereoBase sbm(5, disp_min, disp_max);
sbm.gui(left, right, disp, eval);
}