Commit 74a4ed9e authored by Tomoaki Teshima's avatar Tomoaki Teshima

[moved from opencv] fix test failure of StereoBeliefPropagation

  * if the src has odd number of height, access error happens
  * it could happen on width, too
  * check both dst and src range in both width and height

original commit: https://github.com/opencv/opencv/commit/7294ae0f17282d52f5734d4cd45482f6c40b4b28
parent b8dc51ad
...@@ -255,7 +255,7 @@ namespace cv { namespace cuda { namespace device ...@@ -255,7 +255,7 @@ namespace cv { namespace cuda { namespace device
/////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////
template <typename T> template <typename T>
__global__ void data_step_down(int dst_cols, int dst_rows, int src_rows, const PtrStep<T> src, PtrStep<T> dst) __global__ void data_step_down(int dst_cols, int dst_rows, int src_cols, int src_rows, const PtrStep<T> src, PtrStep<T> dst)
{ {
const int x = blockIdx.x * blockDim.x + threadIdx.x; const int x = blockIdx.x * blockDim.x + threadIdx.x;
const int y = blockIdx.y * blockDim.y + threadIdx.y; const int y = blockIdx.y * blockDim.y + threadIdx.y;
...@@ -264,10 +264,15 @@ namespace cv { namespace cuda { namespace device ...@@ -264,10 +264,15 @@ namespace cv { namespace cuda { namespace device
{ {
for (int d = 0; d < cndisp; ++d) for (int d = 0; d < cndisp; ++d)
{ {
float dst_reg = src.ptr(d * src_rows + (2*y+0))[(2*x+0)]; // check the index of src
dst_reg += src.ptr(d * src_rows + (2*y+1))[(2*x+0)]; const int x0 = 2 * x;
dst_reg += src.ptr(d * src_rows + (2*y+0))[(2*x+1)]; const int x1 = ::min(x0 + 1, src_cols - 1);
dst_reg += src.ptr(d * src_rows + (2*y+1))[(2*x+1)]; const int y0 = 2 * y;
const int y1 = ::min(y0 + 1, src_rows - 1);
float dst_reg = src.ptr(d * src_rows + y0)[x0];
dst_reg += src.ptr(d * src_rows + y1)[x0];
dst_reg += src.ptr(d * src_rows + y0)[x1];
dst_reg += src.ptr(d * src_rows + y1)[x1];
dst.ptr(d * dst_rows + y)[x] = saturate_cast<T>(dst_reg); dst.ptr(d * dst_rows + y)[x] = saturate_cast<T>(dst_reg);
} }
...@@ -275,7 +280,7 @@ namespace cv { namespace cuda { namespace device ...@@ -275,7 +280,7 @@ namespace cv { namespace cuda { namespace device
} }
template<typename T> template<typename T>
void data_step_down_gpu(int dst_cols, int dst_rows, int src_rows, const PtrStepSzb& src, const PtrStepSzb& dst, cudaStream_t stream) void data_step_down_gpu(int dst_cols, int dst_rows, int src_cols, int src_rows, const PtrStepSzb& src, const PtrStepSzb& dst, cudaStream_t stream)
{ {
dim3 threads(32, 8, 1); dim3 threads(32, 8, 1);
dim3 grid(1, 1, 1); dim3 grid(1, 1, 1);
...@@ -283,15 +288,15 @@ namespace cv { namespace cuda { namespace device ...@@ -283,15 +288,15 @@ namespace cv { namespace cuda { namespace device
grid.x = divUp(dst_cols, threads.x); grid.x = divUp(dst_cols, threads.x);
grid.y = divUp(dst_rows, threads.y); grid.y = divUp(dst_rows, threads.y);
data_step_down<T><<<grid, threads, 0, stream>>>(dst_cols, dst_rows, src_rows, (PtrStepSz<T>)src, (PtrStepSz<T>)dst); data_step_down<T><<<grid, threads, 0, stream>>>(dst_cols, dst_rows, src_cols, src_rows, (PtrStepSz<T>)src, (PtrStepSz<T>)dst);
cudaSafeCall( cudaGetLastError() ); cudaSafeCall( cudaGetLastError() );
if (stream == 0) if (stream == 0)
cudaSafeCall( cudaDeviceSynchronize() ); cudaSafeCall( cudaDeviceSynchronize() );
} }
template void data_step_down_gpu<short>(int dst_cols, int dst_rows, int src_rows, const PtrStepSzb& src, const PtrStepSzb& dst, cudaStream_t stream); template void data_step_down_gpu<short>(int dst_cols, int dst_rows, int src_cols, int src_rows, const PtrStepSzb& src, const PtrStepSzb& dst, cudaStream_t stream);
template void data_step_down_gpu<float>(int dst_cols, int dst_rows, int src_rows, const PtrStepSzb& src, const PtrStepSzb& dst, cudaStream_t stream); template void data_step_down_gpu<float>(int dst_cols, int dst_rows, int src_cols, int src_rows, const PtrStepSzb& src, const PtrStepSzb& dst, cudaStream_t stream);
/////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////
/////////////////// level up messages //////////////////////// /////////////////// level up messages ////////////////////////
......
...@@ -61,7 +61,7 @@ namespace cv { namespace cuda { namespace device ...@@ -61,7 +61,7 @@ namespace cv { namespace cuda { namespace device
template<typename T, typename D> template<typename T, typename D>
void comp_data_gpu(const PtrStepSzb& left, const PtrStepSzb& right, const PtrStepSzb& data, cudaStream_t stream); void comp_data_gpu(const PtrStepSzb& left, const PtrStepSzb& right, const PtrStepSzb& data, cudaStream_t stream);
template<typename T> template<typename T>
void data_step_down_gpu(int dst_cols, int dst_rows, int src_rows, const PtrStepSzb& src, const PtrStepSzb& dst, cudaStream_t stream); void data_step_down_gpu(int dst_cols, int dst_rows, int src_cols, int src_rows, const PtrStepSzb& src, const PtrStepSzb& dst, cudaStream_t stream);
template <typename T> template <typename T>
void level_up_messages_gpu(int dst_idx, int dst_cols, int dst_rows, int src_rows, PtrStepSzb* mus, PtrStepSzb* mds, PtrStepSzb* mls, PtrStepSzb* mrs, cudaStream_t stream); void level_up_messages_gpu(int dst_idx, int dst_cols, int dst_rows, int src_rows, PtrStepSzb* mus, PtrStepSzb* mds, PtrStepSzb* mls, PtrStepSzb* mrs, cudaStream_t stream);
template <typename T> template <typename T>
...@@ -283,7 +283,7 @@ namespace ...@@ -283,7 +283,7 @@ namespace
{ {
using namespace cv::cuda::device::stereobp; using namespace cv::cuda::device::stereobp;
typedef void (*data_step_down_t)(int dst_cols, int dst_rows, int src_rows, const PtrStepSzb& src, const PtrStepSzb& dst, cudaStream_t stream); typedef void (*data_step_down_t)(int dst_cols, int dst_rows, int src_cols, int src_rows, const PtrStepSzb& src, const PtrStepSzb& dst, cudaStream_t stream);
static const data_step_down_t data_step_down_callers[2] = static const data_step_down_t data_step_down_callers[2] =
{ {
data_step_down_gpu<short>, data_step_down_gpu<float> data_step_down_gpu<short>, data_step_down_gpu<float>
...@@ -318,7 +318,7 @@ namespace ...@@ -318,7 +318,7 @@ namespace
datas_[i].create(rows_all_[i] * ndisp_, cols_all_[i], msg_type_); datas_[i].create(rows_all_[i] * ndisp_, cols_all_[i], msg_type_);
data_step_down_callers[funcIdx](cols_all_[i], rows_all_[i], rows_all_[i-1], datas_[i-1], datas_[i], stream); data_step_down_callers[funcIdx](cols_all_[i], rows_all_[i], cols_all_[i-1], rows_all_[i-1], datas_[i-1], datas_[i], stream);
} }
PtrStepSzb mus[] = {u_, u2_}; PtrStepSzb mus[] = {u_, u2_};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment