Skip to content

Commit 18a59b4

Browse files
author
Konstantin Matskevich
committed
fixes
1 parent ddc2351 commit 18a59b4

File tree

2 files changed

+49
-45
lines changed

2 files changed

+49
-45
lines changed

modules/calib3d/src/opencl/stereobm.cl

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
#define MAX_VAL 32767
5252

5353
void calcDisp(__local short * costFunc, __global short * disp, int uniquenessRatio/*, int textureTreshold, short textsum*/,
54-
int mindisp, int ndisp, int w, __local short * dispbuf, int d)
54+
int mindisp, int ndisp, int w, __local short * dispbuf, int d, int x, int y, int cols, int rows, int wsz2)
5555
{
5656
short FILTERED = (mindisp - 1)<<4;
5757
short best_disp = FILTERED, best_cost = MAX_VAL-1;
@@ -73,6 +73,7 @@ void calcDisp(__local short * costFunc, __global short * disp, int uniquenessRat
7373
}
7474
best_disp = ndisp - dispbuf[0] - 1;
7575
best_cost = costFunc[(ndisp-best_disp-1)*w];
76+
barrier(CLK_LOCAL_MEM_FENCE);
7677

7778
int thresh = best_cost + (best_cost * uniquenessRatio/100);
7879
dispbuf[d] = ( (cost[d*w] <= thresh) && (d < (ndisp - best_disp - 2) || d > (ndisp - best_disp) ) ) ? FILTERED : best_disp;
@@ -90,7 +91,7 @@ void calcDisp(__local short * costFunc, __global short * disp, int uniquenessRat
9091

9192
// best_disp = (textsum < textureTreshold) ? FILTERED : best_disp;
9293

93-
if( dispbuf[0] != FILTERED )
94+
if( dispbuf[0] != FILTERED && x < cols-wsz2-mindisp && y < rows-wsz2)
9495
{
9596
cost = &costFunc[0] + (ndisp - best_disp - 1)*w;
9697
int y3 = ((ndisp - best_disp - 1) > 0) ? cost[-w] : cost[w],
@@ -179,68 +180,71 @@ __kernel void stereoBM_opt(__global const uchar * leftptr, __global const uchar
179180
cost = costFunc + costIdx;
180181

181182
short tempcost = 0;
182-
for(int i = 0; i < wsz; i++)
183+
if(x < cols-wsz2-mindisp && y < rows-wsz2)
183184
{
184-
int idx = mad24(y-wsz2+i*nthread, cols, x-wsz2+i*(1-nthread));
185-
left = leftptr + idx;
186-
right = rightptr + (idx - d);
187-
short costdiff = 0;
188-
189-
for(int j = 0; j < wsz; j++)
190-
{
191-
costdiff += abs( left[0] - right[0] );
192-
left += 1*nthread + cols*(1-nthread);
193-
right += 1*nthread + cols*(1-nthread);// maybe use ? operator
194-
}
195-
if(nthread==1)
185+
for(int i = 0; i < wsz; i++)
196186
{
197-
tempcost += costdiff;
187+
int idx = mad24(y-wsz2+i*nthread, cols, x-wsz2+i*(1-nthread));
188+
left = leftptr + idx;
189+
right = rightptr + (idx - d);
190+
short costdiff = 0;
191+
192+
for(int j = 0; j < wsz; j++)
193+
{
194+
costdiff += abs( left[0] - right[0] );
195+
left += 1*nthread + cols*(1-nthread);
196+
right += 1*nthread + cols*(1-nthread);// maybe use ? operator
197+
}
198+
if(nthread==1)
199+
{
200+
tempcost += costdiff;
201+
}
202+
costbuf[head] = costdiff;
203+
head++;
198204
}
199-
costbuf[head] = costdiff;
200-
head++;
201205
}
202206
barrier(CLK_LOCAL_MEM_FENCE);
203207
cost[0] = tempcost;
204208

205-
if(x < cols-wsz2-mindisp && y < rows-wsz2 && nthread == 1)
206-
{
207-
int dispIdx = mad24(gy, disp_step, disp_offset + gx*(int)sizeof(short));
208-
disp = (__global short *)(dispptr + dispIdx);
209-
calcDisp(&costFunc[sizeY - 1 + lx - ly], disp, uniquenessRatio, /*textureTreshold, textsum,*/
210-
mindisp, ndisp, 2*sizeY, &dispbuf[nthread*tsize/2], d);
211-
}
209+
int dispIdx = mad24(gy, disp_step, disp_offset + gx*(int)sizeof(short));
210+
disp = (__global short *)(dispptr + dispIdx);
211+
calcDisp(&costFunc[sizeY - 1 + lx - ly], disp, uniquenessRatio, /*textureTreshold, textsum,*/
212+
mindisp, ndisp, 2*sizeY, &dispbuf[nthread*tsize/2], d, x, y, cols, rows, wsz2);
212213
barrier(CLK_LOCAL_MEM_FENCE);
213214

214215
lx = 1 - nthread;
215216
ly = nthread;
216217

217-
while(lx < sizeX && ly < sizeY )
218+
while(lx < sizeX || ly < sizeY )
218219
{
219-
x = gx + shiftX + lx;
220-
y = gy + shiftY + ly;
220+
x = (lx < sizeX) ? gx + shiftX + lx : cols;
221+
y = (ly < sizeY) ? gy + shiftY + ly : rows;
221222

222223
costIdx = calcLocalIdx(lx, ly, d, sizeY);
223224
cost = costFunc + costIdx;
224-
cost[0] = ( ly*(1-nthread) + lx*nthread == 0 ) ?
225-
calcCostBorder(leftptr, rightptr, x, y, nthread, wsz2, costbuf, &head, cols, d,
226-
costFunc[calcLocalIdx(lx-1*(1-nthread), ly-1*nthread, d, sizeY)]) :
227-
calcCostInside(leftptr, rightptr, x, y, wsz2, cols, d,
228-
costFunc[calcLocalIdx(lx-1, ly-1, d, sizeY)],
229-
costFunc[calcLocalIdx(lx, ly-1, d, sizeY)],
230-
costFunc[calcLocalIdx(lx-1, ly, d, sizeY)]);
231-
barrier(CLK_LOCAL_MEM_FENCE);
232-
233-
if(x < cols-mindisp-wsz2 && y < rows-wsz2)
225+
if(x < cols-wsz2-mindisp && y < rows-wsz2 )
234226
{
235-
int dispIdx = mad24(gy+ly, disp_step, disp_offset + (gx+lx)*(int)sizeof(short));
236-
disp = (__global short *)(dispptr + dispIdx);
237-
calcDisp(&costFunc[sizeY - 1 - ly + lx], disp, uniquenessRatio, //textureTreshold, textsum,
238-
mindisp, ndisp, 2*sizeY, &dispbuf[nthread*tsize/2], d);
227+
cost[0] = ( ly*(1-nthread) + lx*nthread == 0 ) ?
228+
calcCostBorder(leftptr, rightptr, x, y, nthread, wsz2, costbuf, &head, cols, d,
229+
costFunc[calcLocalIdx(lx-1*(1-nthread), ly-1*nthread, d, sizeY)]) :
230+
calcCostInside(leftptr, rightptr, x, y, wsz2, cols, d,
231+
costFunc[calcLocalIdx(lx-1, ly-1, d, sizeY)],
232+
costFunc[calcLocalIdx(lx, ly-1, d, sizeY)],
233+
costFunc[calcLocalIdx(lx-1, ly, d, sizeY)]);
239234
}
240235
barrier(CLK_LOCAL_MEM_FENCE);
241236

237+
int dispIdx = mad24(gy+ly, disp_step, disp_offset + (gx+lx)*(int)sizeof(short));
238+
disp = (__global short *)(dispptr + dispIdx);
239+
calcDisp(&costFunc[sizeY - 1 - ly + lx], disp, uniquenessRatio, //textureTreshold, textsum,
240+
mindisp, ndisp, 2*sizeY, &dispbuf[nthread*tsize/2], d, x, y, cols, rows, wsz2);
241+
barrier(CLK_LOCAL_MEM_FENCE);
242+
242243
calcNewCoordinates(&lx, &ly, nthread);
243244
}
245+
246+
247+
244248
}
245249

246250
#endif

modules/calib3d/test/opencl/test_stereobm.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,18 @@ OCL_TEST_P(StereoBMFixture, StereoBM)
9090
cv::ocl::finish();
9191
long t3 = clock();
9292
std::cout << (double)(t2-t1)/CLOCKS_PER_SEC << " " << (double)(t3-t2)/CLOCKS_PER_SEC << std::endl;
93-
/*
93+
9494
Mat t; absdiff(disp, udisp, t);
9595
for(int i = 0; i<t.rows; i++)
9696
for(int j = 0; j< t.cols; j++)
9797
if(t.at<short>(i,j) > 0)
98-
// if(i== 255 && j == 375)
98+
// if(i== 12 && j == 44)
9999
printf("%d %d cv: %d ocl: %d\n", i, j, disp.at<short>(i,j), udisp.getMat(ACCESS_READ).at<short>(i,j) );
100100
/* imshow("diff.png", t*100);
101101
imshow("cv.png", disp*100);
102102
imshow("ocl.png", udisp.getMat(ACCESS_READ)*100);
103103
waitKey(0);*/
104-
// Near(1e-3);
104+
Near(1e-3);
105105
}
106106

107107
OCL_INSTANTIATE_TEST_CASE_P(StereoMatcher, StereoBMFixture, testing::Combine(testing::Values(32, 64, 128),

0 commit comments

Comments
 (0)